serde_bytes/
bytes.rs

1use core::cmp::Ordering;
2use core::fmt::{self, Debug};
3use core::hash::{Hash, Hasher};
4use core::ops::{Deref, DerefMut};
5
6#[cfg(feature = "alloc")]
7use alloc::borrow::ToOwned;
8
9#[cfg(feature = "alloc")]
10use alloc::boxed::Box;
11
12#[cfg(any(feature = "std", feature = "alloc"))]
13use crate::ByteBuf;
14
15use serde::de::{Deserialize, Deserializer};
16use serde::ser::{Serialize, Serializer};
17
18/// Wrapper around `[u8]` to serialize and deserialize efficiently.
19///
20/// ```
21/// use std::collections::HashMap;
22/// use std::io;
23///
24/// use serde_bytes::Bytes;
25///
26/// fn print_encoded_cache() -> Result<(), bincode::error::EncodeError> {
27///     let mut cache = HashMap::new();
28///     cache.insert(3, Bytes::new(b"three"));
29///     cache.insert(2, Bytes::new(b"two"));
30///     cache.insert(1, Bytes::new(b"one"));
31///
32///     bincode::serde::encode_into_std_write(
33///         &cache,
34///         &mut io::stdout(),
35///         bincode::config::standard(),
36///     )?;
37///
38///     Ok(())
39/// }
40/// #
41/// # fn main() {
42/// #     print_encoded_cache().unwrap();
43/// # }
44/// ```
45#[derive(Eq, Ord)]
46#[repr(transparent)]
47pub struct Bytes {
48    bytes: [u8],
49}
50
51impl Bytes {
52    /// Wrap an existing `&[u8]`.
53    pub fn new(bytes: &[u8]) -> &Self {
54        unsafe { &*(bytes as *const [u8] as *const Bytes) }
55    }
56}
57
58impl Debug for Bytes {
59    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
60        Debug::fmt(&self.bytes, f)
61    }
62}
63
64impl AsRef<[u8]> for Bytes {
65    fn as_ref(&self) -> &[u8] {
66        &self.bytes
67    }
68}
69
70impl AsMut<[u8]> for Bytes {
71    fn as_mut(&mut self) -> &mut [u8] {
72        &mut self.bytes
73    }
74}
75
76impl Deref for Bytes {
77    type Target = [u8];
78
79    fn deref(&self) -> &Self::Target {
80        &self.bytes
81    }
82}
83
84impl DerefMut for Bytes {
85    fn deref_mut(&mut self) -> &mut Self::Target {
86        &mut self.bytes
87    }
88}
89
90impl<'a> From<&'a [u8]> for &'a Bytes {
91    fn from(bytes: &'a [u8]) -> Self {
92        Bytes::new(bytes)
93    }
94}
95
96#[cfg(any(feature = "std", feature = "alloc"))]
97impl ToOwned for Bytes {
98    type Owned = ByteBuf;
99
100    fn to_owned(&self) -> Self::Owned {
101        ByteBuf::from(&self.bytes)
102    }
103}
104
105#[cfg(any(feature = "std", feature = "alloc"))]
106impl From<Box<[u8]>> for Box<Bytes> {
107    fn from(bytes: Box<[u8]>) -> Self {
108        unsafe { Box::from_raw(Box::into_raw(bytes) as *mut Bytes) }
109    }
110}
111
112impl Default for &Bytes {
113    fn default() -> Self {
114        Bytes::new(&[])
115    }
116}
117
118#[cfg(any(feature = "std", feature = "alloc"))]
119impl Default for Box<Bytes> {
120    fn default() -> Self {
121        ByteBuf::new().into_boxed_bytes()
122    }
123}
124
125impl<Rhs> PartialEq<Rhs> for Bytes
126where
127    Rhs: ?Sized + AsRef<[u8]>,
128{
129    fn eq(&self, other: &Rhs) -> bool {
130        self.as_ref().eq(other.as_ref())
131    }
132}
133
134impl<Rhs> PartialOrd<Rhs> for Bytes
135where
136    Rhs: ?Sized + AsRef<[u8]>,
137{
138    fn partial_cmp(&self, other: &Rhs) -> Option<Ordering> {
139        self.as_ref().partial_cmp(other.as_ref())
140    }
141}
142
143impl Hash for Bytes {
144    fn hash<H: Hasher>(&self, state: &mut H) {
145        self.bytes.hash(state);
146    }
147}
148
149impl<'a> IntoIterator for &'a Bytes {
150    type Item = &'a u8;
151    type IntoIter = <&'a [u8] as IntoIterator>::IntoIter;
152
153    fn into_iter(self) -> Self::IntoIter {
154        self.bytes.iter()
155    }
156}
157
158impl<'a> IntoIterator for &'a mut Bytes {
159    type Item = &'a mut u8;
160    type IntoIter = <&'a mut [u8] as IntoIterator>::IntoIter;
161
162    fn into_iter(self) -> Self::IntoIter {
163        self.bytes.iter_mut()
164    }
165}
166
167impl Serialize for Bytes {
168    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
169    where
170        S: Serializer,
171    {
172        serializer.serialize_bytes(&self.bytes)
173    }
174}
175
176impl<'a, 'de: 'a> Deserialize<'de> for &'a Bytes {
177    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
178    where
179        D: Deserializer<'de>,
180    {
181        // serde::Deserialize for &[u8] is already optimized, so simply forward to that.
182        Deserialize::deserialize(deserializer).map(Bytes::new)
183    }
184}