wick_packet/
b64_bytes.rs

1use std::cmp;
2use std::ops::Deref;
3
4use base64_serde::base64_serde_type;
5use bytes::buf::IntoIter;
6base64_serde_type!(Base64Standard, base64::engine::general_purpose::STANDARD);
7
8#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
9#[non_exhaustive]
10pub struct Base64Bytes(#[serde(with = "Base64Standard")] pub bytes::Bytes);
11
12impl Base64Bytes {
13  pub fn new<T>(value: T) -> Self
14  where
15    T: Into<bytes::Bytes>,
16  {
17    Self(value.into())
18  }
19}
20
21impl AsRef<[u8]> for Base64Bytes {
22  fn as_ref(&self) -> &[u8] {
23    self.0.as_ref()
24  }
25}
26
27impl From<bytes::Bytes> for Base64Bytes {
28  fn from(value: bytes::Bytes) -> Self {
29    Self(value)
30  }
31}
32
33impl From<Base64Bytes> for bytes::Bytes {
34  fn from(value: Base64Bytes) -> Self {
35    value.0
36  }
37}
38
39impl Deref for Base64Bytes {
40  type Target = [u8];
41
42  fn deref(&self) -> &Self::Target {
43    &self.0
44  }
45}
46
47impl std::borrow::Borrow<[u8]> for Base64Bytes {
48  fn borrow(&self) -> &[u8] {
49    &self.0
50  }
51}
52
53impl From<Base64Bytes> for Vec<u8> {
54  fn from(bytes: Base64Bytes) -> Vec<u8> {
55    bytes.0.into()
56  }
57}
58impl From<Vec<u8>> for Base64Bytes {
59  fn from(vec: Vec<u8>) -> Base64Bytes {
60    Base64Bytes::new(vec)
61  }
62}
63
64impl Default for Base64Bytes {
65  #[inline]
66  fn default() -> Base64Bytes {
67    Base64Bytes::new(bytes::Bytes::default())
68  }
69}
70
71impl PartialEq for Base64Bytes {
72  fn eq(&self, other: &Base64Bytes) -> bool {
73    self.0 == other.0
74  }
75}
76
77impl PartialOrd for Base64Bytes {
78  fn partial_cmp(&self, other: &Base64Bytes) -> Option<cmp::Ordering> {
79    self.0.partial_cmp(&other.0)
80  }
81}
82
83impl Ord for Base64Bytes {
84  fn cmp(&self, other: &Base64Bytes) -> cmp::Ordering {
85    self.0.cmp(&other.0)
86  }
87}
88
89impl Eq for Base64Bytes {}
90
91impl PartialEq<[u8]> for Base64Bytes {
92  fn eq(&self, other: &[u8]) -> bool {
93    self.0 == other
94  }
95}
96
97impl PartialOrd<[u8]> for Base64Bytes {
98  fn partial_cmp(&self, other: &[u8]) -> Option<cmp::Ordering> {
99    self.0.partial_cmp(other)
100  }
101}
102
103impl PartialEq<Base64Bytes> for [u8] {
104  fn eq(&self, other: &Base64Bytes) -> bool {
105    *other == *self
106  }
107}
108
109impl PartialOrd<Base64Bytes> for [u8] {
110  fn partial_cmp(&self, other: &Base64Bytes) -> Option<cmp::Ordering> {
111    <[u8] as PartialOrd<[u8]>>::partial_cmp(self, other)
112  }
113}
114
115impl PartialEq<str> for Base64Bytes {
116  fn eq(&self, other: &str) -> bool {
117    self.0 == other.as_bytes()
118  }
119}
120
121impl PartialOrd<str> for Base64Bytes {
122  fn partial_cmp(&self, other: &str) -> Option<cmp::Ordering> {
123    self.0.partial_cmp(other.as_bytes())
124  }
125}
126
127impl PartialEq<Base64Bytes> for str {
128  fn eq(&self, other: &Base64Bytes) -> bool {
129    *other == *self
130  }
131}
132
133impl PartialOrd<Base64Bytes> for str {
134  fn partial_cmp(&self, other: &Base64Bytes) -> Option<cmp::Ordering> {
135    <[u8] as PartialOrd<[u8]>>::partial_cmp(self.as_bytes(), other)
136  }
137}
138
139impl PartialEq<Vec<u8>> for Base64Bytes {
140  fn eq(&self, other: &Vec<u8>) -> bool {
141    *self == other[..]
142  }
143}
144
145impl PartialOrd<Vec<u8>> for Base64Bytes {
146  fn partial_cmp(&self, other: &Vec<u8>) -> Option<cmp::Ordering> {
147    self.0.partial_cmp(&**other)
148  }
149}
150
151impl PartialEq<Base64Bytes> for Vec<u8> {
152  fn eq(&self, other: &Base64Bytes) -> bool {
153    *other == *self
154  }
155}
156
157impl PartialOrd<Base64Bytes> for Vec<u8> {
158  fn partial_cmp(&self, other: &Base64Bytes) -> Option<cmp::Ordering> {
159    <[u8] as PartialOrd<[u8]>>::partial_cmp(self, other)
160  }
161}
162
163impl PartialEq<String> for Base64Bytes {
164  fn eq(&self, other: &String) -> bool {
165    *self == other[..]
166  }
167}
168
169impl PartialOrd<String> for Base64Bytes {
170  fn partial_cmp(&self, other: &String) -> Option<cmp::Ordering> {
171    self.0.partial_cmp(other.as_bytes())
172  }
173}
174
175impl PartialEq<Base64Bytes> for String {
176  fn eq(&self, other: &Base64Bytes) -> bool {
177    *other == *self
178  }
179}
180
181impl PartialOrd<Base64Bytes> for String {
182  fn partial_cmp(&self, other: &Base64Bytes) -> Option<cmp::Ordering> {
183    <[u8] as PartialOrd<[u8]>>::partial_cmp(self.as_bytes(), other)
184  }
185}
186
187impl PartialEq<Base64Bytes> for &[u8] {
188  fn eq(&self, other: &Base64Bytes) -> bool {
189    other.0 == *self
190  }
191}
192
193impl PartialEq<&[u8]> for Base64Bytes {
194  fn eq(&self, other: &&[u8]) -> bool {
195    *other == self.0
196  }
197}
198
199impl PartialOrd<Base64Bytes> for &[u8] {
200  fn partial_cmp(&self, other: &Base64Bytes) -> Option<cmp::Ordering> {
201    <[u8] as PartialOrd<[u8]>>::partial_cmp(self, other)
202  }
203}
204
205impl PartialEq<Base64Bytes> for &str {
206  fn eq(&self, other: &Base64Bytes) -> bool {
207    other.0 == *self
208  }
209}
210
211impl PartialOrd<Base64Bytes> for &str {
212  fn partial_cmp(&self, other: &Base64Bytes) -> Option<cmp::Ordering> {
213    <[u8] as PartialOrd<[u8]>>::partial_cmp(self.as_bytes(), other)
214  }
215}
216
217impl IntoIterator for Base64Bytes {
218  type Item = u8;
219  type IntoIter = IntoIter<Base64Bytes>;
220
221  fn into_iter(self) -> Self::IntoIter {
222    IntoIter::new(self)
223  }
224}
225
226impl<'a> IntoIterator for &'a Base64Bytes {
227  type Item = &'a u8;
228  type IntoIter = core::slice::Iter<'a, u8>;
229
230  fn into_iter(self) -> Self::IntoIter {
231    self.0.iter()
232  }
233}
234
235impl FromIterator<u8> for Base64Bytes {
236  fn from_iter<T: IntoIterator<Item = u8>>(into_iter: T) -> Self {
237    Vec::from_iter(into_iter).into()
238  }
239}
240
241impl bytes::Buf for Base64Bytes {
242  fn remaining(&self) -> usize {
243    self.0.remaining()
244  }
245
246  fn chunk(&self) -> &[u8] {
247    self.0.chunk()
248  }
249
250  fn advance(&mut self, cnt: usize) {
251    self.0.advance(cnt);
252  }
253}
254
255impl From<String> for Base64Bytes {
256  fn from(value: String) -> Self {
257    Self::new(value)
258  }
259}
260
261impl Extend<Base64Bytes> for bytes::BytesMut {
262  fn extend<T: IntoIterator<Item = Base64Bytes>>(&mut self, iter: T) {
263    for bytes in iter {
264      self.extend(bytes);
265    }
266  }
267}