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
impl Session
Sourcepub fn get<T: DeserializeOwned>(&self, key: &str) -> Option<T>
pub fn get<T: DeserializeOwned>(&self, key: &str) -> Option<T>
Reads a value, including values flashed by the previous request.
Sourcepub fn has(&self, key: &str) -> bool
pub fn has(&self, key: &str) -> bool
Whether key is set, including values flashed by the previous request.
Sourcepub fn remove(&self, key: &str) -> Option<Value>
pub fn remove(&self, key: &str) -> Option<Value>
Removes key and returns its value, if it was set.
Sourcepub fn push(&self, key: &str, value: impl Serialize) -> Result<usize>
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, …Sourcepub fn increment(&self, key: &str, by: i64) -> Result<i64>
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.
Sourcepub fn pull<T: DeserializeOwned>(&self, key: &str) -> Option<T>
pub fn pull<T: DeserializeOwned>(&self, key: &str) -> Option<T>
Reads a value and removes it.
Sourcepub fn set_lifetime(&self, lifetime: Duration)
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).
Sourcepub fn flash(&self, key: &str, value: impl Serialize) -> Result
pub fn flash(&self, key: &str, value: impl Serialize) -> Result
Stores a value for the next request only.
Sourcepub fn keep(&self, keys: &[&str])
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.
Sourcepub fn flash_now(&self, key: &str, value: impl Serialize) -> Result
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.
Sourcepub fn flash_input(&self, input: &impl Serialize) -> Result
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).
Sourcepub fn old(&self, field: &str) -> Option<Value>
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.
Sourcepub fn has_old_input(&self) -> bool
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”.
Sourcepub fn flash_errors(&self, errors: &impl Serialize) -> Result
pub fn flash_errors(&self, errors: &impl Serialize) -> Result
Flashes validation errors, keyed by field, for the next request.
Sourcepub fn flash_errors_in(&self, bag: &str, errors: &impl Serialize) -> Result
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').
Sourcepub fn errors(&self) -> Map<String, Value>
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).
Sourcepub fn errors_in(&self, bag: &str) -> Map<String, Value>
pub fn errors_in(&self, bag: &str) -> Map<String, Value>
Validation errors flashed by the previous request in the bag bag.
Sourcepub fn error_bag(&self) -> Option<String>
pub fn error_bag(&self) -> Option<String>
The bag the previous request’s errors were flashed in, if not the default.
Sourcepub fn flashed(&self) -> Map<String, Value>
pub fn flashed(&self) -> Map<String, Value>
Values flashed by the previous request, except old input and errors.
Sourcepub fn regenerate_token(&self)
pub fn regenerate_token(&self)
Issues a new CSRF token, e.g. after logging in.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Session
impl RefUnwindSafe for Session
impl Send for Session
impl Sync for Session
impl Unpin for Session
impl UnsafeUnpin for Session
impl UnwindSafe for Session
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<S, T> FromRequest<S, ViaParts> for T
impl<S, T> FromRequest<S, ViaParts> for T
Source§type Rejection = <T as FromRequestParts<S>>::Rejection
type Rejection = <T as FromRequestParts<S>>::Rejection
Source§fn from_request(
req: Request<Body>,
state: &S,
) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>
fn from_request( req: Request<Body>, state: &S, ) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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