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::*;
use serde_json::error::Error as JsonError;
fn index(session: Session) -> Result<&'static str, JsonError> {
// 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
impl Session
Sourcepub fn get<T: DeserializeOwned>(
&self,
key: &str,
) -> Result<Option<T>, JsonError>
pub fn get<T: DeserializeOwned>( &self, key: &str, ) -> Result<Option<T>, JsonError>
Get a value from the session.
Sourcepub fn set<T: Serialize>(&self, key: &str, value: T) -> Result<(), JsonError>
pub fn set<T: Serialize>(&self, key: &str, value: T) -> Result<(), JsonError>
Set a value from the session.
pub fn set_session<St>( data: impl Iterator<Item = (String, String)>, req: &WebRequest<St>, )
pub fn get_changes( res: &mut WebResponse, ) -> (SessionStatus, Option<impl Iterator<Item = (String, String)> + use<>>)
Trait Implementations§
Source§impl<St> FromRequest<St> for Session
Extractor implementation for Session type.
impl<St> FromRequest<St> for Session
Extractor implementation for Session type.
use ntex_session::Session;
use serde_json::error::Error as JsonError;
fn index(session: Session) -> Result<&'static str, JsonError> {
// access session data
if let Some(count) = session.get::<i32>("counter")? {
session.set("counter", count + 1)?;
} else {
session.set("counter", 1)?;
}
Ok("Welcome!")
}Source§async fn from_request(
_: &St,
req: &HttpRequest,
_: &mut Payload,
) -> Result<Session, Infallible>
async fn from_request( _: &St, req: &HttpRequest, _: &mut Payload, ) -> Result<Session, Infallible>
Convert request to a Self
Auto Trait Implementations§
impl !RefUnwindSafe for Session
impl !Send for Session
impl !Sync for Session
impl !UnwindSafe for Session
impl Freeze for Session
impl Unpin for Session
impl UnsafeUnpin 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
Mutably borrows from an owned value. Read more