use std::{
any::{Any, TypeId},
collections::HashMap,
hash::{BuildHasherDefault, Hasher},
};
pub struct Extensions {
map: Option<AnyMap>,
}
impl Extensions {
pub const fn new() -> Extensions {
Extensions { map: None }
}
pub fn has<T: Send + Sync + 'static>(&self) -> bool {
self.map
.as_ref()
.is_some_and(|map| map.contains_key(&TypeId::of::<T>()))
}
pub fn get<T: Send + Sync + 'static>(&self) -> Option<&T> {
self.map
.as_ref()?
.get(&TypeId::of::<T>())
.and_then(|any| any.downcast_ref::<T>())
}
pub fn get_mut<T: Send + Sync + 'static>(&mut self) -> Option<&mut T> {
self.map
.as_mut()?
.get_mut(&TypeId::of::<T>())
.and_then(|any| any.downcast_mut::<T>())
}
pub fn insert<T: Send + Sync + 'static>(&mut self, value: T) -> Option<T> {
self.map
.get_or_insert_default()
.insert(TypeId::of::<T>(), Box::new(value))
.and_then(|any| any.downcast().ok())
.map(|boxed| *boxed)
}
pub fn remove<T: Send + Sync + 'static>(&mut self) -> Option<T> {
self.map
.as_mut()?
.remove(&TypeId::of::<T>())
.and_then(|any| any.downcast().ok())
.map(|boxed| *boxed)
}
}
impl Default for Extensions {
fn default() -> Self {
Self::new()
}
}
type AnyMap = HashMap<TypeId, Box<dyn Any + Send + Sync>, BuildHasherDefault<IdHasher>>;
#[derive(Debug, Default)]
struct IdHasher(u64);
impl Hasher for IdHasher {
fn finish(&self) -> u64 {
self.0
}
fn write(&mut self, _bytes: &[u8]) {
unreachable!("TypeId calls write_u64")
}
#[inline]
fn write_u64(&mut self, i: u64) {
self.0 = i;
}
}