serde_human_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_human_bytes::Bytes` and `Vec<u8>` by wrapping it
10//! in `serde_human_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_human_bytes")]
23//!     bytes: &'a [u8],
24//!
25//!     #[serde(with = "serde_human_bytes")]
26//!     byte_buf: Vec<u8>,
27//!
28//!     #[serde(with = "serde_human_bytes")]
29//!     byte_array: [u8; 314],
30//! }
31//! ```
32
33#![doc(html_root_url = "https://docs.rs/serde_human_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::needless_lifetimes,
42    clippy::ptr_as_ptr
43)]
44
45mod bytearray;
46mod bytes;
47mod de;
48mod ser;
49
50mod bytebuf;
51
52extern crate alloc;
53
54use serde::{Deserializer, Serializer};
55
56pub use crate::bytearray::ByteArray;
57pub use crate::bytes::Bytes;
58pub use crate::de::Deserialize;
59pub use crate::ser::Serialize;
60
61pub use crate::bytebuf::ByteBuf;
62
63/// Serde `serialize_with` function to serialize bytes efficiently.
64///
65/// This function can be used with either of the following Serde attributes:
66///
67/// - `#[serde(with = "serde_human_bytes")]`
68/// - `#[serde(serialize_with = "serde_human_bytes::serialize")]`
69///
70/// ```
71/// # use serde_derive::Serialize;
72/// use serde::Serialize;
73///
74/// #[derive(Serialize)]
75/// struct Efficient<'a> {
76///     #[serde(with = "serde_human_bytes")]
77///     bytes: &'a [u8],
78///
79///     #[serde(with = "serde_human_bytes")]
80///     byte_buf: Vec<u8>,
81///
82///     #[serde(with = "serde_human_bytes")]
83///     byte_array: [u8; 314],
84/// }
85/// ```
86pub fn serialize<T, S>(bytes: &T, serializer: S) -> Result<S::Ok, S::Error>
87where
88    T: ?Sized + Serialize,
89    S: Serializer,
90{
91    Serialize::serialize(bytes, serializer)
92}
93
94/// Serde `deserialize_with` function to deserialize bytes efficiently.
95///
96/// This function can be used with either of the following Serde attributes:
97///
98/// - `#[serde(with = "serde_human_bytes")]`
99/// - `#[serde(deserialize_with = "serde_human_bytes::deserialize")]`
100///
101/// ```
102/// # use serde_derive::Deserialize;
103/// use serde::Deserialize;
104///
105/// #[derive(Deserialize)]
106/// struct Packet {
107///     #[serde(with = "serde_human_bytes")]
108///     payload: Vec<u8>,
109///
110///     #[serde(with = "serde_human_bytes")]
111///     byte_array: [u8; 314],
112/// }
113/// ```
114pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
115where
116    T: Deserialize<'de>,
117    D: Deserializer<'de>,
118{
119    Deserialize::deserialize(deserializer)
120}