pub struct Session(/* private fields */);
session
only.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.
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>>
pub fn get<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>>
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 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<()>
pub fn insert<T: Serialize>( &self, key: impl Into<String>, value: T, ) -> Result<()>
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.
It returns an error if it fails to serialize value
to JSON.
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
impl FromRequest for Session
Extractor implementation for Session
s.
Source§fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
Self
from request parts asynchronously.