refactor: dereference in partial_eq and add tests

This commit is contained in:
Amr Bashir 2023-08-09 02:50:07 +03:00
parent 121c97519f
commit 829051a30a
No known key found for this signature in database
GPG key ID: BBD7A47A2003FF33
2 changed files with 25 additions and 4 deletions

5
.changes/derefence.md Normal file
View file

@ -0,0 +1,5 @@
---
"muda": "patch"
---
Dereference `&String` and `&&str` in `PartialEq` for `MenuId` type

View file

@ -1,6 +1,6 @@
use std::{convert::Infallible, str::FromStr};
/// An unique id that is associated with a menu item.
/// An unique id that is associated with a menu or a menu item.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
pub struct MenuId(pub String);
@ -39,19 +39,19 @@ impl FromStr for MenuId {
impl PartialEq<&str> for MenuId {
fn eq(&self, other: &&str) -> bool {
other == &self.0
self.0 == *other
}
}
impl PartialEq<String> for MenuId {
fn eq(&self, other: &String) -> bool {
other == &self.0
self.0 == *other
}
}
impl PartialEq<&String> for MenuId {
fn eq(&self, other: &&String) -> bool {
other == &&self.0
self.0 == **other
}
}
@ -60,3 +60,19 @@ impl PartialEq<&MenuId> for MenuId {
other.0 == self.0
}
}
#[cfg(test)]
mod test {
use crate::MenuId;
#[test]
fn is_eq() {
assert_eq!(MenuId::new("t"), "t",);
assert_eq!(MenuId::new("t"), String::from("t"));
assert_eq!(MenuId::new("t"), &String::from("t"));
assert_eq!(MenuId::new("t"), MenuId::new("t"));
assert_eq!(MenuId::new("t"), &MenuId::new("t"));
assert_eq!(&MenuId::new("t"), &MenuId::new("t"));
assert_eq!(MenuId::new("t").as_ref(), "t");
}
}