use std::any::TypeId; use std::collections::hash_map::IntoIter; use std::collections::HashMap; /// Base typemap used for implementation but not elsewhere /// Each other typemap is just a newtype of this base type with /// additional implementation. #[doc(hidden)] #[derive(Debug)] pub(crate) struct TypeIndexedMap(pub(crate) HashMap); impl Default for TypeIndexedMap { fn default() -> Self { Self(HashMap::default()) } } impl TypeIndexedMap { #[inline] pub fn new() -> Self { Self::default() } #[inline] pub fn insert(&mut self, value: T) { self.0.insert(TypeId::of::(), value); } #[inline] pub fn get(&self) -> Option<&T> { self.0.get(&TypeId::of::()) } #[inline] pub fn get_mut(&mut self) -> Option<&mut T> { self.0.get_mut(&TypeId::of::()) } #[inline] pub fn remove(&mut self) -> Option { self.0.remove(&TypeId::of::()) } #[inline] pub fn contains_key(&self) -> bool { self.0.contains_key(&TypeId::of::()) } } impl IntoIterator for TypeIndexedMap { type Item = (TypeId, T); type IntoIter = IntoIter; fn into_iter(self) -> Self::IntoIter { self.0.into_iter() } } impl Extend<(TypeId, T)> for TypeIndexedMap { fn extend>(&mut self, iter: I) { self.0.extend(iter) } }