Skip to main content

non_empty_slice/
cmp.rs

1use core::cmp::Ordering;
2
3use crate::slice::NonEmptySlice;
4
5// NonEmptySlice <-> NonEmptySlice
6
7impl<T: PartialEq<U>, U> PartialEq<NonEmptySlice<U>> for NonEmptySlice<T> {
8    fn eq(&self, other: &NonEmptySlice<U>) -> bool {
9        self.as_slice() == other.as_slice()
10    }
11}
12
13impl<T: Eq> Eq for NonEmptySlice<T> {}
14
15impl<T: PartialOrd> PartialOrd for NonEmptySlice<T> {
16    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
17        self.as_slice().partial_cmp(other.as_slice())
18    }
19}
20
21impl<T: Ord> Ord for NonEmptySlice<T> {
22    fn cmp(&self, other: &Self) -> Ordering {
23        self.as_slice().cmp(other.as_slice())
24    }
25}
26
27// NonEmptySlice <-> Slice
28
29impl<T: PartialEq<U>, U> PartialEq<[U]> for NonEmptySlice<T> {
30    fn eq(&self, other: &[U]) -> bool {
31        self.as_slice() == other
32    }
33}
34
35impl<T: PartialOrd> PartialOrd<[T]> for NonEmptySlice<T> {
36    fn partial_cmp(&self, other: &[T]) -> Option<Ordering> {
37        self.as_slice().partial_cmp(other)
38    }
39}
40
41impl<T: PartialEq<U>, U> PartialEq<NonEmptySlice<U>> for [T] {
42    fn eq(&self, other: &NonEmptySlice<U>) -> bool {
43        self == other.as_slice()
44    }
45}
46
47impl<T: PartialOrd> PartialOrd<NonEmptySlice<T>> for [T] {
48    fn partial_cmp(&self, other: &NonEmptySlice<T>) -> Option<Ordering> {
49        self.partial_cmp(other.as_slice())
50    }
51}
52
53#[cfg(any(feature = "std", feature = "alloc"))]
54mod std_or_alloc {
55    cfg_select! {
56        feature = "std" => {
57            use std::borrow::Cow;
58        }
59        feature = "alloc" => {
60            use alloc::{borrow::Cow, boxed::Box, vec::Vec};
61        }
62        _ => {
63            compile_error!("expected either `std` or `alloc` to be enabled");
64        }
65    }
66
67    use core::cmp::Ordering;
68
69    use crate::{
70        boxed::NonEmptyBoxedSlice, cow::NonEmptyCowSlice, slice::NonEmptySlice, vec::NonEmptyVec,
71    };
72
73    // NonEmptySlice <-> BoxedSlice
74
75    impl<T: PartialEq<U>, U> PartialEq<Box<[U]>> for NonEmptySlice<T> {
76        fn eq(&self, other: &Box<[U]>) -> bool {
77            self == other.as_ref()
78        }
79    }
80
81    impl<T: PartialOrd> PartialOrd<Box<[T]>> for NonEmptySlice<T> {
82        fn partial_cmp(&self, other: &Box<[T]>) -> Option<Ordering> {
83            self.partial_cmp(other.as_ref())
84        }
85    }
86
87    impl<T: PartialEq<U>, U> PartialEq<NonEmptySlice<U>> for Box<[T]> {
88        fn eq(&self, other: &NonEmptySlice<U>) -> bool {
89            self.as_ref() == other
90        }
91    }
92
93    impl<T: PartialOrd> PartialOrd<NonEmptySlice<T>> for Box<[T]> {
94        fn partial_cmp(&self, other: &NonEmptySlice<T>) -> Option<Ordering> {
95            self.as_ref().partial_cmp(other)
96        }
97    }
98
99    // NonEmptySlice <-> CowSlice
100
101    impl<T: PartialEq<U>, U: Clone> PartialEq<Cow<'_, [U]>> for NonEmptySlice<T> {
102        fn eq(&self, other: &Cow<'_, [U]>) -> bool {
103            self == other.as_ref()
104        }
105    }
106
107    impl<T: PartialOrd + Clone> PartialOrd<Cow<'_, [T]>> for NonEmptySlice<T> {
108        fn partial_cmp(&self, other: &Cow<'_, [T]>) -> Option<Ordering> {
109            self.partial_cmp(other.as_ref())
110        }
111    }
112
113    impl<T: PartialEq<U> + Clone, U> PartialEq<NonEmptySlice<U>> for Cow<'_, [T]> {
114        fn eq(&self, other: &NonEmptySlice<U>) -> bool {
115            self.as_ref() == other
116        }
117    }
118
119    impl<T: PartialOrd + Clone> PartialOrd<NonEmptySlice<T>> for Cow<'_, [T]> {
120        fn partial_cmp(&self, other: &NonEmptySlice<T>) -> Option<Ordering> {
121            self.as_ref().partial_cmp(other)
122        }
123    }
124
125    // NonEmptySlice <-> Vec
126
127    impl<T: PartialEq<U>, U> PartialEq<Vec<U>> for NonEmptySlice<T> {
128        fn eq(&self, other: &Vec<U>) -> bool {
129            self == other.as_slice()
130        }
131    }
132
133    impl<T: PartialOrd> PartialOrd<Vec<T>> for NonEmptySlice<T> {
134        fn partial_cmp(&self, other: &Vec<T>) -> Option<Ordering> {
135            self.partial_cmp(other.as_slice())
136        }
137    }
138
139    impl<T: PartialEq<U>, U> PartialEq<NonEmptySlice<U>> for Vec<T> {
140        fn eq(&self, other: &NonEmptySlice<U>) -> bool {
141            self.as_slice() == other
142        }
143    }
144
145    impl<T: PartialOrd> PartialOrd<NonEmptySlice<T>> for Vec<T> {
146        fn partial_cmp(&self, other: &NonEmptySlice<T>) -> Option<Ordering> {
147            self.as_slice().partial_cmp(other)
148        }
149    }
150
151    // NonEmptySlice <-> NonEmptyCowSlice
152
153    impl<T: PartialEq<U>, U: Clone> PartialEq<NonEmptyCowSlice<'_, U>> for NonEmptySlice<T> {
154        fn eq(&self, other: &NonEmptyCowSlice<'_, U>) -> bool {
155            self == other.as_ref()
156        }
157    }
158
159    impl<T: PartialOrd + Clone> PartialOrd<NonEmptyCowSlice<'_, T>> for NonEmptySlice<T> {
160        fn partial_cmp(&self, other: &NonEmptyCowSlice<'_, T>) -> Option<Ordering> {
161            self.partial_cmp(other.as_ref())
162        }
163    }
164
165    impl<T: PartialEq<U> + Clone, U> PartialEq<NonEmptySlice<U>> for NonEmptyCowSlice<'_, T> {
166        fn eq(&self, other: &NonEmptySlice<U>) -> bool {
167            self.as_ref() == other
168        }
169    }
170
171    impl<T: PartialOrd + Clone> PartialOrd<NonEmptySlice<T>> for NonEmptyCowSlice<'_, T> {
172        fn partial_cmp(&self, other: &NonEmptySlice<T>) -> Option<Ordering> {
173            self.as_ref().partial_cmp(other)
174        }
175    }
176
177    // NonEmptyVec <-> NonEmptyVec
178
179    impl<T: PartialEq<U>, U> PartialEq<NonEmptyVec<U>> for NonEmptyVec<T> {
180        fn eq(&self, other: &NonEmptyVec<U>) -> bool {
181            self == other.as_vec()
182        }
183    }
184
185    impl<T: Eq> Eq for NonEmptyVec<T> {}
186
187    impl<T: PartialOrd> PartialOrd for NonEmptyVec<T> {
188        fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
189            self.partial_cmp(other.as_vec())
190        }
191    }
192
193    impl<T: Ord> Ord for NonEmptyVec<T> {
194        fn cmp(&self, other: &Self) -> Ordering {
195            self.as_vec().cmp(other.as_vec())
196        }
197    }
198
199    // NonEmptyVec <-> Vec
200
201    impl<T: PartialEq<U>, U> PartialEq<Vec<U>> for NonEmptyVec<T> {
202        fn eq(&self, other: &Vec<U>) -> bool {
203            self.as_vec() == other
204        }
205    }
206
207    impl<T: PartialOrd> PartialOrd<Vec<T>> for NonEmptyVec<T> {
208        fn partial_cmp(&self, other: &Vec<T>) -> Option<Ordering> {
209            self.as_vec().partial_cmp(other)
210        }
211    }
212
213    impl<T: PartialEq<U>, U> PartialEq<NonEmptyVec<U>> for Vec<T> {
214        fn eq(&self, other: &NonEmptyVec<U>) -> bool {
215            self == other.as_vec()
216        }
217    }
218
219    impl<T: PartialOrd> PartialOrd<NonEmptyVec<T>> for Vec<T> {
220        fn partial_cmp(&self, other: &NonEmptyVec<T>) -> Option<Ordering> {
221            self.partial_cmp(other.as_vec())
222        }
223    }
224
225    // NonEmptyVec <-> NonEmptySlice
226
227    impl<T: PartialEq<U>, U> PartialEq<NonEmptySlice<U>> for NonEmptyVec<T> {
228        fn eq(&self, other: &NonEmptySlice<U>) -> bool {
229            self.as_non_empty_slice() == other
230        }
231    }
232
233    impl<T: PartialOrd> PartialOrd<NonEmptySlice<T>> for NonEmptyVec<T> {
234        fn partial_cmp(&self, other: &NonEmptySlice<T>) -> Option<Ordering> {
235            self.as_non_empty_slice().partial_cmp(other)
236        }
237    }
238
239    impl<T: PartialEq<U>, U> PartialEq<NonEmptyVec<U>> for NonEmptySlice<T> {
240        fn eq(&self, other: &NonEmptyVec<U>) -> bool {
241            self == other.as_non_empty_slice()
242        }
243    }
244
245    impl<T: PartialOrd> PartialOrd<NonEmptyVec<T>> for NonEmptySlice<T> {
246        fn partial_cmp(&self, other: &NonEmptyVec<T>) -> Option<Ordering> {
247            self.partial_cmp(other.as_non_empty_slice())
248        }
249    }
250
251    // NonEmptyVec <-> Slice
252
253    impl<T: PartialEq<U>, U> PartialEq<[U]> for NonEmptyVec<T> {
254        fn eq(&self, other: &[U]) -> bool {
255            self.as_slice() == other
256        }
257    }
258
259    impl<T: PartialOrd> PartialOrd<[T]> for NonEmptyVec<T> {
260        fn partial_cmp(&self, other: &[T]) -> Option<Ordering> {
261            self.as_slice().partial_cmp(other)
262        }
263    }
264
265    impl<T: PartialEq<U>, U> PartialEq<NonEmptyVec<U>> for [T] {
266        fn eq(&self, other: &NonEmptyVec<U>) -> bool {
267            self == other.as_slice()
268        }
269    }
270
271    impl<T: PartialOrd> PartialOrd<NonEmptyVec<T>> for [T] {
272        fn partial_cmp(&self, other: &NonEmptyVec<T>) -> Option<Ordering> {
273            self.partial_cmp(other.as_slice())
274        }
275    }
276
277    // NonEmptyVec <-> NonEmptyCowSlice
278
279    impl<T: PartialEq<U>, U: Clone> PartialEq<NonEmptyCowSlice<'_, U>> for NonEmptyVec<T> {
280        fn eq(&self, other: &NonEmptyCowSlice<'_, U>) -> bool {
281            self == other.as_ref()
282        }
283    }
284
285    impl<T: PartialOrd + Clone> PartialOrd<NonEmptyCowSlice<'_, T>> for NonEmptyVec<T> {
286        fn partial_cmp(&self, other: &NonEmptyCowSlice<'_, T>) -> Option<Ordering> {
287            self.partial_cmp(other.as_ref())
288        }
289    }
290
291    impl<T: PartialEq<U> + Clone, U> PartialEq<NonEmptyVec<U>> for NonEmptyCowSlice<'_, T> {
292        fn eq(&self, other: &NonEmptyVec<U>) -> bool {
293            self.as_ref() == other
294        }
295    }
296
297    impl<T: PartialOrd + Clone> PartialOrd<NonEmptyVec<T>> for NonEmptyCowSlice<'_, T> {
298        fn partial_cmp(&self, other: &NonEmptyVec<T>) -> Option<Ordering> {
299            self.as_ref().partial_cmp(other)
300        }
301    }
302
303    // NonEmptyVec <-> CowSlice
304
305    impl<T: PartialEq<U>, U: Clone> PartialEq<Cow<'_, [U]>> for NonEmptyVec<T> {
306        fn eq(&self, other: &Cow<'_, [U]>) -> bool {
307            self == other.as_ref()
308        }
309    }
310
311    impl<T: PartialOrd + Clone> PartialOrd<Cow<'_, [T]>> for NonEmptyVec<T> {
312        fn partial_cmp(&self, other: &Cow<'_, [T]>) -> Option<Ordering> {
313            self.partial_cmp(other.as_ref())
314        }
315    }
316
317    impl<T: PartialEq<U> + Clone, U> PartialEq<NonEmptyVec<U>> for Cow<'_, [T]> {
318        fn eq(&self, other: &NonEmptyVec<U>) -> bool {
319            self.as_ref() == other
320        }
321    }
322
323    impl<T: PartialOrd + Clone> PartialOrd<NonEmptyVec<T>> for Cow<'_, [T]> {
324        fn partial_cmp(&self, other: &NonEmptyVec<T>) -> Option<Ordering> {
325            self.as_ref().partial_cmp(other)
326        }
327    }
328
329    // NonEmptyVec <-> NonEmptyBoxedSlice
330
331    impl<T: PartialEq<U>, U> PartialEq<NonEmptyVec<U>> for NonEmptyBoxedSlice<T> {
332        fn eq(&self, other: &NonEmptyVec<U>) -> bool {
333            self.as_ref() == other
334        }
335    }
336
337    impl<T: PartialOrd> PartialOrd<NonEmptyVec<T>> for NonEmptyBoxedSlice<T> {
338        fn partial_cmp(&self, other: &NonEmptyVec<T>) -> Option<Ordering> {
339            self.as_ref().partial_cmp(other)
340        }
341    }
342
343    impl<T: PartialEq<U>, U> PartialEq<NonEmptyBoxedSlice<U>> for NonEmptyVec<T> {
344        fn eq(&self, other: &NonEmptyBoxedSlice<U>) -> bool {
345            self == other.as_ref()
346        }
347    }
348
349    impl<T: PartialOrd> PartialOrd<NonEmptyBoxedSlice<T>> for NonEmptyVec<T> {
350        fn partial_cmp(&self, other: &NonEmptyBoxedSlice<T>) -> Option<Ordering> {
351            self.partial_cmp(other.as_ref())
352        }
353    }
354
355    // NonEmptyVec <-> BoxedSlice
356
357    impl<T: PartialEq<U>, U> PartialEq<Box<[U]>> for NonEmptyVec<T> {
358        fn eq(&self, other: &Box<[U]>) -> bool {
359            self == other.as_ref()
360        }
361    }
362
363    impl<T: PartialOrd> PartialOrd<Box<[T]>> for NonEmptyVec<T> {
364        fn partial_cmp(&self, other: &Box<[T]>) -> Option<Ordering> {
365            self.partial_cmp(other.as_ref())
366        }
367    }
368
369    impl<T: PartialEq<U>, U> PartialEq<NonEmptyVec<U>> for Box<[T]> {
370        fn eq(&self, other: &NonEmptyVec<U>) -> bool {
371            self.as_ref() == other.as_slice()
372        }
373    }
374
375    impl<T: PartialOrd> PartialOrd<NonEmptyVec<T>> for Box<[T]> {
376        fn partial_cmp(&self, other: &NonEmptyVec<T>) -> Option<Ordering> {
377            self.as_ref().partial_cmp(other.as_slice())
378        }
379    }
380
381    // NonEmptyBoxedSlice <-> NonEmptySlice
382
383    impl<T: PartialEq<U>, U> PartialEq<NonEmptySlice<U>> for NonEmptyBoxedSlice<T> {
384        fn eq(&self, other: &NonEmptySlice<U>) -> bool {
385            self.as_ref() == other
386        }
387    }
388
389    impl<T: PartialOrd> PartialOrd<NonEmptySlice<T>> for NonEmptyBoxedSlice<T> {
390        fn partial_cmp(&self, other: &NonEmptySlice<T>) -> Option<Ordering> {
391            self.as_ref().partial_cmp(other)
392        }
393    }
394
395    impl<T: PartialEq<U>, U> PartialEq<NonEmptyBoxedSlice<U>> for NonEmptySlice<T> {
396        fn eq(&self, other: &NonEmptyBoxedSlice<U>) -> bool {
397            self == other.as_ref()
398        }
399    }
400
401    impl<T: PartialOrd> PartialOrd<NonEmptyBoxedSlice<T>> for NonEmptySlice<T> {
402        fn partial_cmp(&self, other: &NonEmptyBoxedSlice<T>) -> Option<Ordering> {
403            self.partial_cmp(other.as_ref())
404        }
405    }
406
407    // NonEmptyBoxedSlice <-> Slice
408
409    impl<T: PartialEq<U>, U> PartialEq<[U]> for NonEmptyBoxedSlice<T> {
410        fn eq(&self, other: &[U]) -> bool {
411            self.as_ref() == other
412        }
413    }
414
415    impl<T: PartialOrd> PartialOrd<[T]> for NonEmptyBoxedSlice<T> {
416        fn partial_cmp(&self, other: &[T]) -> Option<Ordering> {
417            self.as_ref().partial_cmp(other)
418        }
419    }
420
421    impl<T: PartialEq<U>, U> PartialEq<NonEmptyBoxedSlice<U>> for [T] {
422        fn eq(&self, other: &NonEmptyBoxedSlice<U>) -> bool {
423            self == other.as_ref()
424        }
425    }
426
427    impl<T: PartialOrd> PartialOrd<NonEmptyBoxedSlice<T>> for [T] {
428        fn partial_cmp(&self, other: &NonEmptyBoxedSlice<T>) -> Option<Ordering> {
429            self.partial_cmp(other.as_ref())
430        }
431    }
432
433    // NonEmptyBoxedSlice <-> BoxedSlice
434
435    impl<T: PartialEq<U>, U> PartialEq<Box<[U]>> for NonEmptyBoxedSlice<T> {
436        fn eq(&self, other: &Box<[U]>) -> bool {
437            self.as_ref() == other.as_ref()
438        }
439    }
440
441    impl<T: PartialOrd> PartialOrd<Box<[T]>> for NonEmptyBoxedSlice<T> {
442        fn partial_cmp(&self, other: &Box<[T]>) -> Option<Ordering> {
443            self.as_ref().partial_cmp(other.as_ref())
444        }
445    }
446
447    impl<T: PartialEq<U>, U> PartialEq<NonEmptyBoxedSlice<U>> for Box<[T]> {
448        fn eq(&self, other: &NonEmptyBoxedSlice<U>) -> bool {
449            self.as_ref() == other.as_ref()
450        }
451    }
452
453    impl<T: PartialOrd> PartialOrd<NonEmptyBoxedSlice<T>> for Box<[T]> {
454        fn partial_cmp(&self, other: &NonEmptyBoxedSlice<T>) -> Option<Ordering> {
455            self.as_ref().partial_cmp(other.as_ref())
456        }
457    }
458
459    // NonEmptyBoxedSlice <-> NonEmptyCowSlice
460
461    impl<T: PartialEq<U>, U: Clone> PartialEq<NonEmptyCowSlice<'_, U>> for NonEmptyBoxedSlice<T> {
462        fn eq(&self, other: &NonEmptyCowSlice<'_, U>) -> bool {
463            self == other.as_ref()
464        }
465    }
466
467    impl<T: PartialOrd + Clone> PartialOrd<NonEmptyCowSlice<'_, T>> for NonEmptyBoxedSlice<T> {
468        fn partial_cmp(&self, other: &NonEmptyCowSlice<'_, T>) -> Option<Ordering> {
469            self.partial_cmp(other.as_ref())
470        }
471    }
472
473    impl<T: PartialEq<U> + Clone, U> PartialEq<NonEmptyBoxedSlice<U>> for NonEmptyCowSlice<'_, T> {
474        fn eq(&self, other: &NonEmptyBoxedSlice<U>) -> bool {
475            self.as_ref() == other
476        }
477    }
478
479    impl<T: PartialOrd + Clone> PartialOrd<NonEmptyBoxedSlice<T>> for NonEmptyCowSlice<'_, T> {
480        fn partial_cmp(&self, other: &NonEmptyBoxedSlice<T>) -> Option<Ordering> {
481            self.as_ref().partial_cmp(other)
482        }
483    }
484
485    // NonEmptyBoxedSlice <-> CowSlice
486
487    impl<T: PartialEq<U>, U: Clone> PartialEq<Cow<'_, [U]>> for NonEmptyBoxedSlice<T> {
488        fn eq(&self, other: &Cow<'_, [U]>) -> bool {
489            self == other.as_ref()
490        }
491    }
492
493    impl<T: PartialOrd + Clone> PartialOrd<Cow<'_, [T]>> for NonEmptyBoxedSlice<T> {
494        fn partial_cmp(&self, other: &Cow<'_, [T]>) -> Option<Ordering> {
495            self.partial_cmp(other.as_ref())
496        }
497    }
498
499    impl<T: PartialEq<U> + Clone, U> PartialEq<NonEmptyBoxedSlice<U>> for Cow<'_, [T]> {
500        fn eq(&self, other: &NonEmptyBoxedSlice<U>) -> bool {
501            self.as_ref() == other
502        }
503    }
504
505    impl<T: PartialOrd + Clone> PartialOrd<NonEmptyBoxedSlice<T>> for Cow<'_, [T]> {
506        fn partial_cmp(&self, other: &NonEmptyBoxedSlice<T>) -> Option<Ordering> {
507            self.as_ref().partial_cmp(other)
508        }
509    }
510
511    // NonEmptyBoxedSlice <-> Vec
512
513    impl<T: PartialEq<U>, U> PartialEq<Vec<U>> for NonEmptyBoxedSlice<T> {
514        fn eq(&self, other: &Vec<U>) -> bool {
515            self.as_ref() == other
516        }
517    }
518
519    impl<T: PartialOrd> PartialOrd<Vec<T>> for NonEmptyBoxedSlice<T> {
520        fn partial_cmp(&self, other: &Vec<T>) -> Option<Ordering> {
521            self.as_ref().partial_cmp(other)
522        }
523    }
524
525    impl<T: PartialEq<U>, U> PartialEq<NonEmptyBoxedSlice<U>> for Vec<T> {
526        fn eq(&self, other: &NonEmptyBoxedSlice<U>) -> bool {
527            self == other.as_ref()
528        }
529    }
530
531    impl<T: PartialOrd> PartialOrd<NonEmptyBoxedSlice<T>> for Vec<T> {
532        fn partial_cmp(&self, other: &NonEmptyBoxedSlice<T>) -> Option<Ordering> {
533            self.partial_cmp(other.as_ref())
534        }
535    }
536}