Skip to content

Commit

Permalink
feat(session): use real async traits (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede authored Jan 4, 2024
1 parent 77b8dcd commit e2bf504
Show file tree
Hide file tree
Showing 16 changed files with 36 additions and 28 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
target:
- { name: Linux, os: ubuntu-latest, triple: x86_64-unknown-linux-gnu }
version:
- { name: msrv, version: 1.68.0 }
- { name: msrv, version: 1.75.0 }
- { name: stable, version: stable }

name: ${{ matrix.target.name }} / ${{ matrix.version.name }}
Expand Down Expand Up @@ -83,7 +83,7 @@ jobs:
- { name: macOS, os: macos-latest, triple: x86_64-apple-darwin }
- { name: Windows, os: windows-latest, triple: x86_64-pc-windows-msvc }
version:
- { name: msrv, version: 1.68.0 }
- { name: msrv, version: 1.75.0 }
- { name: stable, version: stable }

name: ${{ matrix.target.name }} / ${{ matrix.version.name }}
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ members = [
]

[workspace.package]
homepage = "https://actix.rs"
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.68"
rust-version = "1.75"

[patch.crates-io]
actix-cors = { path = "./actix-cors" }
Expand Down
1 change: 1 addition & 0 deletions actix-cors/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- `Cors` is now marked `#[must_use]`.
- Minimum supported Rust version (MSRV) is now 1.75.

## 0.6.5

Expand Down
2 changes: 2 additions & 0 deletions actix-identity/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Minimum supported Rust version (MSRV) is now 1.75.

## 0.6.0

- Add `error` module.
Expand Down
2 changes: 2 additions & 0 deletions actix-protobuf/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Minimum supported Rust version (MSRV) is now 1.75.

## 0.10.0

- Updated `prost` dependency to `0.12`.
Expand Down
2 changes: 2 additions & 0 deletions actix-redis/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Minimum supported Rust version (MSRV) is now 1.75.

## 0.13.0

- Update `redis-async` dependency to `0.16`.
Expand Down
3 changes: 3 additions & 0 deletions actix-session/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- Remove use of `async-trait` on `SessionStore` trait.
- Minimum supported Rust version (MSRV) is now 1.75.

## 0.8.0

- Set secure attribute when adding a session removal cookie.
Expand Down
5 changes: 2 additions & 3 deletions actix-session/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ authors = [
]
description = "Session management for Actix Web"
keywords = ["http", "web", "framework", "async", "session"]
homepage = "https://actix.rs"
repository = "https://github.com/actix/actix-extras.git"
repository = "https://github.com/actix/actix-extras/tree/master/actix-session"
homepage.workspace = true
license.workspace = true
edition.workspace = true
rust-version.workspace = true
Expand All @@ -30,7 +30,6 @@ actix-utils = "3"
actix-web = { version = "4", default-features = false, features = ["cookies", "secure-cookies"] }

anyhow = "1"
async-trait = "0.1"
derive_more = "0.99.7"
rand = { version = "0.8", optional = true }
serde = { version = "1" }
Expand Down
5 changes: 2 additions & 3 deletions actix-session/src/storage/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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(
Expand Down
26 changes: 12 additions & 14 deletions actix-session/src/storage/interface.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::{collections::HashMap, future::Future};

use actix_web::cookie::time::Duration;
use derive_more::Display;
Expand All @@ -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:
Expand Down
1 change: 0 additions & 1 deletion actix-session/src/storage/redis_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
1 change: 0 additions & 1 deletion actix-session/src/storage/redis_rs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
1 change: 0 additions & 1 deletion actix-session/tests/opaque_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ async fn errors_are_opaque() {

struct MockStore;

#[async_trait::async_trait(?Send)]
impl SessionStore for MockStore {
async fn load(
&self,
Expand Down
2 changes: 2 additions & 0 deletions actix-settings/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Minimum supported Rust version (MSRV) is now 1.75.

## 0.7.1

- Fix doc examples.
Expand Down
2 changes: 2 additions & 0 deletions actix-web-httpauth/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Minimum supported Rust version (MSRV) is now 1.75.

## 0.8.1

- Implement `From<Basic>` for `BasicAuth`.
Expand Down
4 changes: 2 additions & 2 deletions actix-ws/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
## Documentation & Resources

- [API Documentation](https://docs.rs/actix-ws)
- [Example Projects](https://github.com/actix/examples/tree/master/websockets)
- Minimum Supported Rust Version (MSRV): 1.68
- [Example Chat Project](https://github.com/actix/examples/tree/master/websockets/chat-actorless)
- Minimum Supported Rust Version (MSRV): 1.75

## Usage

Expand Down

0 comments on commit e2bf504

Please sign in to comment.