Struct actix_session::Session
source · [−]pub struct Session(_);
Expand description
The high-level interface you use to modify session data.
Session object is obtained with UserSession::get_session
. The UserSession
trait is
implemented for HttpRequest
, ServiceRequest
, and RequestHead
.
use actix_session::Session;
use actix_web::Result;
async fn index(session: Session) -> Result<&'static str> {
// access session data
if let Some(count) = session.get::<i32>("counter")? {
session.insert("counter", count + 1)?;
} else {
session.insert("counter", 1)?;
}
Ok("Welcome!")
}
Implementations
sourceimpl Session
impl Session
sourcepub fn get<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>, Error>
pub fn get<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>, Error>
Get a value
from the session.
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 insert(
&self,
key: impl Into<String>,
value: impl Serialize
) -> Result<(), Error>
pub fn insert(
&self,
key: impl Into<String>,
value: impl Serialize
) -> Result<(), Error>
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.
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.
sourcepub fn remove_as<T: DeserializeOwned>(
&self,
key: &str
) -> Option<Result<T, String>>
pub fn remove_as<T: DeserializeOwned>(
&self,
key: &str
) -> Option<Result<T, String>>
Remove value from the session and deserialize.
Returns None if key was not present in session. Returns T if deserialization succeeds, otherwise returns un-deserialized JSON string.
sourcepub fn set_session(
req: &mut ServiceRequest,
data: impl IntoIterator<Item = (String, String)>
)
pub fn set_session(
req: &mut ServiceRequest,
data: impl IntoIterator<Item = (String, String)>
)
Adds the given key-value pairs to the session on the request.
Values that match keys already existing on the session will be overwritten. Values should already be JSON serialized.
Examples
let mut req = test::TestRequest::default().to_srv_request();
Session::set_session(
&mut req,
vec![("counter".to_string(), serde_json::to_string(&0).unwrap())],
);
sourcepub fn get_changes<B>(
res: &mut ServiceResponse<B>
) -> (SessionStatus, impl Iterator<Item = (String, String)>)
pub fn get_changes<B>(
res: &mut ServiceResponse<B>
) -> (SessionStatus, impl Iterator<Item = (String, String)>)
Returns session status and iterator of key-value pairs of changes.
Trait Implementations
sourceimpl FromRequest for Session
impl FromRequest for Session
Extractor implementation for Session type.
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))
}
type Error = Error
type Error = Error
The associated error which can be returned.
sourcefn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
Create a Self from request parts asynchronously.
Auto Trait Implementations
impl !RefUnwindSafe for Session
impl !Send for Session
impl !Sync for Session
impl Unpin for Session
impl !UnwindSafe for Session
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
pub fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more