pub use futures::lock::{Mutex, MutexGuard};
use std::cell::RefCell;
use std::collections::HashMap;
pub struct SessionMap<T> {
inner: RefCell<HashMap<String, T>>,
}
impl<T> Default for SessionMap<T> {
fn default() -> Self {
Self {
inner: RefCell::new(HashMap::new()),
}
}
}
impl<T: Default> SessionMap<T> {
pub fn with<R>(&self, session_id: &str, f: impl FnOnce(&mut T) -> R) -> R {
let mut map = self.inner.borrow_mut();
f(map.entry(session_id.to_owned()).or_default())
}
pub fn remove(&self, session_id: &str) {
self.inner.borrow_mut().remove(session_id);
}
}