use std::{
any::{Any, TypeId},
collections::HashMap,
hash::{BuildHasherDefault, Hasher},
};
#[derive(Debug, Default)]
struct TypeIdHasher(u64);
impl Hasher for TypeIdHasher {
fn write(&mut self, bytes: &[u8]) {
unimplemented!("This TypeIdHasher can only handle u64s, not {:?}", bytes);
}
fn write_u64(&mut self, i: u64) {
self.0 = i;
}
fn finish(&self) -> u64 {
self.0
}
}
#[derive(Debug, Default)]
pub struct TypeStore {
map: HashMap<TypeId, Box<dyn Any>, BuildHasherDefault<TypeIdHasher>>,
}
impl TypeStore {
#[inline]
pub fn new() -> Self {
Self {
map: HashMap::default(),
}
}
#[inline]
pub fn insert<T: 'static>(&mut self, val: T) {
self.map.insert(TypeId::of::<T>(), Box::new(val));
}
#[inline]
pub fn get<T: 'static>(&self) -> Option<&T> {
self.map
.get(&TypeId::of::<T>())
.and_then(|v| v.downcast_ref::<T>())
}
#[inline]
pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T> {
self.map
.get_mut(&TypeId::of::<T>())
.and_then(|v| v.downcast_mut::<T>())
}
#[inline]
pub fn remove<T: 'static>(&mut self) -> Option<T> {
self.map
.remove(&TypeId::of::<T>())
.and_then(|v| v.downcast::<T>().ok())
.map(|v| *v)
}
#[inline]
pub fn contains<T: 'static>(&self) -> bool {
self.map.contains_key(&TypeId::of::<T>())
}
#[inline]
pub fn clear(&mut self) {
self.map.clear();
}
#[inline]
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
}