use std::sync::Arc;
use parking_lot::{MappedRwLockReadGuard, RwLock, RwLockReadGuard};
use typemap_rev::{TypeMap, TypeMapKey};
#[derive(Clone, Default)]
pub struct BspContext {
inner: Arc<RwLock<TypeMap>>,
}
impl BspContext {
#[must_use]
pub fn new() -> Self {
Self {
inner: Arc::new(RwLock::new(TypeMap::new())),
}
}
pub fn insert<K: TypeMapKey>(&self, val: K::Value) {
self.inner.write().insert::<K>(val);
}
#[must_use]
pub fn get<K: TypeMapKey>(&self) -> Option<MappedRwLockReadGuard<'_, K::Value>> {
let guard = self.inner.read();
RwLockReadGuard::try_map(guard, typemap_rev::TypeMap::get::<K>).ok()
}
}