Skip to main content

smol_bytes/buffer/
cmp.rs

1use core::cmp::Ordering;
2
3use super::*;
4
5macro_rules! bail {
6  ($($ty:ty => $method:ident($other:ty)),+$(,)?) => {
7    $(
8      impl ::core::cmp::PartialEq<$ty> for Buffer {
9        #[cfg_attr(not(coverage), inline(always))]
10        fn eq(&self, other: &$ty) -> ::core::primitive::bool {
11          <Self as ::core::cmp::PartialEq<$other>>::eq(self, other.$method())
12        }
13      }
14
15      impl ::core::cmp::PartialEq<Buffer> for $ty {
16        #[cfg_attr(not(coverage), inline(always))]
17        fn eq(&self, other: &Buffer) -> ::core::primitive::bool {
18          <Buffer as ::core::cmp::PartialEq<$ty>>::eq(other, self)
19        }
20      }
21
22      impl ::core::cmp::PartialOrd<$ty> for Buffer {
23        #[cfg_attr(not(coverage), inline(always))]
24        fn partial_cmp(&self, other: &$ty) -> ::core::option::Option<::core::cmp::Ordering> {
25          <Self as ::core::cmp::PartialOrd<$other>>::partial_cmp(self, other.$method())
26        }
27      }
28
29      impl ::core::cmp::PartialOrd<Buffer> for $ty {
30        #[cfg_attr(not(coverage), inline(always))]
31        fn partial_cmp(&self, other: &Buffer) -> ::core::option::Option<::core::cmp::Ordering> {
32          <$other as ::core::cmp::PartialOrd<Buffer>>::partial_cmp(self.$method(), other)
33        }
34      }
35    )*
36  };
37}
38
39impl PartialEq for Buffer {
40  #[cfg_attr(not(coverage), inline(always))]
41  fn eq(&self, other: &Self) -> bool {
42    self.as_ref() == other.as_ref()
43  }
44}
45
46impl Eq for Buffer {}
47
48impl PartialOrd for Buffer {
49  #[cfg_attr(not(coverage), inline(always))]
50  fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
51    Some(self.cmp(other))
52  }
53}
54
55impl Ord for Buffer {
56  #[cfg_attr(not(coverage), inline(always))]
57  fn cmp(&self, other: &Self) -> Ordering {
58    self.as_ref().cmp(other.as_ref())
59  }
60}
61
62impl core::hash::Hash for Buffer {
63  #[cfg_attr(not(coverage), inline(always))]
64  fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
65    self.as_ref().hash(state);
66  }
67}
68
69impl<'a, T: ?Sized> PartialEq<&'a T> for Buffer
70where
71  Self: PartialEq<T>,
72{
73  #[cfg_attr(not(coverage), inline(always))]
74  fn eq(&self, other: &&'a T) -> bool {
75    self.eq(other)
76  }
77}
78
79impl<'a, T: ?Sized> PartialOrd<&'a T> for Buffer
80where
81  Self: PartialOrd<T>,
82{
83  #[cfg_attr(not(coverage), inline(always))]
84  fn partial_cmp(&self, other: &&'a T) -> Option<Ordering> {
85    self.partial_cmp(other)
86  }
87}
88
89// ---- [u8] comparisons ----
90impl PartialEq<[u8]> for Buffer {
91  #[cfg_attr(not(coverage), inline(always))]
92  fn eq(&self, other: &[u8]) -> bool {
93    self.as_ref() == other
94  }
95}
96
97impl PartialEq<Buffer> for [u8] {
98  #[cfg_attr(not(coverage), inline(always))]
99  fn eq(&self, other: &Buffer) -> bool {
100    self == other.as_ref()
101  }
102}
103
104impl PartialOrd<[u8]> for Buffer {
105  #[cfg_attr(not(coverage), inline(always))]
106  fn partial_cmp(&self, other: &[u8]) -> Option<Ordering> {
107    Some(self.as_ref().cmp(other))
108  }
109}
110
111impl PartialOrd<Buffer> for [u8] {
112  #[cfg_attr(not(coverage), inline(always))]
113  fn partial_cmp(&self, other: &Buffer) -> Option<Ordering> {
114    Some(self.cmp(other.as_ref()))
115  }
116}
117
118impl<const N: usize> PartialEq<[u8; N]> for Buffer {
119  #[cfg_attr(not(coverage), inline(always))]
120  fn eq(&self, other: &[u8; N]) -> bool {
121    N == self.len() && self.as_ref() == other.as_slice()
122  }
123}
124
125impl<const N: usize> PartialEq<Buffer> for [u8; N] {
126  #[cfg_attr(not(coverage), inline(always))]
127  fn eq(&self, other: &Buffer) -> bool {
128    other.eq(self)
129  }
130}
131
132impl<const N: usize> PartialOrd<[u8; N]> for Buffer {
133  #[cfg_attr(not(coverage), inline(always))]
134  fn partial_cmp(&self, other: &[u8; N]) -> Option<Ordering> {
135    Some(self.as_ref().cmp(other.as_slice()))
136  }
137}
138
139impl<const N: usize> PartialOrd<Buffer> for [u8; N] {
140  #[cfg_attr(not(coverage), inline(always))]
141  fn partial_cmp(&self, other: &Buffer) -> Option<Ordering> {
142    Some(self.as_slice().cmp(other.as_ref()))
143  }
144}
145
146// ---- &[u8] comparisons ----
147impl PartialEq<Buffer> for &[u8] {
148  #[cfg_attr(not(coverage), inline(always))]
149  fn eq(&self, other: &Buffer) -> bool {
150    <[u8] as PartialEq<Buffer>>::eq(self, other)
151  }
152}
153
154impl PartialOrd<Buffer> for &[u8] {
155  #[cfg_attr(not(coverage), inline(always))]
156  fn partial_cmp(&self, other: &Buffer) -> Option<Ordering> {
157    <[u8] as PartialOrd<Buffer>>::partial_cmp(self, other)
158  }
159}
160
161// ---- &str comparisons ----
162impl PartialEq<Buffer> for &str {
163  #[cfg_attr(not(coverage), inline(always))]
164  fn eq(&self, other: &Buffer) -> bool {
165    <str as PartialEq<Buffer>>::eq(self, other)
166  }
167}
168
169impl PartialOrd<Buffer> for &str {
170  #[cfg_attr(not(coverage), inline(always))]
171  fn partial_cmp(&self, other: &Buffer) -> Option<Ordering> {
172    <str as PartialOrd<Buffer>>::partial_cmp(self, other)
173  }
174}
175
176bail!(
177  str => as_bytes([u8]),
178);
179
180#[cfg(any(feature = "alloc", feature = "std"))]
181const _: () = {
182  use std::{boxed::Box, rc::Rc, string::String, sync::Arc, vec::Vec};
183
184  use crate::{bytes::RawBytes, strategy::ImmutableStorage};
185
186  bail!(
187    ::bytes::Bytes => as_ref([u8]),
188    ::bytes::BytesMut => as_ref([u8]),
189    String => as_str(str),
190    Vec<u8> => as_slice([u8]),
191    Box<[u8]> => as_ref([u8]),
192    Rc<[u8]> => as_ref([u8]),
193    Arc<[u8]> => as_ref([u8]),
194    Box<str> => as_ref(str),
195    Rc<str> => as_ref(str),
196    Arc<str> => as_ref(str),
197  );
198
199  // --- RawBytes comparisons ----
200  impl<S> PartialEq<RawBytes<S>> for Buffer
201  where
202    RawBytes<S>: ImmutableStorage,
203  {
204    #[cfg_attr(not(coverage), inline(always))]
205    fn eq(&self, other: &RawBytes<S>) -> bool {
206      self.as_ref() == other.as_ref()
207    }
208  }
209
210  impl<S> PartialEq<Buffer> for RawBytes<S>
211  where
212    RawBytes<S>: ImmutableStorage,
213  {
214    #[cfg_attr(not(coverage), inline(always))]
215    fn eq(&self, other: &Buffer) -> bool {
216      other.eq(self)
217    }
218  }
219
220  impl<S> PartialOrd<RawBytes<S>> for Buffer
221  where
222    RawBytes<S>: ImmutableStorage,
223  {
224    #[cfg_attr(not(coverage), inline(always))]
225    fn partial_cmp(&self, other: &RawBytes<S>) -> Option<Ordering> {
226      Some(self.as_ref().cmp(other.as_ref()))
227    }
228  }
229
230  impl<S> PartialOrd<Buffer> for RawBytes<S>
231  where
232    RawBytes<S>: ImmutableStorage,
233  {
234    #[cfg_attr(not(coverage), inline(always))]
235    fn partial_cmp(&self, other: &Buffer) -> Option<Ordering> {
236      Some(self.as_ref().cmp(other.as_ref()))
237    }
238  }
239};