Skip to content

Commit

Permalink
Add PartialEq and PartialOrd instances for byte arrays [u8; N]
Browse files Browse the repository at this point in the history
Add instances for `[u8; N]` and `&[u8; N]`, for convenience.

Verified to work with current MSRV of 1.65.

Add test for these impls.
  • Loading branch information
joshtriplett committed Oct 7, 2024
1 parent 3dc5939 commit 8b81735
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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<Ordering> {
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<Ordering> {
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};
Expand Down Expand Up @@ -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]
Expand All @@ -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 {
Expand Down Expand Up @@ -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<u8>);
Expand Down Expand Up @@ -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<u8>);
Expand Down Expand Up @@ -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));
}

0 comments on commit 8b81735

Please sign in to comment.