Skip to main content

non_empty_str/
cmp.rs

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