Add map that provides a PartialEq implementation

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 2 years ago
parent 29a76538dc
commit 06121f8234
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -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

@ -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 )+);

@ -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::<TestStruct2Key>());
assert_eq!(map.contains_key::<NotInsertedKey>(), false);
}
#[test]
fn it_creates_partial_eq_maps() {
let mut map = PartialEqTypeMap::new();
map.insert::<TestStructKey>(TestStruct::default());
let mut map2 = PartialEqTypeMap::new();
map2.insert::<TestStructKey>(TestStruct::default());
assert_eq!(map, map2)
}

@ -1,5 +1,7 @@
mod clone_typemap;
mod partialeq_typemap;
mod typemap;
pub use clone_typemap::*;
pub use partialeq_typemap::*;
pub use typemap::*;

@ -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())
}
}
Loading…
Cancel
Save