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