diff --git a/.github/workflows/toolchain.yml b/.github/workflows/toolchain.yml index bc1cc99..9e4c188 100644 --- a/.github/workflows/toolchain.yml +++ b/.github/workflows/toolchain.yml @@ -66,14 +66,14 @@ jobs: args: --all-features test_minimum: - name: Test (1.70.0) + name: Test (1.75.0) runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions-rs/toolchain@v1 with: profile: minimal - toolchain: 1.70.0 + toolchain: 1.75.0 override: true - uses: actions-rs/cargo@v1 with: diff --git a/Cargo.toml b/Cargo.toml index 4e3df47..bcd8abe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "aerosol" -version = "1.0.0" +version = "1.1.0" authors = ["Diggory Blake "] edition = "2018" description = "Simple dependency injection for Rust" @@ -20,8 +20,8 @@ axum-extra = ["axum", "dep:axum-extra"] parking_lot = "0.12.1" anymap = { package = "anymap3", version = "1.0.0", features = ["hashbrown"] } async-trait = { version = "0.1", optional = true } -axum = { version = "0.7.5", optional = true } -axum-extra = { version = "0.9.3", optional = true, features = [ +axum = { version = "0.8.0", optional = true } +axum-extra = { version = "0.10.0", optional = true, features = [ "cookie-private", ] } tracing = { version = "0.1", optional = true } diff --git a/src/axum.rs b/src/axum.rs index 228db42..6ad0ed1 100644 --- a/src/axum.rs +++ b/src/axum.rs @@ -7,10 +7,10 @@ //! an `Aero`, or you must implement `FromRef` for `Aero`. use std::any::type_name; +use std::convert::Infallible; -use async_trait::async_trait; use axum::{ - extract::{FromRef, FromRequestParts}, + extract::{FromRef, FromRequestParts, OptionalFromRequestParts}, http::{request::Parts, StatusCode}, response::{IntoResponse, Response}, }; @@ -63,7 +63,6 @@ impl DependencyError { /// Get an already-existing resource from the state. Equivalent to calling `Aero::try_get_async`. pub struct Dep(pub T); -#[async_trait] impl FromRequestParts for Dep where Aero: FromRef, @@ -79,10 +78,28 @@ where } } +impl OptionalFromRequestParts for Dep +where + Aero: FromRef, +{ + type Rejection = Infallible; + + // Required method + async fn from_request_parts( + parts: &mut Parts, + state: &S, + ) -> Result, Self::Rejection> { + Ok( + >::from_request_parts(parts, state) + .await + .ok(), + ) + } +} + /// Get a resource from the state, or construct it if it doesn't exist. Equivalent to calling `Aero::try_obtain_async`. pub struct Obtain(pub T); -#[async_trait] impl FromRequestParts for Obtain where Aero: FromRef, @@ -98,6 +115,25 @@ where } } +impl OptionalFromRequestParts for Obtain +where + Aero: FromRef, +{ + type Rejection = Infallible; + + // Required method + async fn from_request_parts( + parts: &mut Parts, + state: &S, + ) -> Result, Self::Rejection> { + Ok( + >::from_request_parts(parts, state) + .await + .ok(), + ) + } +} + impl FromRef>> for Aero { fn from_ref(input: &Aero>) -> Self { input.clone().into()