1use std::sync::Arc;
2
3use parking_lot::{MappedRwLockReadGuard, RwLock, RwLockReadGuard};
4use typemap_rev::{TypeMap, TypeMapKey};
5
6#[derive(Clone, Default)]
8pub struct BspContext {
9 inner: Arc<RwLock<TypeMap>>,
10}
11
12impl BspContext {
13 #[must_use]
15 pub fn new() -> Self {
16 Self {
17 inner: Arc::new(RwLock::new(TypeMap::new())),
18 }
19 }
20
21 pub fn insert<K: TypeMapKey>(&self, val: K::Value) {
23 self.inner.write().insert::<K>(val);
24 }
25
26 #[must_use]
28 pub fn get<K: TypeMapKey>(&self) -> Option<MappedRwLockReadGuard<'_, K::Value>> {
29 let guard = self.inner.read();
30 RwLockReadGuard::try_map(guard, typemap_rev::TypeMap::get::<K>).ok()
31 }
32}