Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PartialEq and PartialOrd instances for byte arrays [u8; N] #191

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
Loading