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.15")]
34#![cfg_attr(not(feature = "std"), no_std)]
35#![deny(missing_docs)]
36#![allow(
37    clippy::into_iter_without_iter,
38    clippy::missing_errors_doc,
39    clippy::must_use_candidate,
40    clippy::needless_doctest_main,
41    clippy::ptr_as_ptr
42)]
43
44mod bytearray;
45mod bytes;
46mod de;
47mod ser;
48
49#[cfg(any(feature = "std", feature = "alloc"))]
50mod bytebuf;
51
52#[cfg(feature = "alloc")]
53extern crate alloc;
54
55use serde::{Deserializer, Serializer};
56
57pub use crate::bytearray::ByteArray;
58pub use crate::bytes::Bytes;
59pub use crate::de::Deserialize;
60pub use crate::ser::Serialize;
61
62#[cfg(any(feature = "std", feature = "alloc"))]
63pub use crate::bytebuf::ByteBuf;
64
65/// Serde `serialize_with` function to serialize bytes efficiently.
66///
67/// This function can be used with either of the following Serde attributes:
68///
69/// - `#[serde(with = "serde_bytes")]`
70/// - `#[serde(serialize_with = "serde_bytes::serialize")]`
71///
72/// ```
73/// # use serde_derive::Serialize;
74/// use serde::Serialize;
75///
76/// #[derive(Serialize)]
77/// struct Efficient<'a> {
78///     #[serde(with = "serde_bytes")]
79///     bytes: &'a [u8],
80///
81///     #[serde(with = "serde_bytes")]
82///     byte_buf: Vec<u8>,
83///
84///     #[serde(with = "serde_bytes")]
85///     byte_array: [u8; 314],
86/// }
87/// ```
88pub fn serialize<T, S>(bytes: &T, serializer: S) -> Result<S::Ok, S::Error>
89where
90    T: ?Sized + Serialize,
91    S: Serializer,
92{
93    Serialize::serialize(bytes, serializer)
94}
95
96/// Serde `deserialize_with` function to deserialize bytes efficiently.
97///
98/// This function can be used with either of the following Serde attributes:
99///
100/// - `#[serde(with = "serde_bytes")]`
101/// - `#[serde(deserialize_with = "serde_bytes::deserialize")]`
102///
103/// ```
104/// # use serde_derive::Deserialize;
105/// use serde::Deserialize;
106///
107/// #[derive(Deserialize)]
108/// struct Packet {
109///     #[serde(with = "serde_bytes")]
110///     payload: Vec<u8>,
111///
112///     #[serde(with = "serde_bytes")]
113///     byte_array: [u8; 314],
114/// }
115/// ```
116pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
117where
118    T: Deserialize<'de>,
119    D: Deserializer<'de>,
120{
121    Deserialize::deserialize(deserializer)
122}