Coverage Report

Created: 2025-07-02 06:18

/rust/registry/src/index.crates.io-6f17d22bba15001f/serde_bytes-0.11.17/src/lib.rs
Line
Count
Source (jump to first uncovered line)
1
//! Wrapper types to enable optimized handling of `&[u8]` and `Vec<u8>`.
2
//!
3
//! Without specialization, Rust forces Serde to treat `&[u8]` just like any
4
//! other slice and `Vec<u8>` just like any other vector. In reality this
5
//! particular slice and vector can often be serialized and deserialized in a
6
//! more efficient, compact representation in many formats.
7
//!
8
//! When working with such a format, you can opt into specialized handling of
9
//! `&[u8]` by wrapping it in `serde_bytes::Bytes` and `Vec<u8>` by wrapping it
10
//! in `serde_bytes::ByteBuf`.
11
//!
12
//! Additionally this crate supports the Serde `with` attribute to enable
13
//! efficient handling of `&[u8]` and `Vec<u8>` in structs without needing a
14
//! wrapper type.
15
//!
16
//! ```
17
//! # use serde_derive::{Deserialize, Serialize};
18
//! use serde::{Deserialize, Serialize};
19
//!
20
//! #[derive(Deserialize, Serialize)]
21
//! struct Efficient<'a> {
22
//!     #[serde(with = "serde_bytes")]
23
//!     bytes: &'a [u8],
24
//!
25
//!     #[serde(with = "serde_bytes")]
26
//!     byte_buf: Vec<u8>,
27
//!
28
//!     #[serde(with = "serde_bytes")]
29
//!     byte_array: [u8; 314],
30
//! }
31
//! ```
32
33
#![doc(html_root_url = "https://docs.rs/serde_bytes/0.11.17")]
34
#![cfg_attr(not(feature = "std"), no_std)]
35
#![deny(missing_docs)]
36
#![allow(
37
    clippy::elidable_lifetime_names,
38
    clippy::into_iter_without_iter,
39
    clippy::missing_errors_doc,
40
    clippy::must_use_candidate,
41
    clippy::needless_doctest_main,
42
    clippy::needless_lifetimes,
43
    clippy::ptr_as_ptr
44
)]
45
46
mod bytearray;
47
mod bytes;
48
mod de;
49
mod ser;
50
51
#[cfg(any(feature = "std", feature = "alloc"))]
52
mod bytebuf;
53
54
#[cfg(feature = "alloc")]
55
extern crate alloc;
56
57
use serde::{Deserializer, Serializer};
58
59
pub use crate::bytearray::ByteArray;
60
pub use crate::bytes::Bytes;
61
pub use crate::de::Deserialize;
62
pub use crate::ser::Serialize;
63
64
#[cfg(any(feature = "std", feature = "alloc"))]
65
pub use crate::bytebuf::ByteBuf;
66
67
/// Serde `serialize_with` function to serialize bytes efficiently.
68
///
69
/// This function can be used with either of the following Serde attributes:
70
///
71
/// - `#[serde(with = "serde_bytes")]`
72
/// - `#[serde(serialize_with = "serde_bytes::serialize")]`
73
///
74
/// ```
75
/// # use serde_derive::Serialize;
76
/// use serde::Serialize;
77
///
78
/// #[derive(Serialize)]
79
/// struct Efficient<'a> {
80
///     #[serde(with = "serde_bytes")]
81
///     bytes: &'a [u8],
82
///
83
///     #[serde(with = "serde_bytes")]
84
///     byte_buf: Vec<u8>,
85
///
86
///     #[serde(with = "serde_bytes")]
87
///     byte_array: [u8; 314],
88
/// }
89
/// ```
90
0
pub fn serialize<T, S>(bytes: &T, serializer: S) -> Result<S::Ok, S::Error>
91
0
where
92
0
    T: ?Sized + Serialize,
93
0
    S: Serializer,
94
0
{
95
0
    Serialize::serialize(bytes, serializer)
96
0
}
97
98
/// Serde `deserialize_with` function to deserialize bytes efficiently.
99
///
100
/// This function can be used with either of the following Serde attributes:
101
///
102
/// - `#[serde(with = "serde_bytes")]`
103
/// - `#[serde(deserialize_with = "serde_bytes::deserialize")]`
104
///
105
/// ```
106
/// # use serde_derive::Deserialize;
107
/// use serde::Deserialize;
108
///
109
/// #[derive(Deserialize)]
110
/// struct Packet {
111
///     #[serde(with = "serde_bytes")]
112
///     payload: Vec<u8>,
113
///
114
///     #[serde(with = "serde_bytes")]
115
///     byte_array: [u8; 314],
116
/// }
117
/// ```
118
0
pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
119
0
where
120
0
    T: Deserialize<'de>,
121
0
    D: Deserializer<'de>,
122
0
{
123
0
    Deserialize::deserialize(deserializer)
124
0
}