1#[cfg(feature = "introspection")]
2use crate::introspection::{ir, Introspectable, LexicalId, References};
3use crate::tags::{self, PrimaryTag, Tag};
4use crate::{Deserialize, DeserializeError, Deserializer, Serialize, SerializeError, Serializer};
5use std::borrow::Borrow;
6use std::ops::Deref;
7
8#[derive(Debug, Clone, PartialEq, Eq)]
10#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
11#[cfg_attr(
12 feature = "serde",
13 derive(serde::Serialize, serde::Deserialize),
14 serde(transparent)
15)]
16pub struct Bytes(pub Vec<u8>);
17
18impl Bytes {
19 pub fn new<T: Into<Vec<u8>>>(bytes: T) -> Self {
20 Self(bytes.into())
21 }
22}
23
24impl Deref for Bytes {
25 type Target = ByteSlice;
26
27 fn deref(&self) -> &ByteSlice {
28 ByteSlice::new(&self.0)
29 }
30}
31
32impl AsRef<ByteSlice> for Bytes {
33 fn as_ref(&self) -> &ByteSlice {
34 self
35 }
36}
37
38impl Borrow<ByteSlice> for Bytes {
39 fn borrow(&self) -> &ByteSlice {
40 self
41 }
42}
43
44impl AsRef<[u8]> for Bytes {
45 fn as_ref(&self) -> &[u8] {
46 self
47 }
48}
49
50impl From<Vec<u8>> for Bytes {
51 fn from(bytes: Vec<u8>) -> Self {
52 Self(bytes)
53 }
54}
55
56impl From<Bytes> for Vec<u8> {
57 fn from(bytes: Bytes) -> Self {
58 bytes.0
59 }
60}
61
62impl PartialEq<ByteSlice> for Bytes {
63 fn eq(&self, other: &ByteSlice) -> bool {
64 **self == *other
65 }
66}
67
68impl PartialEq<[u8]> for Bytes {
69 fn eq(&self, other: &[u8]) -> bool {
70 **self == *other
71 }
72}
73
74impl PartialEq<Bytes> for [u8] {
75 fn eq(&self, other: &Bytes) -> bool {
76 *self == ***other
77 }
78}
79
80impl PrimaryTag for Bytes {
81 type Tag = tags::Bytes;
82}
83
84impl Serialize<tags::Bytes> for Bytes {
85 fn serialize(self, serializer: Serializer) -> Result<(), SerializeError> {
86 serializer.serialize_byte_slice2(&self)
87 }
88}
89
90impl Serialize<tags::Bytes> for &Bytes {
91 fn serialize(self, serializer: Serializer) -> Result<(), SerializeError> {
92 serializer.serialize_byte_slice2(self)
93 }
94}
95
96impl Deserialize<tags::Bytes> for Bytes {
97 fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
98 deserializer.deserialize_bytes_extend_new().map(Self)
99 }
100}
101
102impl<T> Serialize<tags::Vec<T>> for Bytes
103where
104 T: Tag,
105 u8: Serialize<T>,
106{
107 fn serialize(self, serializer: Serializer) -> Result<(), SerializeError> {
108 serializer.serialize_vec2_iter(self.0.iter().copied())
109 }
110}
111
112impl<T> Serialize<tags::Vec<T>> for &Bytes
113where
114 T: Tag,
115 u8: Serialize<T>,
116{
117 fn serialize(self, serializer: Serializer) -> Result<(), SerializeError> {
118 serializer.serialize_vec2_iter(self.0.iter().copied())
119 }
120}
121
122impl<T> Deserialize<tags::Vec<T>> for Bytes
123where
124 T: Tag,
125 u8: Deserialize<T>,
126{
127 fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
128 deserializer
129 .deserialize_vec_extend_new::<T, u8, _>()
130 .map(Self)
131 }
132}
133
134#[cfg(feature = "introspection")]
135impl Introspectable for Bytes {
136 fn layout() -> ir::LayoutIr {
137 ir::BuiltInTypeIr::Bytes.into()
138 }
139
140 fn lexical_id() -> LexicalId {
141 LexicalId::BYTES
142 }
143
144 fn add_references(_references: &mut References) {}
145}
146
147#[derive(Debug, PartialEq, Eq)]
149#[repr(transparent)]
150pub struct ByteSlice(pub [u8]);
151
152impl ByteSlice {
153 pub fn new<T: AsRef<[u8]> + ?Sized>(bytes: &T) -> &Self {
154 let self_ptr = bytes.as_ref() as *const [u8] as *const Self;
155 unsafe { &*self_ptr }
157 }
158}
159
160impl Deref for ByteSlice {
161 type Target = [u8];
162
163 fn deref(&self) -> &[u8] {
164 &self.0
165 }
166}
167
168impl AsRef<[u8]> for ByteSlice {
169 fn as_ref(&self) -> &[u8] {
170 self
171 }
172}
173
174impl AsRef<ByteSlice> for [u8] {
175 fn as_ref(&self) -> &ByteSlice {
176 ByteSlice::new(self)
177 }
178}
179
180impl<'a, T: AsRef<[u8]>> From<&'a T> for &'a ByteSlice {
181 fn from(bytes: &'a T) -> Self {
182 ByteSlice::new(bytes)
183 }
184}
185
186impl ToOwned for ByteSlice {
187 type Owned = Bytes;
188
189 fn to_owned(&self) -> Bytes {
190 Bytes::new(&self.0)
191 }
192}
193
194impl PartialEq<Bytes> for ByteSlice {
195 fn eq(&self, other: &Bytes) -> bool {
196 *self == **other
197 }
198}
199
200impl PartialEq<[u8]> for ByteSlice {
201 fn eq(&self, other: &[u8]) -> bool {
202 **self == *other
203 }
204}
205
206impl PartialEq<ByteSlice> for [u8] {
207 fn eq(&self, other: &ByteSlice) -> bool {
208 *self == **other
209 }
210}
211
212impl PrimaryTag for &ByteSlice {
213 type Tag = tags::Bytes;
214}
215
216impl Serialize<tags::Bytes> for &ByteSlice {
217 fn serialize(self, serializer: Serializer) -> Result<(), SerializeError> {
218 serializer.serialize_byte_slice2(self)
219 }
220}
221
222impl<T> Serialize<tags::Vec<T>> for &ByteSlice
223where
224 T: Tag,
225 u8: Serialize<T>,
226{
227 fn serialize(self, serializer: Serializer) -> Result<(), SerializeError> {
228 serializer.serialize_vec2_iter(self.0.iter().copied())
229 }
230}
231
232#[cfg(feature = "introspection")]
233impl Introspectable for ByteSlice {
234 fn layout() -> ir::LayoutIr {
235 ir::BuiltInTypeIr::Bytes.into()
236 }
237
238 fn lexical_id() -> LexicalId {
239 LexicalId::BYTES
240 }
241
242 fn add_references(_references: &mut References) {}
243}