serde_bytes_ng/
lib.rs

1//! Wrapper types to enable optimized handling of `[u8; N]`, `&[u8]` and `Vec<u8>`.
2//!
3//! This crate is a fork of [`serde_bytes`](https://crates.io/crates/serde_bytes) that adds
4//! support for `[u8; N]` and `&[u8; N]`.
5//!
6//! ```
7//! # use serde_derive::{Deserialize, Serialize};
8//!#[derive(Deserialize, Serialize)]
9//! struct Efficient<'a> {
10//!     #[serde(with = "serde_bytes_ng")]
11//!     bytes: &'a [u8],
12//!
13//!     #[serde(with = "serde_bytes_ng")]
14//!     byte_buf: Vec<u8>,
15//!
16//!     #[serde(with = "serde_bytes_ng")]
17//!     byte_array: [u8; 188],
18//!
19//!     #[serde(with = "serde_bytes_ng", borrow)]
20//!     byte_array_ref: &'a [u8; 188],
21//!}
22//! ```
23
24#![doc(html_root_url = "https://docs.rs/serde_bytes_ng/0.11.12")]
25#![cfg_attr(not(feature = "std"), no_std)]
26#![deny(missing_docs)]
27#![allow(
28    clippy::into_iter_without_iter,
29    clippy::missing_errors_doc,
30    clippy::must_use_candidate,
31    clippy::needless_doctest_main
32)]
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_ng")]`
61/// - `#[serde(serialize_with = "serde_bytes_ng::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_ng")]
70///     bytes: &'a [u8],
71///
72///     #[serde(with = "serde_bytes_ng")]
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_ng")]`
89/// - `#[serde(deserialize_with = "serde_bytes_ng::deserialize")]`
90///
91/// ```
92/// # use serde_derive::Deserialize;
93/// use serde::Deserialize;
94///
95/// #[derive(Deserialize)]
96/// struct Packet {
97///     #[serde(with = "serde_bytes_ng")]
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}