Skip to main content

Session

Struct Session 

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

The current visitor’s session, stored in an encrypted cookie.

Values put in the session last until it expires. Flashed values are readable during the next request only, which is how “saved!” messages and old form input survive a redirect.

Implementations§

Source§

impl Session

Source

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

Reads a value, including values flashed by the previous request.

Source

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

Whether key is set, including values flashed by the previous request.

Source

pub fn put(&self, key: &str, value: impl Serialize) -> Result

Stores a value under key.

Source

pub fn remove(&self, key: &str) -> Option<Value>

Removes key and returns its value, if it was set.

Source

pub fn push(&self, key: &str, value: impl Serialize) -> Result<usize>

Appends value to the list stored under key (a new list if there is none; a value that isn’t a list becomes its first item), e.g. the recently viewed products. Returns the list’s new length.

session.push("recent", 42)?;
let views = session.increment("views", 1)?; // 1, then 2, …
Source

pub fn increment(&self, key: &str, by: i64) -> Result<i64>

Adds by (which may be negative) to the number under key, taking a missing or non-numeric value as 0, and returns the new number.

Source

pub fn pull<T: DeserializeOwned>(&self, key: &str) -> Option<T>

Reads a value and removes it.

Source

pub fn set_lifetime(&self, lifetime: Duration)

Keeps this session for lifetime of inactivity instead of SESSION_LIFETIME, e.g. for “remember me” (whole minutes, at least one).

Source

pub fn flash(&self, key: &str, value: impl Serialize) -> Result

Stores a value for the next request only.

Source

pub fn reflash(&self)

Keeps the values flashed by the previous request for one more request.

Source

pub fn keep(&self, keys: &[&str])

Keeps only these of the values flashed by the previous request for one more request (Laravel’s keep), e.g. a status message across a second redirect.

Source

pub fn flash_now(&self, key: &str, value: impl Serialize) -> Result

A flash value for this request only (Laravel’s flash()->now()): the page rendered now sees it in flash, the next request doesn’t. For a handler that renders a page instead of redirecting.

Source

pub fn flash_input(&self, input: &impl Serialize) -> Result

Flashes the submitted form so the next page can refill it with old(). Passwords and Renox’s own fields (_token, _method) are never kept, and the largest values are dropped when the rest wouldn’t fit in the cookie (browsers drop cookies over 4 KB).

Source

pub fn old(&self, field: &str) -> Option<Value>

A field from the input flashed by the previous request. field may be a nested name (items[0][name] or items.0.name) of a form that used them.

Source

pub fn has_old_input(&self) -> bool

Whether the previous request flashed its input (a failed submit), even with no field in it: an unticked checkbox sends nothing, so old() alone can’t tell “unticked” from “not submitted yet”.

Source

pub fn flash_errors(&self, errors: &impl Serialize) -> Result

Flashes validation errors, keyed by field, for the next request.

Source

pub fn flash_errors_in(&self, bag: &str, errors: &impl Serialize) -> Result

Flashes validation errors for the next request in a named bag (Laravel’s error bags), e.g. login when a page has two forms; templates read them with error('email', bag='login').

Source

pub fn errors(&self) -> Map<String, Value>

Validation errors flashed by the previous request, in the default bag (errors flashed in a named bag aren’t here; see errors_in).

Source

pub fn errors_in(&self, bag: &str) -> Map<String, Value>

Validation errors flashed by the previous request in the bag bag.

Source

pub fn error_bag(&self) -> Option<String>

The bag the previous request’s errors were flashed in, if not the default.

Source

pub fn flashed(&self) -> Map<String, Value>

Values flashed by the previous request, except old input and errors.

Source

pub fn token(&self) -> String

The CSRF token forms and HTMX requests must send back.

Source

pub fn regenerate_token(&self)

Issues a new CSRF token, e.g. after logging in.

Source

pub fn flush(&self)

Removes all data and flashed values and issues a new CSRF token.

Source§

impl Session

Source

pub async fn prune_expired(db: &Db) -> Result<u64>

Deletes expired rows of the sessions table (SESSION_DRIVER=database) and returns how many; my-app session:prune runs it. Requests also prune now and then, so this is for a quiet app or a schedule.

Trait Implementations§

Source§

impl Clone for Session

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<S: Send + Sync> FromRequestParts<S> for Session

Source§

type Rejection = Error

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
Source§

async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self>

Perform the extraction.

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<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> 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> Fake for T

Source§

fn fake<U>(&self) -> U
where Self: FakeBase<U>,

Source§

fn fake_with_rng<U, R>(&self, rng: &mut R) -> U
where R: RngExt + ?Sized, Self: FakeBase<U>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<S, T> FromRequest<S, ViaParts> for T
where S: Send + Sync, T: FromRequestParts<S>,

Source§

type Rejection = <T as FromRequestParts<S>>::Rejection

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
Source§

fn from_request( req: Request<Body>, state: &S, ) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>

Perform the extraction.
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

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

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more