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 more PartialEq implementations for CowStr to allow 'cow_str == blah' #44

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
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
47 changes: 45 additions & 2 deletions merde_core/src/cowstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,36 @@ impl From<CowStr<'_>> for Box<str> {
}
}

impl PartialEq for CowStr<'_> {
fn eq(&self, other: &Self) -> bool {
impl<'a, 'b> PartialEq<CowStr<'a>> for CowStr<'b> {
fn eq(&self, other: &CowStr<'a>) -> bool {
self.deref() == other.deref()
}
}

impl PartialEq<&str> for CowStr<'_> {
fn eq(&self, other: &&str) -> bool {
self.deref() == *other
}
}

impl PartialEq<CowStr<'_>> for &str {
fn eq(&self, other: &CowStr<'_>) -> bool {
*self == other.deref()
}
}

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

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

impl Eq for CowStr<'_> {}

impl Hash for CowStr<'_> {
Expand Down Expand Up @@ -156,3 +180,22 @@ mod serde_impls {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_partialeq_with_str() {
let cow_str1 = CowStr::Borrowed("hello");
let cow_str2 = CowStr::Borrowed("hello");
let cow_str3 = CowStr::Borrowed("world");

assert_eq!(cow_str1, "hello");
assert_eq!("hello", cow_str1);
assert_eq!(cow_str1, cow_str2);
assert_ne!(cow_str1, "world");
assert_ne!("world", cow_str1);
assert_ne!(cow_str1, cow_str3);
}
}
4 changes: 2 additions & 2 deletions zerodeps-example/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.