From 06121f8234890b1e1e275ee9038dcef1dc076b81 Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 14 Mar 2022 21:35:18 +0100 Subject: [PATCH] Add map that provides a PartialEq implementation Signed-off-by: trivernis --- README.md | 2 +- src/macros.rs | 1 + src/tests.rs | 11 ++++++++++- src/trait_maps/mod.rs | 2 ++ src/trait_maps/partialeq_typemap.rs | 18 ++++++++++++++++++ 5 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 src/trait_maps/partialeq_typemap.rs diff --git a/README.md b/README.md index 92615f6..9b26d8b 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Trait bound Typemap This crate offers typemaps that restrict a given type in their -trait and therefore offer additional trait implementations such as `Clone`. +trait and therefore offer additional trait implementations such as `Clone` and `PartialEq`. ## Usage diff --git a/src/macros.rs b/src/macros.rs index fbf1135..bf9a127 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -5,6 +5,7 @@ macro_rules! impl_typemap { ($( #[$outer:meta] )* $map:ident, $key:ident, $($trt:ident), +) => { $( #[$outer] )* + #[derive(Debug)] pub struct $map($crate::base::TypeMapBase); $crate::impl_typekey!($key, $( $trt )+); diff --git a/src/tests.rs b/src/tests.rs index 7af5cb4..7e9bef1 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,4 +1,4 @@ -use crate::{CloneTypeMap, TypeMap, TypeMapKey, TypeMapTrait}; +use crate::{CloneTypeMap, PartialEqTypeMap, TypeMap, TypeMapKey, TypeMapTrait}; pub struct TestStructKey; #[derive(Clone, Debug, PartialEq)] @@ -70,3 +70,12 @@ fn it_creates_clonable_maps() { assert!(map.contains_key::()); assert_eq!(map.contains_key::(), false); } + +#[test] +fn it_creates_partial_eq_maps() { + let mut map = PartialEqTypeMap::new(); + map.insert::(TestStruct::default()); + let mut map2 = PartialEqTypeMap::new(); + map2.insert::(TestStruct::default()); + assert_eq!(map, map2) +} diff --git a/src/trait_maps/mod.rs b/src/trait_maps/mod.rs index 4299cce..8c8b47b 100644 --- a/src/trait_maps/mod.rs +++ b/src/trait_maps/mod.rs @@ -1,5 +1,7 @@ mod clone_typemap; +mod partialeq_typemap; mod typemap; pub use clone_typemap::*; +pub use partialeq_typemap::*; pub use typemap::*; diff --git a/src/trait_maps/partialeq_typemap.rs b/src/trait_maps/partialeq_typemap.rs new file mode 100644 index 0000000..d8bca7d --- /dev/null +++ b/src/trait_maps/partialeq_typemap.rs @@ -0,0 +1,18 @@ +use crate::impl_typemap; +use multi_trait_object::{PartialEqAny, TryPartialEq}; +impl_typemap!( + /// A typemap that provides a PartialEq implementation + PartialEqTypeMap, + PartialEqTypeMapKey, + PartialEqAny +); + +impl PartialEq for PartialEqTypeMap { + fn eq(&self, other: &Self) -> bool { + self.0 + .0 + .iter() + .zip(other.0 .0.iter()) + .all(|(a, b)| a.0 == b.0 && a.1.try_eq(b.1).unwrap()) + } +}