Skip to main content

SessionHandle

Struct SessionHandle 

Source
pub struct SessionHandle(/* private fields */);
Expand description

The request’s session, shared between the middleware and the handler.

Request::extension hands out a shared reference, but the middleware has to read the session back after the handler has changed it — so the session lives behind a mutex and both sides hold the same handle. Cloning a handle is cloning an Arc; every clone is the same session.

Implementations§

Source§

impl SessionHandle

Source

pub fn new(session: Session) -> Self

Source

pub fn lock(&self) -> MutexGuard<'_, Session>

Borrow the session directly.

The guard is not Send, so holding it across an .await will not compile — which is the behaviour we want: a session held open across an await is a session held open across another request.

Source

pub fn with<R>(&self, action: impl FnOnce(&mut Session) -> R) -> R

Run a closure against the session, releasing the lock immediately.

Source

pub fn id(&self) -> String

Source

pub fn get(&self, key: &str) -> Option<Json>

Source

pub fn get_string(&self, key: &str) -> Option<String>

Source

pub fn has(&self, key: &str) -> bool

Source

pub fn put(&self, key: impl Into<String>, value: impl Into<Json>)

Source

pub fn forget(&self, key: &str) -> Option<Json>

Source

pub fn flush(&self)

Source

pub fn all(&self) -> BTreeMap<String, Json>

Source

pub fn flash(&self, key: impl Into<String>, value: impl Into<Json>)

Source

pub fn keep(&self, key: &str)

Source

pub fn reflash(&self)

Source

pub fn regenerate(&self) -> String

Rotate the id, keeping the contents. See Session::regenerate.

Source

pub fn invalidate(&self) -> String

Empty the session and rotate the id.

Source

pub fn token(&self) -> String

The CSRF token, generated on first use.

Trait Implementations§

Source§

impl Clone for SessionHandle

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SessionHandle

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Flash for SessionHandle

The session is where a flashed value lives.

flash writes with the session’s own one-request lifetime, so a value left here is gone after the next request whether or not anybody read it — which is what stops an old error message reappearing on a page three clicks later.

Source§

fn flash(&self, key: &str, value: Json)

Leave a value for the next request, and no longer.
Source§

fn take(&self, key: &str) -> Option<Json>

Read a value and consume it.
Source§

fn peek(&self, key: &str) -> Option<Json>

Read a value and leave it in place. Read more

Auto Trait Implementations§

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.