1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-06-26 18:37:41 +02:00

feat(session): use real async traits (#365)

This commit is contained in:
Rob Ede
2024-01-04 04:10:46 +00:00
committed by GitHub
parent 77b8dcdf59
commit e2bf504055
16 changed files with 36 additions and 28 deletions

View File

@ -49,7 +49,6 @@ use crate::storage::{
#[non_exhaustive]
pub struct CookieSessionStore;
#[async_trait::async_trait(?Send)]
impl SessionStore for CookieSessionStore {
async fn load(&self, session_key: &SessionKey) -> Result<Option<SessionState>, LoadError> {
serde_json::from_str(session_key.as_ref())
@ -67,10 +66,10 @@ impl SessionStore for CookieSessionStore {
.map_err(anyhow::Error::new)
.map_err(SaveError::Serialization)?;
Ok(session_key
session_key
.try_into()
.map_err(Into::into)
.map_err(SaveError::Other)?)
.map_err(SaveError::Other)
}
async fn update(

View File

@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::{collections::HashMap, future::Future};
use actix_web::cookie::time::Duration;
use derive_more::Display;
@ -10,41 +10,39 @@ pub(crate) type SessionState = HashMap<String, String>;
/// The interface to retrieve and save the current session data from/to the chosen storage backend.
///
/// You can provide your own custom session store backend by implementing this trait.
///
/// [`async-trait`](https://docs.rs/async-trait) is used for this trait's definition. Therefore, it
/// is required for implementations, too. In particular, we use the send-optional variant:
/// `#[async_trait(?Send)]`.
#[async_trait::async_trait(?Send)]
pub trait SessionStore {
/// Loads the session state associated to a session key.
async fn load(&self, session_key: &SessionKey) -> Result<Option<SessionState>, LoadError>;
fn load(
&self,
session_key: &SessionKey,
) -> impl Future<Output = Result<Option<SessionState>, LoadError>>;
/// Persist the session state for a newly created session.
///
/// Returns the corresponding session key.
async fn save(
fn save(
&self,
session_state: SessionState,
ttl: &Duration,
) -> Result<SessionKey, SaveError>;
) -> impl Future<Output = Result<SessionKey, SaveError>>;
/// Updates the session state associated to a pre-existing session key.
async fn update(
fn update(
&self,
session_key: SessionKey,
session_state: SessionState,
ttl: &Duration,
) -> Result<SessionKey, UpdateError>;
) -> impl Future<Output = Result<SessionKey, UpdateError>>;
/// Updates the TTL of the session state associated to a pre-existing session key.
async fn update_ttl(
fn update_ttl(
&self,
session_key: &SessionKey,
ttl: &Duration,
) -> Result<(), anyhow::Error>;
) -> impl Future<Output = Result<(), anyhow::Error>>;
/// Deletes a session from the store.
async fn delete(&self, session_key: &SessionKey) -> Result<(), anyhow::Error>;
fn delete(&self, session_key: &SessionKey) -> impl Future<Output = Result<(), anyhow::Error>>;
}
// We cannot derive the `Error` implementation using `derive_more` for our custom errors:

View File

@ -118,7 +118,6 @@ impl RedisActorSessionStoreBuilder {
}
}
#[async_trait::async_trait(?Send)]
impl SessionStore for RedisActorSessionStore {
async fn load(&self, session_key: &SessionKey) -> Result<Option<SessionState>, LoadError> {
let cache_key = (self.configuration.cache_keygen)(session_key.as_ref());

View File

@ -132,7 +132,6 @@ impl RedisSessionStoreBuilder {
}
}
#[async_trait::async_trait(?Send)]
impl SessionStore for RedisSessionStore {
async fn load(&self, session_key: &SessionKey) -> Result<Option<SessionState>, LoadError> {
let cache_key = (self.configuration.cache_keygen)(session_key.as_ref());