You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.1 KiB
Rust

use std::any::TypeId;
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<T>(pub(crate) HashMap<TypeId, T>);
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: T) {
self.0.insert(TypeId::of::<K>(), value);
}
#[inline]
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 T> {
self.0.get_mut(&TypeId::of::<K>())
}
#[inline]
pub fn remove<K: 'static>(&mut self) -> Option<T> {
self.0.remove(&TypeId::of::<K>())
}
#[inline]
pub fn contains_key<K: 'static>(&self) -> bool {
self.0.contains_key(&TypeId::of::<K>())
}
}