pub struct SessionRegistry<T> { /* private fields */ }Expand description
Per-component map of session-id → user state T.
T is whatever the component wants to keep around for a session
(a database connection, a counter, a parsed config, …). T is
stored by value; if it owns expensive resources, drop in
SessionRegistry::remove runs your Drop impl.
Ids are allocated as "<prefix>_<counter>" where prefix is set
at construction. Components SHOULD use a short, recognisable
prefix; per-component uniqueness is what matters since the host
scopes session-ids to one component instance.
Implementations§
Source§impl<T> SessionRegistry<T>
impl<T> SessionRegistry<T>
Sourcepub fn new(prefix: &'static str) -> Self
pub fn new(prefix: &'static str) -> Self
Create an empty registry. prefix becomes part of every
allocated session-id (e.g. "ctr" → "ctr_0", "ctr_1", …).
Sourcepub fn with<R>(&self, id: &str, f: impl FnOnce(&T) -> R) -> Option<R>
pub fn with<R>(&self, id: &str, f: impl FnOnce(&T) -> R) -> Option<R>
Look up a session by id and apply f to a shared reference.
Returns None if the id is unknown.
Sourcepub fn with_mut<R>(&self, id: &str, f: impl FnOnce(&mut T) -> R) -> Option<R>
pub fn with_mut<R>(&self, id: &str, f: impl FnOnce(&mut T) -> R) -> Option<R>
Look up a session by id and apply f to a mutable reference.
Returns None if the id is unknown.