Skip to content

Commit

Permalink
Merge pull request #6 from jac32/jc/dep/upgrade-axum-to-0.8
Browse files Browse the repository at this point in the history
Upgrade axum to 0.8
  • Loading branch information
Diggsey authored Jan 7, 2025
2 parents 698dfa3 + b600012 commit 2b304f7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/toolchain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aerosol"
version = "1.0.0"
version = "1.1.0"
authors = ["Diggory Blake <diggsey@googlemail.com>"]
edition = "2018"
description = "Simple dependency injection for Rust"
Expand All @@ -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 }
Expand Down
44 changes: 40 additions & 4 deletions src/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
//! an `Aero`, or you must implement `FromRef<YourState>` 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},
};
Expand Down Expand Up @@ -63,7 +63,6 @@ impl DependencyError {
/// Get an already-existing resource from the state. Equivalent to calling `Aero::try_get_async`.
pub struct Dep<T: Resource>(pub T);

#[async_trait]
impl<T: Resource, S: Send + Sync> FromRequestParts<S> for Dep<T>
where
Aero: FromRef<S>,
Expand All @@ -79,10 +78,28 @@ where
}
}

impl<T: Resource, S: Send + Sync> OptionalFromRequestParts<S> for Dep<T>
where
Aero: FromRef<S>,
{
type Rejection = Infallible;

// Required method
async fn from_request_parts(
parts: &mut Parts,
state: &S,
) -> Result<Option<Self>, Self::Rejection> {
Ok(
<Self as FromRequestParts<S>>::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<T: AsyncConstructibleResource>(pub T);

#[async_trait]
impl<T: AsyncConstructibleResource, S: Send + Sync> FromRequestParts<S> for Obtain<T>
where
Aero: FromRef<S>,
Expand All @@ -98,6 +115,25 @@ where
}
}

impl<T: AsyncConstructibleResource, S: Send + Sync> OptionalFromRequestParts<S> for Obtain<T>
where
Aero: FromRef<S>,
{
type Rejection = Infallible;

// Required method
async fn from_request_parts(
parts: &mut Parts,
state: &S,
) -> Result<Option<Self>, Self::Rejection> {
Ok(
<Self as FromRequestParts<S>>::from_request_parts(parts, state)
.await
.ok(),
)
}
}

impl<H: Resource, T: ResourceList> FromRef<Aero<HCons<H, T>>> for Aero {
fn from_ref(input: &Aero<HCons<H, T>>) -> Self {
input.clone().into()
Expand Down

0 comments on commit 2b304f7

Please sign in to comment.