Skip to main content

smol_bytes/bytes_mut/
cmp.rs

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