Add extend implementation for all typemaps

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 2 years ago
parent f2bf355633
commit 7938f6064d
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1,6 +1,6 @@
[package]
name = "trait-bound-typemap"
version = "0.3.0"
version = "0.3.1"
edition = "2021"
license = "Apache-2.0"
readme = "README.md"

@ -80,6 +80,17 @@ macro_rules! impl_typemap {
$map($crate::type_indexed::TypeIndexedMap(map))
}
}
impl<K: $crate::MapKey<Map = M>, M: 'static $(+ $trt )+> Extend<$crate::TypeMapEntry<K>> for $map {
fn extend<T: IntoIterator<Item = $crate::TypeMapEntry<K>>>(&mut self, iter: T) {
let iter = iter
.into_iter()
.map(|e: $crate::TypeMapEntry<K>| (e.type_id, e.mto))
.collect::<Vec<(std::any::TypeId, multi_trait_object::MultitraitObject)>>();
self.0.extend(iter)
}
}
};
}

@ -97,8 +97,23 @@ fn it_converts() {
let mut clone_send_sync_map = CloneSendSyncTypeMap::new();
clone_send_sync_map.insert::<TestStructKey>(TestStruct::default());
assert!(clone_send_sync_map.contains_key::<TestStructKey>());
let clone_map = CloneTypeMap::from_iter(clone_send_sync_map);
assert!(clone_map.contains_key::<TestStructKey>());
let any_map = AnyTypeMap::from_iter(clone_map);
assert!(any_map.contains_key::<TestStructKey>());
}
#[test]
fn it_can_be_extended() {
let mut clone_send_sync_map = CloneSendSyncTypeMap::new();
clone_send_sync_map.insert::<TestStructKey>(TestStruct::default());
assert!(clone_send_sync_map.contains_key::<TestStructKey>());
let mut clone_map = CloneTypeMap::new();
clone_map.insert::<TestStruct2Key>(TestStruct2::default());
clone_map.extend(clone_send_sync_map);
assert!(clone_map.contains_key::<TestStructKey>());
assert!(clone_map.contains_key::<TestStruct2Key>());
}

@ -1,4 +1,5 @@
use std::any::TypeId;
use std::collections::hash_map::IntoIter;
use std::collections::HashMap;
/// Base typemap used for implementation but not elsewhere
@ -45,3 +46,18 @@ impl<T> TypeIndexedMap<T> {
self.0.contains_key(&TypeId::of::<K>())
}
}
impl<T> IntoIterator for TypeIndexedMap<T> {
type Item = (TypeId, T);
type IntoIter = IntoIter<TypeId, T>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<T> Extend<(TypeId, T)> for TypeIndexedMap<T> {
fn extend<I: IntoIterator<Item = (TypeId, T)>>(&mut self, iter: I) {
self.0.extend(iter)
}
}

Loading…
Cancel
Save