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