@@ -18,6 +18,26 @@ macro_rules! impl_partial_eq {
18
18
} ;
19
19
}
20
20
21
+ macro_rules! impl_partial_eq_n {
22
+ ( $lhs: ty, $rhs: ty) => {
23
+ impl <' a, ' b, const N : usize > PartialEq <$rhs> for $lhs {
24
+ #[ inline]
25
+ fn eq( & self , other: & $rhs) -> bool {
26
+ let other: & [ u8 ] = other. as_ref( ) ;
27
+ PartialEq :: eq( self . as_bytes( ) , other)
28
+ }
29
+ }
30
+
31
+ impl <' a, ' b, const N : usize > PartialEq <$lhs> for $rhs {
32
+ #[ inline]
33
+ fn eq( & self , other: & $lhs) -> bool {
34
+ let this: & [ u8 ] = self . as_ref( ) ;
35
+ PartialEq :: eq( this, other. as_bytes( ) )
36
+ }
37
+ }
38
+ } ;
39
+ }
40
+
21
41
#[ cfg( feature = "alloc" ) ]
22
42
macro_rules! impl_partial_eq_cow {
23
43
( $lhs: ty, $rhs: ty) => {
@@ -59,6 +79,26 @@ macro_rules! impl_partial_ord {
59
79
} ;
60
80
}
61
81
82
+ macro_rules! impl_partial_ord_n {
83
+ ( $lhs: ty, $rhs: ty) => {
84
+ impl <' a, ' b, const N : usize > PartialOrd <$rhs> for $lhs {
85
+ #[ inline]
86
+ fn partial_cmp( & self , other: & $rhs) -> Option <Ordering > {
87
+ let other: & [ u8 ] = other. as_ref( ) ;
88
+ PartialOrd :: partial_cmp( self . as_bytes( ) , other)
89
+ }
90
+ }
91
+
92
+ impl <' a, ' b, const N : usize > PartialOrd <$lhs> for $rhs {
93
+ #[ inline]
94
+ fn partial_cmp( & self , other: & $lhs) -> Option <Ordering > {
95
+ let this: & [ u8 ] = self . as_ref( ) ;
96
+ PartialOrd :: partial_cmp( this, other. as_bytes( ) )
97
+ }
98
+ }
99
+ } ;
100
+ }
101
+
62
102
#[ cfg( feature = "alloc" ) ]
63
103
mod bstring {
64
104
use core:: { cmp:: Ordering , fmt, ops, str:: FromStr } ;
@@ -368,6 +408,8 @@ mod bstring {
368
408
impl_partial_eq ! ( BString , & ' a str ) ;
369
409
impl_partial_eq ! ( BString , BStr ) ;
370
410
impl_partial_eq ! ( BString , & ' a BStr ) ;
411
+ impl_partial_eq_n ! ( BString , [ u8 ; N ] ) ;
412
+ impl_partial_eq_n ! ( BString , & ' a [ u8 ; N ] ) ;
371
413
372
414
impl PartialOrd for BString {
373
415
#[ inline]
@@ -391,6 +433,8 @@ mod bstring {
391
433
impl_partial_ord ! ( BString , & ' a str ) ;
392
434
impl_partial_ord ! ( BString , BStr ) ;
393
435
impl_partial_ord ! ( BString , & ' a BStr ) ;
436
+ impl_partial_ord_n ! ( BString , [ u8 ; N ] ) ;
437
+ impl_partial_ord_n ! ( BString , & ' a [ u8 ; N ] ) ;
394
438
}
395
439
396
440
mod bstr {
@@ -811,6 +855,8 @@ mod bstr {
811
855
impl_partial_eq ! ( BStr , & ' a [ u8 ] ) ;
812
856
impl_partial_eq ! ( BStr , str ) ;
813
857
impl_partial_eq ! ( BStr , & ' a str ) ;
858
+ impl_partial_eq_n ! ( BStr , [ u8 ; N ] ) ;
859
+ impl_partial_eq_n ! ( BStr , & ' a [ u8 ; N ] ) ;
814
860
815
861
#[ cfg( feature = "alloc" ) ]
816
862
impl_partial_eq ! ( BStr , Vec <u8 >) ;
@@ -845,6 +891,8 @@ mod bstr {
845
891
impl_partial_ord ! ( BStr , & ' a [ u8 ] ) ;
846
892
impl_partial_ord ! ( BStr , str ) ;
847
893
impl_partial_ord ! ( BStr , & ' a str ) ;
894
+ impl_partial_ord_n ! ( BStr , [ u8 ; N ] ) ;
895
+ impl_partial_ord_n ! ( BStr , & ' a [ u8 ; N ] ) ;
848
896
849
897
#[ cfg( feature = "alloc" ) ]
850
898
impl_partial_ord ! ( BStr , Vec <u8 >) ;
@@ -1251,3 +1299,23 @@ fn test_cows_regression() {
1251
1299
let c4 = "goodbye str" ;
1252
1300
assert_ne ! ( c3, c4) ;
1253
1301
}
1302
+
1303
+ #[ test]
1304
+ #[ cfg( feature = "alloc" ) ]
1305
+ fn test_eq_ord ( ) {
1306
+ use core:: cmp:: Ordering ;
1307
+
1308
+ use crate :: { BStr , BString } ;
1309
+
1310
+ let b = BStr :: new ( "hello" ) ;
1311
+ assert_eq ! ( b, b"hello" ) ;
1312
+ assert_ne ! ( b, b"world" ) ;
1313
+ assert_eq ! ( b. partial_cmp( b"hello" ) , Some ( Ordering :: Equal ) ) ;
1314
+ assert_eq ! ( b. partial_cmp( b"world" ) , Some ( Ordering :: Less ) ) ;
1315
+
1316
+ let b = BString :: from ( "hello" ) ;
1317
+ assert_eq ! ( b, b"hello" ) ;
1318
+ assert_ne ! ( b, b"world" ) ;
1319
+ assert_eq ! ( b. partial_cmp( b"hello" ) , Some ( Ordering :: Equal ) ) ;
1320
+ assert_eq ! ( b. partial_cmp( b"world" ) , Some ( Ordering :: Less ) ) ;
1321
+ }
0 commit comments