Skip to content

Commit

Permalink
implement PartialEq<str> for string new types
Browse files Browse the repository at this point in the history
  • Loading branch information
ModProg committed Aug 25, 2024
1 parent 03a3604 commit 6233967
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions dbus/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,22 @@ impl<'m> hash::Hash for $t<'m> {
}
}

impl PartialEq<str> for $t<'_> {
fn eq(&self, other: &str) -> bool {
self.0 == other ||
// ignores the trailing \0
&self.0[..self.0.len()-1] == other
}
}

impl PartialEq<&'_ str> for $t<'_> {
fn eq(&self, other: &&str) -> bool {
self.0 == *other ||
// ignores the trailing \0
&self.0[..self.0.len()-1] == *other
}
}

}}

/// A wrapper around a string that is guaranteed to be
Expand Down Expand Up @@ -248,3 +264,11 @@ fn reborrow_path() {
fn make_sig() {
assert_eq!(&*Signature::make::<(&str, u8)>(), "(sy)");
}

#[test]
fn partial_eq_str() {
assert_eq!(Signature::new("s").unwrap(), "s");
assert_eq!(Signature::new("h").unwrap(), "h\0");
assert_eq!(Path::new("/hello").unwrap(), "/hello");
assert_eq!(Path::new("/world").unwrap(), "/world\0");
}

0 comments on commit 6233967

Please sign in to comment.