Skip to content

Commit

Permalink
impl: 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.

Closes #191
  • Loading branch information
joshtriplett authored and BurntSushi committed Nov 13, 2024
1 parent af99a6e commit 6b713be
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, hash, 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 hash::Hash for BString {
#[inline]
Expand Down Expand Up @@ -398,6 +440,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 @@ -821,6 +865,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 @@ -862,6 +908,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 @@ -1276,3 +1324,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 6b713be

Please sign in to comment.