|
|
|
@ -1,4 +1,3 @@
|
|
|
|
|
use multi_trait_object::MultitraitObject;
|
|
|
|
|
use std::any::TypeId;
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
|
@ -6,32 +5,38 @@ use std::collections::HashMap;
|
|
|
|
|
/// Each other typemap is just a newtype of this base type with
|
|
|
|
|
/// additional implementation.
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
|
pub(crate) struct TypeMapBase(pub(crate) HashMap<TypeId, MultitraitObject>);
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub(crate) struct TypeIndexedMap<T>(pub(crate) HashMap<TypeId, T>);
|
|
|
|
|
|
|
|
|
|
impl TypeMapBase {
|
|
|
|
|
impl<T> Default for TypeIndexedMap<T> {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self(HashMap::default())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> TypeIndexedMap<T> {
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
Self::default()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn insert<K: 'static>(&mut self, value: MultitraitObject) {
|
|
|
|
|
pub fn insert<K: 'static>(&mut self, value: T) {
|
|
|
|
|
self.0.insert(TypeId::of::<K>(), value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn get<K: 'static>(&self) -> Option<&MultitraitObject> {
|
|
|
|
|
pub fn get<K: 'static>(&self) -> Option<&T> {
|
|
|
|
|
self.0.get(&TypeId::of::<K>())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn get_mut<K: 'static>(&mut self) -> Option<&mut MultitraitObject> {
|
|
|
|
|
pub fn get_mut<K: 'static>(&mut self) -> Option<&mut T> {
|
|
|
|
|
self.0.get_mut(&TypeId::of::<K>())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn remove<K: 'static>(&mut self) -> Option<MultitraitObject> {
|
|
|
|
|
pub fn remove<K: 'static>(&mut self) -> Option<T> {
|
|
|
|
|
self.0.remove(&TypeId::of::<K>())
|
|
|
|
|
}
|
|
|
|
|
|