Skip to content

Commit 6b713be

Browse files
joshtriplettBurntSushi
authored andcommitted
impl: add PartialEq and PartialOrd instances for byte arrays [u8; N]
Add instances for `[u8; N]` and `&[u8; N]`, for convenience. Closes #191
1 parent af99a6e commit 6b713be

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, hash, 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 hash::Hash for BString {
373415
#[inline]
@@ -398,6 +440,8 @@ mod bstring {
398440
impl_partial_ord!(BString, &'a str);
399441
impl_partial_ord!(BString, BStr);
400442
impl_partial_ord!(BString, &'a BStr);
443+
impl_partial_ord_n!(BString, [u8; N]);
444+
impl_partial_ord_n!(BString, &'a [u8; N]);
401445
}
402446

403447
mod bstr {
@@ -821,6 +865,8 @@ mod bstr {
821865
impl_partial_eq!(BStr, &'a [u8]);
822866
impl_partial_eq!(BStr, str);
823867
impl_partial_eq!(BStr, &'a str);
868+
impl_partial_eq_n!(BStr, [u8; N]);
869+
impl_partial_eq_n!(BStr, &'a [u8; N]);
824870

825871
#[cfg(feature = "alloc")]
826872
impl_partial_eq!(BStr, Vec<u8>);
@@ -862,6 +908,8 @@ mod bstr {
862908
impl_partial_ord!(BStr, &'a [u8]);
863909
impl_partial_ord!(BStr, str);
864910
impl_partial_ord!(BStr, &'a str);
911+
impl_partial_ord_n!(BStr, [u8; N]);
912+
impl_partial_ord_n!(BStr, &'a [u8; N]);
865913

866914
#[cfg(feature = "alloc")]
867915
impl_partial_ord!(BStr, Vec<u8>);
@@ -1276,3 +1324,23 @@ fn test_cows_regression() {
12761324
let c4 = "goodbye str";
12771325
assert_ne!(c3, c4);
12781326
}
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

Comments
 (0)