@@ -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, hash, 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 hash:: Hash for BString {
373
415
#[ inline]
@@ -398,6 +440,8 @@ mod bstring {
398
440
impl_partial_ord ! ( BString , & ' a str ) ;
399
441
impl_partial_ord ! ( BString , BStr ) ;
400
442
impl_partial_ord ! ( BString , & ' a BStr ) ;
443
+ impl_partial_ord_n ! ( BString , [ u8 ; N ] ) ;
444
+ impl_partial_ord_n ! ( BString , & ' a [ u8 ; N ] ) ;
401
445
}
402
446
403
447
mod bstr {
@@ -821,6 +865,8 @@ mod bstr {
821
865
impl_partial_eq ! ( BStr , & ' a [ u8 ] ) ;
822
866
impl_partial_eq ! ( BStr , str ) ;
823
867
impl_partial_eq ! ( BStr , & ' a str ) ;
868
+ impl_partial_eq_n ! ( BStr , [ u8 ; N ] ) ;
869
+ impl_partial_eq_n ! ( BStr , & ' a [ u8 ; N ] ) ;
824
870
825
871
#[ cfg( feature = "alloc" ) ]
826
872
impl_partial_eq ! ( BStr , Vec <u8 >) ;
@@ -862,6 +908,8 @@ mod bstr {
862
908
impl_partial_ord ! ( BStr , & ' a [ u8 ] ) ;
863
909
impl_partial_ord ! ( BStr , str ) ;
864
910
impl_partial_ord ! ( BStr , & ' a str ) ;
911
+ impl_partial_ord_n ! ( BStr , [ u8 ; N ] ) ;
912
+ impl_partial_ord_n ! ( BStr , & ' a [ u8 ; N ] ) ;
865
913
866
914
#[ cfg( feature = "alloc" ) ]
867
915
impl_partial_ord ! ( BStr , Vec <u8 >) ;
@@ -1276,3 +1324,23 @@ fn test_cows_regression() {
1276
1324
let c4 = "goodbye str" ;
1277
1325
assert_ne ! ( c3, c4) ;
1278
1326
}
1327
+
1328
+ #[ test]
1329
+ #[ cfg( feature = "alloc" ) ]
1330
+ fn test_eq_ord ( ) {
1331
+ use core:: cmp:: Ordering ;
1332
+
1333
+ use crate :: { BStr , BString } ;
1334
+
1335
+ let b = BStr :: new ( "hello" ) ;
1336
+ assert_eq ! ( b, b"hello" ) ;
1337
+ assert_ne ! ( b, b"world" ) ;
1338
+ assert_eq ! ( b. partial_cmp( b"hello" ) , Some ( Ordering :: Equal ) ) ;
1339
+ assert_eq ! ( b. partial_cmp( b"world" ) , Some ( Ordering :: Less ) ) ;
1340
+
1341
+ let b = BString :: from ( "hello" ) ;
1342
+ assert_eq ! ( b, b"hello" ) ;
1343
+ assert_ne ! ( b, b"world" ) ;
1344
+ assert_eq ! ( b. partial_cmp( b"hello" ) , Some ( Ordering :: Equal ) ) ;
1345
+ assert_eq ! ( b. partial_cmp( b"world" ) , Some ( Ordering :: Less ) ) ;
1346
+ }
0 commit comments