use std::any::{Any, TypeId};
use std::collections::HashMap;
#[derive(Default)]
pub struct Extensions {
entries: HashMap<TypeId, Box<dyn Any + Send + Sync>>,
}
impl Extensions {
pub fn new() -> Self {
Self::default()
}
pub fn insert<T>(&mut self, value: T)
where
T: Any + Send + Sync + 'static,
{
self.entries.insert(TypeId::of::<T>(), Box::new(value));
}
pub fn get<T>(&self) -> Option<&T>
where
T: Any + Send + Sync + 'static,
{
self.entries
.get(&TypeId::of::<T>())
.and_then(|value| value.downcast_ref::<T>())
}
pub fn get_mut<T>(&mut self) -> Option<&mut T>
where
T: Any + Send + Sync + 'static,
{
self.entries
.get_mut(&TypeId::of::<T>())
.and_then(|value| value.downcast_mut::<T>())
}
}