pub struct Session(/* private fields */);Expand description
The primary interface to access and modify session state.
Session is an extractor—you can specify it as an input type for your
request handlers and it will be automatically extracted from the incoming request.
use actix_session::Session;
async fn index(session: Session) -> actix_web::Result<&'static str> {
    // access session data
    if let Some(count) = session.get::<i32>("counter")? {
        session.insert("counter", count + 1)?;
    } else {
        session.insert("counter", 1)?;
    }
    // or use the shorthand
    session.update_or("counter", 1, |count: i32| count + 1);
    Ok("Welcome!")
}You can also retrieve a Session object from an HttpRequest or a ServiceRequest using
SessionExt.
Implementations§
Source§impl Session
 
impl Session
Sourcepub fn get<T: DeserializeOwned>(
    &self,
    key: &str,
) -> Result<Option<T>, SessionGetError>
 
pub fn get<T: DeserializeOwned>( &self, key: &str, ) -> Result<Option<T>, SessionGetError>
Get a value from the session.
It returns an error if it fails to deserialize as T the JSON value associated with key.
Sourcepub fn contains_key(&self, key: &str) -> bool
 
pub fn contains_key(&self, key: &str) -> bool
Returns true if the session contains a value for the specified key.
Sourcepub fn entries(&self) -> Ref<'_, HashMap<String, String>>
 
pub fn entries(&self) -> Ref<'_, HashMap<String, String>>
Get all raw key-value data from the session.
Note that values are JSON encoded.
Sourcepub fn status(&self) -> SessionStatus
 
pub fn status(&self) -> SessionStatus
Returns session status.
Sourcepub fn insert<T: Serialize>(
    &self,
    key: impl Into<String>,
    value: T,
) -> Result<(), SessionInsertError>
 
pub fn insert<T: Serialize>( &self, key: impl Into<String>, value: T, ) -> Result<(), SessionInsertError>
Inserts a key-value pair into the session.
Any serializable value can be used and will be encoded as JSON in session data, hence why only a reference to the value is taken.
§Errors
Returns an error if JSON serialization of value fails.
Sourcepub fn update<T: Serialize + DeserializeOwned, F>(
    &self,
    key: impl Into<String>,
    updater: F,
) -> Result<(), SessionUpdateError>where
    F: FnOnce(T) -> T,
 
pub fn update<T: Serialize + DeserializeOwned, F>(
    &self,
    key: impl Into<String>,
    updater: F,
) -> Result<(), SessionUpdateError>where
    F: FnOnce(T) -> T,
Updates a key-value pair into the session.
If the key exists then update it to the new value and place it back in. If the key does not exist it will not be updated.
Any serializable value can be used and will be encoded as JSON in the session data, hence why only a reference to the value is taken.
§Errors
Returns an error if JSON serialization of the value fails.
Sourcepub fn update_or<T: Serialize + DeserializeOwned, F>(
    &self,
    key: &str,
    default_value: T,
    updater: F,
) -> Result<(), SessionUpdateError>where
    F: FnOnce(T) -> T,
 
pub fn update_or<T: Serialize + DeserializeOwned, F>(
    &self,
    key: &str,
    default_value: T,
    updater: F,
) -> Result<(), SessionUpdateError>where
    F: FnOnce(T) -> T,
Updates a key-value pair into the session, or inserts a default value.
If the key exists then update it to the new value and place it back in. If the key does not exist the default value will be inserted instead.
Any serializable value can be used and will be encoded as JSON in session data, hence why only a reference to the value is taken.
§Errors
Returns error if JSON serialization of a value fails.
Sourcepub fn remove(&self, key: &str) -> Option<String>
 
pub fn remove(&self, key: &str) -> Option<String>
Remove value from the session.
If present, the JSON encoded value is returned.
Trait Implementations§
Source§impl FromRequest for Session
Extractor implementation for Sessions.
 
impl FromRequest for Session
Extractor implementation for Sessions.
§Examples
use actix_session::Session;
#[get("/")]
async fn index(session: Session) -> Result<impl Responder> {
    // access session data
    if let Some(count) = session.get::<i32>("counter")? {
        session.insert("counter", count + 1)?;
    } else {
        session.insert("counter", 1)?;
    }
    let count = session.get::<i32>("counter")?.unwrap();
    Ok(format!("Counter: {}", count))
}Source§fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
 
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
Self from request parts asynchronously.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> 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
Source§impl<T> CloneToUninit for Twhere
    T: Clone,
 
impl<T> CloneToUninit for Twhere
    T: Clone,
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