Session

Struct Session 

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

The high-level interface you use to modify session data.

Session object could be obtained with RequestSession::session method. RequestSession trait is implemented for HttpRequest.

use ntex_session::Session;
use ntex::web::*;

fn index(session: Session) -> Result<&'static str, Error> {
    // access session data
    if let Some(count) = session.get::<i32>("counter")? {
        session.set("counter", count + 1)?;
    } else {
        session.set("counter", 1)?;
    }

    Ok("Welcome!")
}

Implementations§

Source§

impl Session

Source

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

Get a value from the session.

Source

pub fn set<T: Serialize>(&self, key: &str, value: T) -> Result<(), Error>

Set a value from the session.

Source

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

Remove value from the session.

Source

pub fn clear(&self)

Clear the session.

Source

pub fn purge(&self)

Removes session, both client and server side.

Source

pub fn renew(&self)

Renews the session key, assigning existing session state to new key.

Source

pub fn set_session<Err>( data: impl Iterator<Item = (String, String)>, req: &WebRequest<Err>, )

Source

pub fn get_changes( res: &mut WebResponse, ) -> (SessionStatus, Option<impl Iterator<Item = (String, String)> + use<>>)

Trait Implementations§

Source§

impl<Err> FromRequest<Err> for Session

Extractor implementation for Session type.

use ntex_session::Session;

fn index(session: Session) -> Result<&'static str, ntex::web::Error> {
    // access session data
    if let Some(count) = session.get::<i32>("counter")? {
        session.set("counter", count + 1)?;
    } else {
        session.set("counter", 1)?;
    }

    Ok("Welcome!")
}
Source§

type Error = Infallible

The associated error which can be returned.
Source§

async fn from_request( req: &HttpRequest, _: &mut Payload, ) -> Result<Session, Infallible>

Convert request to a Self

Auto Trait Implementations§

§

impl Freeze for Session

§

impl !RefUnwindSafe for Session

§

impl !Send for Session

§

impl !Sync for Session

§

impl Unpin for Session

§

impl !UnwindSafe for Session

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

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

Source§

fn vzip(self) -> V