Skip to content

Commit

Permalink
fix: More PartialEq impls (ParkMyCar#381)
Browse files Browse the repository at this point in the history
* start

* fix clippy

* some more impls
  • Loading branch information
ParkMyCar authored Jun 8, 2024
1 parent d479863 commit 8bee76b
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 8 deletions.
59 changes: 56 additions & 3 deletions compact_str/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,8 +624,7 @@ impl CompactString {
///
/// # Safety
/// * All Rust strings, including `CompactString`, must be valid UTF-8. The caller must
/// guarantee
/// that any modifications made to the underlying buffer are valid UTF-8.
/// guarantee that any modifications made to the underlying buffer are valid UTF-8.
///
/// # Examples
/// ```
Expand Down Expand Up @@ -2027,30 +2026,84 @@ impl BorrowMut<str> for CompactString {

impl Eq for CompactString {}

impl<T: AsRef<str>> PartialEq<T> for CompactString {
impl<T: AsRef<str> + ?Sized> PartialEq<T> for CompactString {
fn eq(&self, other: &T) -> bool {
self.as_str() == other.as_ref()
}
}

impl PartialEq<CompactString> for &CompactString {
fn eq(&self, other: &CompactString) -> bool {
self.as_str() == other.as_str()
}
}

impl PartialEq<CompactString> for String {
fn eq(&self, other: &CompactString) -> bool {
self.as_str() == other.as_str()
}
}

impl<'a> PartialEq<&'a CompactString> for String {
fn eq(&self, other: &&CompactString) -> bool {
self.as_str() == other.as_str()
}
}

impl PartialEq<CompactString> for &String {
fn eq(&self, other: &CompactString) -> bool {
self.as_str() == other.as_str()
}
}

impl PartialEq<CompactString> for str {
fn eq(&self, other: &CompactString) -> bool {
self == other.as_str()
}
}

impl<'a> PartialEq<&'a CompactString> for str {
fn eq(&self, other: &&CompactString) -> bool {
self == other.as_str()
}
}

impl PartialEq<CompactString> for &str {
fn eq(&self, other: &CompactString) -> bool {
*self == other.as_str()
}
}

impl PartialEq<CompactString> for &&str {
fn eq(&self, other: &CompactString) -> bool {
**self == other.as_str()
}
}

impl<'a> PartialEq<CompactString> for Cow<'a, str> {
fn eq(&self, other: &CompactString) -> bool {
*self == other.as_str()
}
}

impl<'a> PartialEq<CompactString> for &Cow<'a, str> {
fn eq(&self, other: &CompactString) -> bool {
*self == other.as_str()
}
}

impl PartialEq<String> for &CompactString {
fn eq(&self, other: &String) -> bool {
self.as_str() == other.as_str()
}
}

impl<'a> PartialEq<Cow<'a, str>> for &CompactString {
fn eq(&self, other: &Cow<'a, str>) -> bool {
self.as_str() == other
}
}

impl Ord for CompactString {
fn cmp(&self, other: &Self) -> Ordering {
self.as_str().cmp(other.as_str())
Expand Down
10 changes: 5 additions & 5 deletions compact_str/src/repr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ impl Repr {
///
/// # SAFETY
/// * The caller must guarantee that the provided [`Repr`] is actually a [`HeapBuffer`] by
/// checking the discriminant
/// checking the discriminant.
///
/// Note: We used to define [`Repr`] as a `union` which implicitly transmuted between the two
/// types, but that prevented us from defining a "niche" value to make `Option<CompactString>`
Expand All @@ -621,7 +621,7 @@ impl Repr {
///
/// # SAFETY
/// * The caller must guarantee that the provided [`Repr`] is actually a [`HeapBuffer`] by
/// checking the discriminant
/// checking the discriminant.
///
/// Note: We used to define [`Repr`] as a `union` which implicitly transmuted between the two
/// types, but that prevented us from defining a "niche" value to make `Option<CompactString>`
Expand All @@ -636,7 +636,7 @@ impl Repr {
///
/// # SAFETY
/// * The caller must guarantee that the provided [`Repr`] is actually a [`HeapBuffer`] by
/// checking the discriminant
/// checking the discriminant.
///
/// Note: We used to define [`Repr`] as a `union` which implicitly transmuted between the two
/// types, but that prevented us from defining a "niche" value to make `Option<CompactString>`
Expand All @@ -651,7 +651,7 @@ impl Repr {
///
/// # SAFETY
/// * The caller must guarantee that the provided [`Repr`] is actually an [`InlineBuffer`] by
/// checking the discriminant
/// checking the discriminant.
///
/// Note: We used to define [`Repr`] as a `union` which implicitly transmuted between the two
/// types, but that prevented us from defining a "niche" value to make `Option<CompactString>`
Expand All @@ -666,7 +666,7 @@ impl Repr {
///
/// # SAFETY
/// * The caller must guarantee that the provided [`Repr`] is actually an [`InlineBuffer`] by
/// checking the discriminant
/// checking the discriminant.
///
/// Note: We used to define [`Repr`] as a `union` which implicitly transmuted between the two
/// types, but that prevented us from defining a "niche" value to make `Option<CompactString>`
Expand Down
27 changes: 27 additions & 0 deletions compact_str/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,33 @@ fn test_plus_equals_operator_static_str() {
assert_eq!(m, "ab");
}

// Allow these lints because we're explicitly testing impls for owned types and
// reference types.
#[allow(clippy::cmp_owned)]
#[allow(clippy::op_ref)]
#[test]
fn test_eq_operator() {
let x = CompactString::const_new("foo");
let y = x.clone();

macro_rules! test_impl {
($a:expr, $b:expr) => {
let _ = $a == $b;
let _ = &$a == $b;
let _ = &$a == &$b;

let _ = $b == $a;
let _ = &$b == $a;
let _ = &$b == &$a;
};
}

test_impl!("a", x);
test_impl!(String::from("a"), x);
test_impl!(Cow::Borrowed("a"), x);
test_impl!(y, x);
}

#[test]
fn test_u8_to_compact_string() {
let vals = [u8::MIN, 1, 42, u8::MAX - 2, u8::MAX - 1, u8::MAX];
Expand Down

0 comments on commit 8bee76b

Please sign in to comment.