Struct actix_session::Session [−][src]
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
Get a value
from the session.
Get all raw key-value data from the session.
Note that values are JSON encoded.
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.
Remove value from the session.
If present, the JSON encoded value is returned.
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.
pub 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())],
);
pub 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
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))
}
Auto Trait Implementations
impl !RefUnwindSafe for Session
impl !UnwindSafe for Session
Blanket Implementations
Mutably borrows from an owned value. Read more
pub fn vzip(self) -> V
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more