use std::{ any::{Any, TypeId}, collections::HashMap, }; #[derive(Default, Debug)] pub struct ModuleDataMap { map: HashMap>, } impl ModuleDataMap { #[inline] pub fn new() -> ModuleDataMap { ModuleDataMap { map: HashMap::default(), } } pub fn insert(&mut self, val: T) -> Option where T: 'static + Send + Sync, { self.map .insert(TypeId::of::(), Box::new(val)) .and_then(downcast_owned) } pub fn remove(&mut self) -> Option where T: 'static + Send + Sync, { self.map.remove(&TypeId::of::()).and_then(downcast_owned) } pub fn get(&self) -> Option<&T> where T: 'static + Send + Sync, { self.map.get(&TypeId::of::()).and_then(|boxed| boxed.downcast_ref()) } pub fn get_mut(&mut self) -> Option<&mut T> where T: 'static + Send + Sync, { self.map .get_mut(&TypeId::of::()) .and_then(|boxed| boxed.downcast_mut()) } pub fn contains(&self) -> bool where T: 'static + Send + Sync, { self.map.contains_key(&TypeId::of::()) } pub fn extend(&mut self, other: ModuleDataMap) { self.map.extend(other.map); } } fn downcast_owned(boxed: Box) -> Option { boxed.downcast().ok().map(|boxed| *boxed) }