diff --git a/src/impls.rs b/src/impls.rs index 17241e2..4ec38b3 100644 --- a/src/impls.rs +++ b/src/impls.rs @@ -18,6 +18,26 @@ macro_rules! impl_partial_eq { }; } +macro_rules! impl_partial_eq_n { + ($lhs:ty, $rhs:ty) => { + impl<'a, 'b, const N: usize> PartialEq<$rhs> for $lhs { + #[inline] + fn eq(&self, other: &$rhs) -> bool { + let other: &[u8] = other.as_ref(); + PartialEq::eq(self.as_bytes(), other) + } + } + + impl<'a, 'b, const N: usize> PartialEq<$lhs> for $rhs { + #[inline] + fn eq(&self, other: &$lhs) -> bool { + let this: &[u8] = self.as_ref(); + PartialEq::eq(this, other.as_bytes()) + } + } + }; +} + #[cfg(feature = "alloc")] macro_rules! impl_partial_eq_cow { ($lhs:ty, $rhs:ty) => { @@ -59,6 +79,26 @@ macro_rules! impl_partial_ord { }; } +macro_rules! impl_partial_ord_n { + ($lhs:ty, $rhs:ty) => { + impl<'a, 'b, const N: usize> PartialOrd<$rhs> for $lhs { + #[inline] + fn partial_cmp(&self, other: &$rhs) -> Option { + let other: &[u8] = other.as_ref(); + PartialOrd::partial_cmp(self.as_bytes(), other) + } + } + + impl<'a, 'b, const N: usize> PartialOrd<$lhs> for $rhs { + #[inline] + fn partial_cmp(&self, other: &$lhs) -> Option { + let this: &[u8] = self.as_ref(); + PartialOrd::partial_cmp(this, other.as_bytes()) + } + } + }; +} + #[cfg(feature = "alloc")] mod bstring { use core::{cmp::Ordering, fmt, ops, str::FromStr}; @@ -368,6 +408,8 @@ mod bstring { impl_partial_eq!(BString, &'a str); impl_partial_eq!(BString, BStr); impl_partial_eq!(BString, &'a BStr); + impl_partial_eq_n!(BString, [u8; N]); + impl_partial_eq_n!(BString, &'a [u8; N]); impl PartialOrd for BString { #[inline] @@ -391,6 +433,8 @@ mod bstring { impl_partial_ord!(BString, &'a str); impl_partial_ord!(BString, BStr); impl_partial_ord!(BString, &'a BStr); + impl_partial_ord_n!(BString, [u8; N]); + impl_partial_ord_n!(BString, &'a [u8; N]); } mod bstr { @@ -811,6 +855,8 @@ mod bstr { impl_partial_eq!(BStr, &'a [u8]); impl_partial_eq!(BStr, str); impl_partial_eq!(BStr, &'a str); + impl_partial_eq_n!(BStr, [u8; N]); + impl_partial_eq_n!(BStr, &'a [u8; N]); #[cfg(feature = "alloc")] impl_partial_eq!(BStr, Vec); @@ -845,6 +891,8 @@ mod bstr { impl_partial_ord!(BStr, &'a [u8]); impl_partial_ord!(BStr, str); impl_partial_ord!(BStr, &'a str); + impl_partial_ord_n!(BStr, [u8; N]); + impl_partial_ord_n!(BStr, &'a [u8; N]); #[cfg(feature = "alloc")] impl_partial_ord!(BStr, Vec); @@ -1251,3 +1299,23 @@ fn test_cows_regression() { let c4 = "goodbye str"; assert_ne!(c3, c4); } + +#[test] +#[cfg(feature = "alloc")] +fn test_eq_ord() { + use core::cmp::Ordering; + + use crate::{BStr, BString}; + + let b = BStr::new("hello"); + assert_eq!(b, b"hello"); + assert_ne!(b, b"world"); + assert_eq!(b.partial_cmp(b"hello"), Some(Ordering::Equal)); + assert_eq!(b.partial_cmp(b"world"), Some(Ordering::Less)); + + let b = BString::from("hello"); + assert_eq!(b, b"hello"); + assert_ne!(b, b"world"); + assert_eq!(b.partial_cmp(b"hello"), Some(Ordering::Equal)); + assert_eq!(b.partial_cmp(b"world"), Some(Ordering::Less)); +}