From 829051a30a1fef1e83b9bc0d4c10ff9874589f65 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Wed, 9 Aug 2023 02:50:07 +0300 Subject: [PATCH] refactor: dereference in partial_eq and add tests --- .changes/derefence.md | 5 +++++ src/menu_id.rs | 24 ++++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 .changes/derefence.md diff --git a/.changes/derefence.md b/.changes/derefence.md new file mode 100644 index 0000000..19fbda3 --- /dev/null +++ b/.changes/derefence.md @@ -0,0 +1,5 @@ +--- +"muda": "patch" +--- + +Dereference `&String` and `&&str` in `PartialEq` for `MenuId` type diff --git a/src/menu_id.rs b/src/menu_id.rs index 08288e6..2eae062 100644 --- a/src/menu_id.rs +++ b/src/menu_id.rs @@ -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 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"); + } +}