Skip to content

Commit 8b81735

Browse files
committed
Add PartialEq and PartialOrd instances for byte arrays [u8; N]
Add instances for `[u8; N]` and `&[u8; N]`, for convenience. Verified to work with current MSRV of 1.65. Add test for these impls.
1 parent 3dc5939 commit 8b81735

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

src/impls.rs

+68
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ macro_rules! impl_partial_eq {
1818
};
1919
}
2020

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+
2141
#[cfg(feature = "alloc")]
2242
macro_rules! impl_partial_eq_cow {
2343
($lhs:ty, $rhs:ty) => {
@@ -59,6 +79,26 @@ macro_rules! impl_partial_ord {
5979
};
6080
}
6181

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+
62102
#[cfg(feature = "alloc")]
63103
mod bstring {
64104
use core::{cmp::Ordering, fmt, ops, str::FromStr};
@@ -368,6 +408,8 @@ mod bstring {
368408
impl_partial_eq!(BString, &'a str);
369409
impl_partial_eq!(BString, BStr);
370410
impl_partial_eq!(BString, &'a BStr);
411+
impl_partial_eq_n!(BString, [u8; N]);
412+
impl_partial_eq_n!(BString, &'a [u8; N]);
371413

372414
impl PartialOrd for BString {
373415
#[inline]
@@ -391,6 +433,8 @@ mod bstring {
391433
impl_partial_ord!(BString, &'a str);
392434
impl_partial_ord!(BString, BStr);
393435
impl_partial_ord!(BString, &'a BStr);
436+
impl_partial_ord_n!(BString, [u8; N]);
437+
impl_partial_ord_n!(BString, &'a [u8; N]);
394438
}
395439

396440
mod bstr {
@@ -811,6 +855,8 @@ mod bstr {
811855
impl_partial_eq!(BStr, &'a [u8]);
812856
impl_partial_eq!(BStr, str);
813857
impl_partial_eq!(BStr, &'a str);
858+
impl_partial_eq_n!(BStr, [u8; N]);
859+
impl_partial_eq_n!(BStr, &'a [u8; N]);
814860

815861
#[cfg(feature = "alloc")]
816862
impl_partial_eq!(BStr, Vec<u8>);
@@ -845,6 +891,8 @@ mod bstr {
845891
impl_partial_ord!(BStr, &'a [u8]);
846892
impl_partial_ord!(BStr, str);
847893
impl_partial_ord!(BStr, &'a str);
894+
impl_partial_ord_n!(BStr, [u8; N]);
895+
impl_partial_ord_n!(BStr, &'a [u8; N]);
848896

849897
#[cfg(feature = "alloc")]
850898
impl_partial_ord!(BStr, Vec<u8>);
@@ -1251,3 +1299,23 @@ fn test_cows_regression() {
12511299
let c4 = "goodbye str";
12521300
assert_ne!(c3, c4);
12531301
}
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

Comments
 (0)