serde_bytes/lib.rs
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
46mod bytearray;
47mod bytes;
48mod de;
49mod ser;
50
51#[cfg(any(feature = "std", feature = "alloc"))]
52mod bytebuf;
53
54#[cfg(feature = "alloc")]
55extern crate alloc;
56
57use serde::{Deserializer, Serializer};
58
59pub use crate::bytearray::ByteArray;
60pub use crate::bytes::Bytes;
61pub use crate::de::Deserialize;
62pub use crate::ser::Serialize;
63
64#[cfg(any(feature = "std", feature = "alloc"))]
65pub 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/// ```
90pub fn serialize<T, S>(bytes: &T, serializer: S) -> Result<S::Ok, S::Error>
91where
92 T: ?Sized + Serialize,
93 S: Serializer,
94{
95 Serialize::serialize(bytes, serializer)
96}
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/// ```
118pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
119where
120 T: Deserialize<'de>,
121 D: Deserializer<'de>,
122{
123 Deserialize::deserialize(deserializer)
124}