Add typemap that can be cloned and is Send + Sync

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 3 years ago
parent 86ca6ce3e1
commit b353b82e61
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -8,7 +8,7 @@ macro_rules! impl_typemap {
#[derive(Debug)]
pub struct $map($crate::type_indexed::TypeIndexedMap<multi_trait_object::MultitraitObject>);
$crate::impl_typekey!($key, $( $trt )+);
$crate::impl_typekey!($key $(, $trt )+);
impl $crate::MapKey for $key {
type Map = $map;

@ -1,4 +1,6 @@
use crate::{AnyTypeMap, CloneTypeMap, PartialEqTypeMap, TypeMapKey, TypeMapTrait};
use crate::{
AnyTypeMap, CloneSendSyncTypeMap, CloneTypeMap, PartialEqTypeMap, TypeMapKey, TypeMapTrait,
};
pub struct TestStructKey;
#[derive(Clone, Debug, PartialEq)]
@ -71,6 +73,16 @@ fn it_creates_clonable_maps() {
assert_eq!(map.contains_key::<NotInsertedKey>(), false);
}
#[test]
fn it_creates_clonable_send_sync_maps() {
let mut map = CloneSendSyncTypeMap::new();
map.insert::<TestStructKey>(TestStruct::default());
map.insert::<TestStruct2Key>(TestStruct2::default());
assert!(map.contains_key::<TestStructKey>());
assert!(map.contains_key::<TestStruct2Key>());
assert_eq!(map.contains_key::<NotInsertedKey>(), false);
}
#[test]
fn it_creates_partial_eq_maps() {
let mut map = PartialEqTypeMap::new();
@ -82,8 +94,10 @@ fn it_creates_partial_eq_maps() {
#[test]
fn it_converts() {
let mut clone_map = CloneTypeMap::new();
clone_map.insert::<TestStructKey>(TestStruct::default());
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>());

@ -0,0 +1,29 @@
use crate::impl_typemap;
use multi_trait_object::{MultitraitObject, RawClone, TryClone};
use std::any::TypeId;
use std::collections::HashMap;
impl_typemap!(
/// A typemap that implements Clone and is Send + Sync
CloneSendSyncTypeMap,
CloneSendSyncTypeMapKey,
RawClone,
Send,
Sync
);
impl Clone for CloneSendSyncTypeMap {
fn clone(&self) -> Self {
let map = self
.0
.0
.iter()
.map(|(tid, mto)| (tid.clone(), mto.try_clone().unwrap()))
.collect::<HashMap<TypeId, MultitraitObject>>();
Self(crate::type_indexed::TypeIndexedMap(map))
}
}
unsafe impl Send for CloneSendSyncTypeMap {}
unsafe impl Sync for CloneSendSyncTypeMap {}

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

@ -27,10 +27,6 @@ pub trait MapKey {
type Map: TypeMapTrait<Key = Self>;
}
/// A marker trait to transfer trait information
#[doc(hidden)]
pub trait TraitCarrier<T> {}
/// A trait implemented by all typemaps that provides
/// all basic typemap functions
pub trait TypeMapTrait {

Loading…
Cancel
Save