Skip to main content

SessionRegistry

Struct SessionRegistry 

Source
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>

Source

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", …).

Source

pub fn insert(&self, value: T) -> String

Insert a fresh session, returning its allocated id.

Source

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.

Source

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.

Source

pub fn remove(&self, id: &str) -> Option<T>

Remove a session by id. The dropped T is returned (if any).

Source

pub fn len(&self) -> usize

Number of currently-open sessions.

Source

pub fn is_empty(&self) -> bool

Whether the registry has no open sessions.

Auto Trait Implementations§

§

impl<T> !Freeze for SessionRegistry<T>

§

impl<T> !RefUnwindSafe for SessionRegistry<T>

§

impl<T> !Sync for SessionRegistry<T>

§

impl<T> Send for SessionRegistry<T>
where T: Send,

§

impl<T> Unpin for SessionRegistry<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for SessionRegistry<T>

§

impl<T> UnwindSafe for SessionRegistry<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.