From a160f98ac20b173865465ac3f381990a036284d7 Mon Sep 17 00:00:00 2001 From: lz1998 <875543533@qq.com> Date: Sat, 30 Dec 2023 01:08:41 +0800 Subject: [PATCH] remove async_trait --- axum-core/Cargo.toml | 1 - axum-core/src/ext_traits/mod.rs | 3 - axum-core/src/ext_traits/request.rs | 54 ++++++-------- axum-core/src/ext_traits/request_parts.rs | 17 ++--- axum-core/src/extract/mod.rs | 19 +++-- axum-core/src/extract/request_parts.rs | 11 --- axum-core/src/extract/tuple.rs | 4 - axum-extra/src/either.rs | 9 +-- axum-extra/src/extract/cached.rs | 10 +-- axum-extra/src/extract/cookie/mod.rs | 2 - axum-extra/src/extract/cookie/private.rs | 2 - axum-extra/src/extract/cookie/signed.rs | 2 - axum-extra/src/extract/form.rs | 2 - axum-extra/src/extract/multipart.rs | 2 - axum-extra/src/extract/optional_path.rs | 2 - axum-extra/src/extract/query.rs | 3 - axum-extra/src/extract/with_rejection.rs | 4 - axum-extra/src/handler/mod.rs | 3 - axum-extra/src/json_lines.rs | 2 - axum-extra/src/protobuf.rs | 2 - axum-extra/src/typed_header.rs | 2 - axum-macros/rust-toolchain | 2 +- axum-macros/src/from_request.rs | 73 ++++++++++++------- axum-macros/src/typed_path.rs | 3 - .../debug_handler/fail/extract_self_mut.rs | 6 +- .../debug_handler/fail/extract_self_ref.rs | 6 +- .../pass/result_impl_into_response.rs | 3 +- .../tests/debug_handler/pass/self_receiver.rs | 7 +- .../tests/debug_handler/pass/set_state.rs | 4 +- .../from_request/pass/override_rejection.rs | 6 +- .../pass/override_rejection_parts.rs | 2 - axum/CHANGELOG.md | 8 -- axum/Cargo.toml | 2 - axum/src/docs/extract.md | 12 --- axum/src/extension.rs | 2 - axum/src/extract/connect_info.rs | 2 - axum/src/extract/host.rs | 2 - axum/src/extract/matched_path.rs | 2 - axum/src/extract/multipart.rs | 2 - axum/src/extract/nested_path.rs | 2 - axum/src/extract/path/mod.rs | 3 - axum/src/extract/query.rs | 2 - axum/src/extract/raw_form.rs | 2 - axum/src/extract/raw_query.rs | 2 - axum/src/extract/request_parts.rs | 2 - axum/src/extract/state.rs | 4 - axum/src/extract/ws.rs | 2 - axum/src/form.rs | 2 - axum/src/json.rs | 2 - axum/src/lib.rs | 2 - axum/src/middleware/from_extractor.rs | 6 +- .../src/main.rs | 2 - .../src/custom_extractor.rs | 2 - examples/customize-path-rejection/src/main.rs | 2 - examples/diesel-async-postgres/src/main.rs | 2 - examples/jwt/src/main.rs | 2 - examples/oauth/src/main.rs | 2 - .../src/main.rs | 2 - examples/sqlx-postgres/src/main.rs | 2 - examples/tokio-postgres/src/main.rs | 2 - examples/validator/Cargo.toml | 1 - examples/validator/src/main.rs | 2 - examples/versioning/src/main.rs | 2 - 63 files changed, 95 insertions(+), 259 deletions(-) diff --git a/axum-core/Cargo.toml b/axum-core/Cargo.toml index fcceb3f8..37156c97 100644 --- a/axum-core/Cargo.toml +++ b/axum-core/Cargo.toml @@ -18,7 +18,6 @@ tracing = ["dep:tracing"] __private_docs = ["dep:tower-http"] [dependencies] -async-trait = "0.1.67" bytes = "1.0" futures-util = { version = "0.3", default-features = false, features = ["alloc"] } http = "1.0.0" diff --git a/axum-core/src/ext_traits/mod.rs b/axum-core/src/ext_traits/mod.rs index 02595fbe..04d87f81 100644 --- a/axum-core/src/ext_traits/mod.rs +++ b/axum-core/src/ext_traits/mod.rs @@ -6,13 +6,11 @@ mod tests { use std::convert::Infallible; use crate::extract::{FromRef, FromRequestParts}; - use async_trait::async_trait; use http::request::Parts; #[derive(Debug, Default, Clone, Copy)] pub(crate) struct State(pub(crate) S); - #[async_trait] impl FromRequestParts for State where InnerState: FromRef, @@ -32,7 +30,6 @@ mod tests { // some extractor that requires the state, such as `SignedCookieJar` pub(crate) struct RequiresState(pub(crate) String); - #[async_trait] impl FromRequestParts for RequiresState where S: Send + Sync, diff --git a/axum-core/src/ext_traits/request.rs b/axum-core/src/ext_traits/request.rs index 5b7aee78..1123fdd3 100644 --- a/axum-core/src/ext_traits/request.rs +++ b/axum-core/src/ext_traits/request.rs @@ -1,6 +1,6 @@ use crate::body::Body; use crate::extract::{DefaultBodyLimitKind, FromRequest, FromRequestParts, Request}; -use futures_util::future::BoxFuture; +use std::future::Future; mod sealed { pub trait Sealed {} @@ -20,7 +20,6 @@ pub trait RequestExt: sealed::Sealed + Sized { /// /// ``` /// use axum::{ - /// async_trait, /// extract::{Request, FromRequest}, /// body::Body, /// http::{header::CONTENT_TYPE, StatusCode}, @@ -30,7 +29,6 @@ pub trait RequestExt: sealed::Sealed + Sized { /// /// struct FormOrJson(T); /// - /// #[async_trait] /// impl FromRequest for FormOrJson /// where /// Json: FromRequest<()>, @@ -67,7 +65,7 @@ pub trait RequestExt: sealed::Sealed + Sized { /// } /// } /// ``` - fn extract(self) -> BoxFuture<'static, Result> + fn extract(self) -> impl Future> + Send where E: FromRequest<(), M> + 'static, M: 'static; @@ -83,7 +81,6 @@ pub trait RequestExt: sealed::Sealed + Sized { /// /// ``` /// use axum::{ - /// async_trait, /// body::Body, /// extract::{Request, FromRef, FromRequest}, /// RequestExt, @@ -93,7 +90,6 @@ pub trait RequestExt: sealed::Sealed + Sized { /// requires_state: RequiresState, /// } /// - /// #[async_trait] /// impl FromRequest for MyExtractor /// where /// String: FromRef, @@ -111,7 +107,6 @@ pub trait RequestExt: sealed::Sealed + Sized { /// // some extractor that consumes the request body and requires state /// struct RequiresState { /* ... */ } /// - /// #[async_trait] /// impl FromRequest for RequiresState /// where /// String: FromRef, @@ -124,7 +119,10 @@ pub trait RequestExt: sealed::Sealed + Sized { /// # } /// } /// ``` - fn extract_with_state(self, state: &S) -> BoxFuture<'_, Result> + fn extract_with_state( + self, + state: &S, + ) -> impl Future> + Send where E: FromRequest + 'static, S: Send + Sync; @@ -137,7 +135,6 @@ pub trait RequestExt: sealed::Sealed + Sized { /// /// ``` /// use axum::{ - /// async_trait, /// extract::{Path, Request, FromRequest}, /// response::{IntoResponse, Response}, /// body::Body, @@ -154,7 +151,6 @@ pub trait RequestExt: sealed::Sealed + Sized { /// payload: T, /// } /// - /// #[async_trait] /// impl FromRequest for MyExtractor /// where /// S: Send + Sync, @@ -179,7 +175,7 @@ pub trait RequestExt: sealed::Sealed + Sized { /// } /// } /// ``` - fn extract_parts(&mut self) -> BoxFuture<'_, Result> + fn extract_parts(&mut self) -> impl Future> + Send where E: FromRequestParts<()> + 'static; @@ -191,7 +187,6 @@ pub trait RequestExt: sealed::Sealed + Sized { /// /// ``` /// use axum::{ - /// async_trait, /// extract::{Request, FromRef, FromRequest, FromRequestParts}, /// http::request::Parts, /// response::{IntoResponse, Response}, @@ -204,7 +199,6 @@ pub trait RequestExt: sealed::Sealed + Sized { /// payload: T, /// } /// - /// #[async_trait] /// impl FromRequest for MyExtractor /// where /// String: FromRef, @@ -234,7 +228,6 @@ pub trait RequestExt: sealed::Sealed + Sized { /// /// struct RequiresState {} /// - /// #[async_trait] /// impl FromRequestParts for RequiresState /// where /// String: FromRef, @@ -250,7 +243,7 @@ pub trait RequestExt: sealed::Sealed + Sized { fn extract_parts_with_state<'a, E, S>( &'a mut self, state: &'a S, - ) -> BoxFuture<'a, Result> + ) -> impl Future> + Send + 'a where E: FromRequestParts + 'static, S: Send + Sync; @@ -267,7 +260,7 @@ pub trait RequestExt: sealed::Sealed + Sized { } impl RequestExt for Request { - fn extract(self) -> BoxFuture<'static, Result> + fn extract(self) -> impl Future> + Send where E: FromRequest<(), M> + 'static, M: 'static, @@ -275,7 +268,10 @@ impl RequestExt for Request { self.extract_with_state(&()) } - fn extract_with_state(self, state: &S) -> BoxFuture<'_, Result> + fn extract_with_state( + self, + state: &S, + ) -> impl Future> + Send where E: FromRequest + 'static, S: Send + Sync, @@ -283,17 +279,17 @@ impl RequestExt for Request { E::from_request(self, state) } - fn extract_parts(&mut self) -> BoxFuture<'_, Result> + fn extract_parts(&mut self) -> impl Future> + Send where E: FromRequestParts<()> + 'static, { self.extract_parts_with_state(&()) } - fn extract_parts_with_state<'a, E, S>( + async fn extract_parts_with_state<'a, E, S>( &'a mut self, state: &'a S, - ) -> BoxFuture<'a, Result> + ) -> Result where E: FromRequestParts + 'static, S: Send + Sync, @@ -306,17 +302,15 @@ impl RequestExt for Request { *req.extensions_mut() = std::mem::take(self.extensions_mut()); let (mut parts, ()) = req.into_parts(); - Box::pin(async move { - let result = E::from_request_parts(&mut parts, state).await; + let result = E::from_request_parts(&mut parts, state).await; - *self.version_mut() = parts.version; - *self.method_mut() = parts.method.clone(); - *self.uri_mut() = parts.uri.clone(); - *self.headers_mut() = std::mem::take(&mut parts.headers); - *self.extensions_mut() = std::mem::take(&mut parts.extensions); + *self.version_mut() = parts.version; + *self.method_mut() = parts.method.clone(); + *self.uri_mut() = parts.uri.clone(); + *self.headers_mut() = std::mem::take(&mut parts.headers); + *self.extensions_mut() = std::mem::take(&mut parts.extensions); - result - }) + result } fn with_limited_body(self) -> Request { @@ -345,7 +339,6 @@ mod tests { ext_traits::tests::{RequiresState, State}, extract::FromRef, }; - use async_trait::async_trait; use http::Method; #[tokio::test] @@ -414,7 +407,6 @@ mod tests { body: String, } - #[async_trait] impl FromRequest for WorksForCustomExtractor where S: Send + Sync, diff --git a/axum-core/src/ext_traits/request_parts.rs b/axum-core/src/ext_traits/request_parts.rs index e7063f4d..9e1a3d1c 100644 --- a/axum-core/src/ext_traits/request_parts.rs +++ b/axum-core/src/ext_traits/request_parts.rs @@ -1,6 +1,6 @@ use crate::extract::FromRequestParts; -use futures_util::future::BoxFuture; use http::request::Parts; +use std::future::Future; mod sealed { pub trait Sealed {} @@ -21,7 +21,6 @@ pub trait RequestPartsExt: sealed::Sealed + Sized { /// response::{Response, IntoResponse}, /// http::request::Parts, /// RequestPartsExt, - /// async_trait, /// }; /// use std::collections::HashMap; /// @@ -30,7 +29,6 @@ pub trait RequestPartsExt: sealed::Sealed + Sized { /// query_params: HashMap, /// } /// - /// #[async_trait] /// impl FromRequestParts for MyExtractor /// where /// S: Send + Sync, @@ -54,7 +52,7 @@ pub trait RequestPartsExt: sealed::Sealed + Sized { /// } /// } /// ``` - fn extract(&mut self) -> BoxFuture<'_, Result> + fn extract(&mut self) -> impl Future> + Send where E: FromRequestParts<()> + 'static; @@ -70,14 +68,12 @@ pub trait RequestPartsExt: sealed::Sealed + Sized { /// response::{Response, IntoResponse}, /// http::request::Parts, /// RequestPartsExt, - /// async_trait, /// }; /// /// struct MyExtractor { /// requires_state: RequiresState, /// } /// - /// #[async_trait] /// impl FromRequestParts for MyExtractor /// where /// String: FromRef, @@ -97,7 +93,6 @@ pub trait RequestPartsExt: sealed::Sealed + Sized { /// struct RequiresState { /* ... */ } /// /// // some extractor that requires a `String` in the state - /// #[async_trait] /// impl FromRequestParts for RequiresState /// where /// String: FromRef, @@ -113,14 +108,14 @@ pub trait RequestPartsExt: sealed::Sealed + Sized { fn extract_with_state<'a, E, S>( &'a mut self, state: &'a S, - ) -> BoxFuture<'a, Result> + ) -> impl Future> + Send + 'a where E: FromRequestParts + 'static, S: Send + Sync; } impl RequestPartsExt for Parts { - fn extract(&mut self) -> BoxFuture<'_, Result> + fn extract(&mut self) -> impl Future> + Send where E: FromRequestParts<()> + 'static, { @@ -130,7 +125,7 @@ impl RequestPartsExt for Parts { fn extract_with_state<'a, E, S>( &'a mut self, state: &'a S, - ) -> BoxFuture<'a, Result> + ) -> impl Future> + Send + 'a where E: FromRequestParts + 'static, S: Send + Sync, @@ -148,7 +143,6 @@ mod tests { ext_traits::tests::{RequiresState, State}, extract::FromRef, }; - use async_trait::async_trait; use http::{Method, Request}; #[tokio::test] @@ -181,7 +175,6 @@ mod tests { from_state: String, } - #[async_trait] impl FromRequestParts for WorksForCustomExtractor where S: Send + Sync, diff --git a/axum-core/src/extract/mod.rs b/axum-core/src/extract/mod.rs index c8e2d219..436400e1 100644 --- a/axum-core/src/extract/mod.rs +++ b/axum-core/src/extract/mod.rs @@ -5,9 +5,9 @@ //! [`axum::extract`]: https://docs.rs/axum/0.7/axum/extract/index.html use crate::{body::Body, response::IntoResponse}; -use async_trait::async_trait; use http::request::Parts; use std::convert::Infallible; +use std::future::Future; pub mod rejection; @@ -42,7 +42,6 @@ mod private { /// See [`axum::extract`] for more general docs about extractors. /// /// [`axum::extract`]: https://docs.rs/axum/0.7/axum/extract/index.html -#[async_trait] #[cfg_attr( nightly_error_messages, diagnostic::on_unimplemented( @@ -55,7 +54,10 @@ pub trait FromRequestParts: Sized { type Rejection: IntoResponse; /// Perform the extraction. - async fn from_request_parts(parts: &mut Parts, state: &S) -> Result; + fn from_request_parts( + parts: &mut Parts, + state: &S, + ) -> impl Future> + Send; } /// Types that can be created from requests. @@ -69,7 +71,6 @@ pub trait FromRequestParts: Sized { /// See [`axum::extract`] for more general docs about extractors. /// /// [`axum::extract`]: https://docs.rs/axum/0.7/axum/extract/index.html -#[async_trait] #[cfg_attr( nightly_error_messages, diagnostic::on_unimplemented( @@ -82,10 +83,12 @@ pub trait FromRequest: Sized { type Rejection: IntoResponse; /// Perform the extraction. - async fn from_request(req: Request, state: &S) -> Result; + fn from_request( + req: Request, + state: &S, + ) -> impl Future> + Send; } -#[async_trait] impl FromRequest for T where S: Send + Sync, @@ -99,7 +102,6 @@ where } } -#[async_trait] impl FromRequestParts for Option where T: FromRequestParts, @@ -115,7 +117,6 @@ where } } -#[async_trait] impl FromRequest for Option where T: FromRequest, @@ -128,7 +129,6 @@ where } } -#[async_trait] impl FromRequestParts for Result where T: FromRequestParts, @@ -141,7 +141,6 @@ where } } -#[async_trait] impl FromRequest for Result where T: FromRequest, diff --git a/axum-core/src/extract/request_parts.rs b/axum-core/src/extract/request_parts.rs index 73f54db7..4196a60c 100644 --- a/axum-core/src/extract/request_parts.rs +++ b/axum-core/src/extract/request_parts.rs @@ -1,12 +1,10 @@ use super::{rejection::*, FromRequest, FromRequestParts, Request}; use crate::{body::Body, RequestExt}; -use async_trait::async_trait; use bytes::Bytes; use http::{request::Parts, Extensions, HeaderMap, Method, Uri, Version}; use http_body_util::BodyExt; use std::convert::Infallible; -#[async_trait] impl FromRequest for Request where S: Send + Sync, @@ -18,7 +16,6 @@ where } } -#[async_trait] impl FromRequestParts for Method where S: Send + Sync, @@ -30,7 +27,6 @@ where } } -#[async_trait] impl FromRequestParts for Uri where S: Send + Sync, @@ -42,7 +38,6 @@ where } } -#[async_trait] impl FromRequestParts for Version where S: Send + Sync, @@ -59,7 +54,6 @@ where /// Prefer using [`TypedHeader`] to extract only the headers you need. /// /// [`TypedHeader`]: https://docs.rs/axum/0.7/axum/extract/struct.TypedHeader.html -#[async_trait] impl FromRequestParts for HeaderMap where S: Send + Sync, @@ -71,7 +65,6 @@ where } } -#[async_trait] impl FromRequest for Bytes where S: Send + Sync, @@ -90,7 +83,6 @@ where } } -#[async_trait] impl FromRequest for String where S: Send + Sync, @@ -114,7 +106,6 @@ where } } -#[async_trait] impl FromRequestParts for Parts where S: Send + Sync, @@ -126,7 +117,6 @@ where } } -#[async_trait] impl FromRequestParts for Extensions where S: Send + Sync, @@ -138,7 +128,6 @@ where } } -#[async_trait] impl FromRequest for Body where S: Send + Sync, diff --git a/axum-core/src/extract/tuple.rs b/axum-core/src/extract/tuple.rs index 021b9616..cbd91a7f 100644 --- a/axum-core/src/extract/tuple.rs +++ b/axum-core/src/extract/tuple.rs @@ -1,10 +1,8 @@ use super::{FromRequest, FromRequestParts, Request}; use crate::response::{IntoResponse, Response}; -use async_trait::async_trait; use http::request::Parts; use std::convert::Infallible; -#[async_trait] impl FromRequestParts for () where S: Send + Sync, @@ -20,7 +18,6 @@ macro_rules! impl_from_request { ( [$($ty:ident),*], $last:ident ) => { - #[async_trait] #[allow(non_snake_case, unused_mut, unused_variables)] impl FromRequestParts for ($($ty,)* $last,) where @@ -46,7 +43,6 @@ macro_rules! impl_from_request { // This impl must not be generic over M, otherwise it would conflict with the blanket // implementation of `FromRequest` for `T: FromRequestParts`. - #[async_trait] #[allow(non_snake_case, unused_mut, unused_variables)] impl FromRequest for ($($ty,)* $last,) where diff --git a/axum-extra/src/either.rs b/axum-extra/src/either.rs index 2742debb..9fa1f82f 100755 --- a/axum-extra/src/either.rs +++ b/axum-extra/src/either.rs @@ -7,7 +7,6 @@ //! use axum::{ //! body::Bytes, //! Router, -//! async_trait, //! routing::get, //! extract::FromRequestParts, //! }; @@ -15,7 +14,6 @@ //! // extractors for checking permissions //! struct AdminPermissions {} //! -//! #[async_trait] //! impl FromRequestParts for AdminPermissions //! where //! S: Send + Sync, @@ -29,7 +27,6 @@ //! //! struct User {} //! -//! #[async_trait] //! impl FromRequestParts for User //! where //! S: Send + Sync, @@ -96,7 +93,6 @@ use std::task::{Context, Poll}; use axum::{ - async_trait, extract::FromRequestParts, response::{IntoResponse, Response}, }; @@ -236,7 +232,6 @@ macro_rules! impl_traits_for_either { [$($ident:ident),* $(,)?], $last:ident $(,)? ) => { - #[async_trait] impl FromRequestParts for $either<$($ident),*, $last> where $($ident: FromRequestParts),*, @@ -247,12 +242,12 @@ macro_rules! impl_traits_for_either { async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { $( - if let Ok(value) = FromRequestParts::from_request_parts(parts, state).await { + if let Ok(value) = <$ident as FromRequestParts>::from_request_parts(parts, state).await { return Ok(Self::$ident(value)); } )* - FromRequestParts::from_request_parts(parts, state).await.map(Self::$last) + <$last as FromRequestParts>::from_request_parts(parts, state).await.map(Self::$last) } } diff --git a/axum-extra/src/extract/cached.rs b/axum-extra/src/extract/cached.rs index 6f7d6227..5f05caee 100644 --- a/axum-extra/src/extract/cached.rs +++ b/axum-extra/src/extract/cached.rs @@ -1,7 +1,4 @@ -use axum::{ - async_trait, - extract::{Extension, FromRequestParts}, -}; +use axum::extract::{Extension, FromRequestParts}; use http::request::Parts; /// Cache results of other extractors. @@ -19,7 +16,6 @@ use http::request::Parts; /// ```rust /// use axum_extra::extract::Cached; /// use axum::{ -/// async_trait, /// extract::FromRequestParts, /// response::{IntoResponse, Response}, /// http::{StatusCode, request::Parts}, @@ -28,7 +24,6 @@ use http::request::Parts; /// #[derive(Clone)] /// struct Session { /* ... */ } /// -/// #[async_trait] /// impl FromRequestParts for Session /// where /// S: Send + Sync, @@ -43,7 +38,6 @@ use http::request::Parts; /// /// struct CurrentUser { /* ... */ } /// -/// #[async_trait] /// impl FromRequestParts for CurrentUser /// where /// S: Send + Sync, @@ -86,7 +80,6 @@ pub struct Cached(pub T); #[derive(Clone)] struct CachedEntry(T); -#[async_trait] impl FromRequestParts for Cached where S: Send + Sync, @@ -126,7 +119,6 @@ mod tests { #[derive(Clone, Debug, PartialEq, Eq)] struct Extractor(Instant); - #[async_trait] impl FromRequestParts for Extractor where S: Send + Sync, diff --git a/axum-extra/src/extract/cookie/mod.rs b/axum-extra/src/extract/cookie/mod.rs index efd2dcdf..50fa6031 100644 --- a/axum-extra/src/extract/cookie/mod.rs +++ b/axum-extra/src/extract/cookie/mod.rs @@ -3,7 +3,6 @@ //! See [`CookieJar`], [`SignedCookieJar`], and [`PrivateCookieJar`] for more details. use axum::{ - async_trait, extract::FromRequestParts, response::{IntoResponse, IntoResponseParts, Response, ResponseParts}, }; @@ -90,7 +89,6 @@ pub struct CookieJar { jar: cookie::CookieJar, } -#[async_trait] impl FromRequestParts for CookieJar where S: Send + Sync, diff --git a/axum-extra/src/extract/cookie/private.rs b/axum-extra/src/extract/cookie/private.rs index 911b0ef2..3a7d0bee 100644 --- a/axum-extra/src/extract/cookie/private.rs +++ b/axum-extra/src/extract/cookie/private.rs @@ -1,6 +1,5 @@ use super::{cookies_from_request, set_cookies, Cookie, Key}; use axum::{ - async_trait, extract::{FromRef, FromRequestParts}, response::{IntoResponse, IntoResponseParts, Response, ResponseParts}, }; @@ -122,7 +121,6 @@ impl fmt::Debug for PrivateCookieJar { } } -#[async_trait] impl FromRequestParts for PrivateCookieJar where S: Send + Sync, diff --git a/axum-extra/src/extract/cookie/signed.rs b/axum-extra/src/extract/cookie/signed.rs index b65df79f..87ba5444 100644 --- a/axum-extra/src/extract/cookie/signed.rs +++ b/axum-extra/src/extract/cookie/signed.rs @@ -1,6 +1,5 @@ use super::{cookies_from_request, set_cookies}; use axum::{ - async_trait, extract::{FromRef, FromRequestParts}, response::{IntoResponse, IntoResponseParts, Response, ResponseParts}, }; @@ -139,7 +138,6 @@ impl fmt::Debug for SignedCookieJar { } } -#[async_trait] impl FromRequestParts for SignedCookieJar where S: Send + Sync, diff --git a/axum-extra/src/extract/form.rs b/axum-extra/src/extract/form.rs index 8729fb5a..896cbf3d 100644 --- a/axum-extra/src/extract/form.rs +++ b/axum-extra/src/extract/form.rs @@ -1,5 +1,4 @@ use axum::{ - async_trait, extract::{rejection::RawFormRejection, FromRequest, RawForm, Request}, response::{IntoResponse, Response}, Error, RequestExt, @@ -44,7 +43,6 @@ pub struct Form(pub T); axum_core::__impl_deref!(Form); -#[async_trait] impl FromRequest for Form where T: DeserializeOwned, diff --git a/axum-extra/src/extract/multipart.rs b/axum-extra/src/extract/multipart.rs index 8c78a777..261f5edb 100644 --- a/axum-extra/src/extract/multipart.rs +++ b/axum-extra/src/extract/multipart.rs @@ -3,7 +3,6 @@ //! See [`Multipart`] for more details. use axum::{ - async_trait, body::{Body, Bytes}, extract::FromRequest, response::{IntoResponse, Response}, @@ -90,7 +89,6 @@ pub struct Multipart { inner: multer::Multipart<'static>, } -#[async_trait] impl FromRequest for Multipart where S: Send + Sync, diff --git a/axum-extra/src/extract/optional_path.rs b/axum-extra/src/extract/optional_path.rs index ca163450..83b7be72 100644 --- a/axum-extra/src/extract/optional_path.rs +++ b/axum-extra/src/extract/optional_path.rs @@ -1,5 +1,4 @@ use axum::{ - async_trait, extract::{path::ErrorKind, rejection::PathRejection, FromRequestParts, Path}, RequestPartsExt, }; @@ -35,7 +34,6 @@ use serde::de::DeserializeOwned; #[derive(Debug)] pub struct OptionalPath(pub Option); -#[async_trait] impl FromRequestParts for OptionalPath where T: DeserializeOwned + Send + 'static, diff --git a/axum-extra/src/extract/query.rs b/axum-extra/src/extract/query.rs index bdeaf78e..e1b3a47e 100644 --- a/axum-extra/src/extract/query.rs +++ b/axum-extra/src/extract/query.rs @@ -1,5 +1,4 @@ use axum::{ - async_trait, extract::FromRequestParts, response::{IntoResponse, Response}, Error, @@ -55,7 +54,6 @@ use std::fmt; #[derive(Debug, Clone, Copy, Default)] pub struct Query(pub T); -#[async_trait] impl FromRequestParts for Query where T: DeserializeOwned, @@ -155,7 +153,6 @@ impl std::error::Error for QueryRejection { #[derive(Debug, Clone, Copy, Default)] pub struct OptionalQuery(pub Option); -#[async_trait] impl FromRequestParts for OptionalQuery where T: DeserializeOwned, diff --git a/axum-extra/src/extract/with_rejection.rs b/axum-extra/src/extract/with_rejection.rs index 1227a1ab..7555461e 100644 --- a/axum-extra/src/extract/with_rejection.rs +++ b/axum-extra/src/extract/with_rejection.rs @@ -1,4 +1,3 @@ -use axum::async_trait; use axum::extract::{FromRequest, FromRequestParts, Request}; use axum::response::IntoResponse; use http::request::Parts; @@ -107,7 +106,6 @@ impl DerefMut for WithRejection { } } -#[async_trait] impl FromRequest for WithRejection where S: Send + Sync, @@ -122,7 +120,6 @@ where } } -#[async_trait] impl FromRequestParts for WithRejection where S: Send + Sync, @@ -152,7 +149,6 @@ mod tests { struct TestExtractor; struct TestRejection; - #[async_trait] impl FromRequestParts for TestExtractor where S: Send + Sync, diff --git a/axum-extra/src/handler/mod.rs b/axum-extra/src/handler/mod.rs index 4017e149..915852a1 100644 --- a/axum-extra/src/handler/mod.rs +++ b/axum-extra/src/handler/mod.rs @@ -47,7 +47,6 @@ pub trait HandlerCallWithExtractors: Sized { /// use axum_extra::handler::HandlerCallWithExtractors; /// use axum::{ /// Router, - /// async_trait, /// routing::get, /// extract::FromRequestParts, /// }; @@ -68,7 +67,6 @@ pub trait HandlerCallWithExtractors: Sized { /// // extractors for checking permissions /// struct AdminPermissions {} /// - /// #[async_trait] /// impl FromRequestParts for AdminPermissions /// where /// S: Send + Sync, @@ -82,7 +80,6 @@ pub trait HandlerCallWithExtractors: Sized { /// /// struct User {} /// - /// #[async_trait] /// impl FromRequestParts for User /// where /// S: Send + Sync, diff --git a/axum-extra/src/json_lines.rs b/axum-extra/src/json_lines.rs index d72c23b6..8f8d34d5 100644 --- a/axum-extra/src/json_lines.rs +++ b/axum-extra/src/json_lines.rs @@ -1,7 +1,6 @@ //! Newline delimited JSON extractor and response. use axum::{ - async_trait, body::Body, extract::{FromRequest, Request}, response::{IntoResponse, Response}, @@ -99,7 +98,6 @@ impl JsonLines { } } -#[async_trait] impl FromRequest for JsonLines where T: DeserializeOwned, diff --git a/axum-extra/src/protobuf.rs b/axum-extra/src/protobuf.rs index e3c4b51d..2a308731 100644 --- a/axum-extra/src/protobuf.rs +++ b/axum-extra/src/protobuf.rs @@ -1,7 +1,6 @@ //! Protocol Buffer extractor and response. use axum::{ - async_trait, extract::{rejection::BytesRejection, FromRequest, Request}, response::{IntoResponse, Response}, }; @@ -90,7 +89,6 @@ use prost::Message; #[must_use] pub struct Protobuf(pub T); -#[async_trait] impl FromRequest for Protobuf where T: Message + Default, diff --git a/axum-extra/src/typed_header.rs b/axum-extra/src/typed_header.rs index aa89e81d..d10c3a32 100644 --- a/axum-extra/src/typed_header.rs +++ b/axum-extra/src/typed_header.rs @@ -1,7 +1,6 @@ //! Extractor and response for typed headers. use axum::{ - async_trait, extract::FromRequestParts, response::{IntoResponse, IntoResponseParts, Response, ResponseParts}, }; @@ -55,7 +54,6 @@ use std::convert::Infallible; #[must_use] pub struct TypedHeader(pub T); -#[async_trait] impl FromRequestParts for TypedHeader where T: Header, diff --git a/axum-macros/rust-toolchain b/axum-macros/rust-toolchain index 2c3dbd2c..a0769391 100644 --- a/axum-macros/rust-toolchain +++ b/axum-macros/rust-toolchain @@ -1 +1 @@ -nightly-2023-09-23 +nightly-2023-11-23 diff --git a/axum-macros/src/from_request.rs b/axum-macros/src/from_request.rs index 474dd0cd..fc43968f 100644 --- a/axum-macros/src/from_request.rs +++ b/axum-macros/src/from_request.rs @@ -6,6 +6,7 @@ use crate::{ use proc_macro2::{Span, TokenStream}; use quote::{quote, quote_spanned, ToTokens}; use std::{collections::HashSet, fmt, iter}; +use syn::token::Comma; use syn::{ parse_quote, punctuated::Punctuated, spanned::Spanned, Fields, Ident, Path, Token, Type, }; @@ -339,12 +340,16 @@ fn impl_struct_by_extracting_each_field( state: &State, tr: Trait, ) -> syn::Result { + let trait_generics = state + .trait_generics() + .collect::>(); + let trait_fn_body = match state { State::CannotInfer => quote! { ::std::unimplemented!() }, _ => { - let extract_fields = extract_fields(&fields, &rejection, tr)?; + let extract_fields = extract_fields(&fields, &rejection, tr, &trait_generics)?; quote! { ::std::result::Result::Ok(Self { #(#extract_fields)* @@ -365,15 +370,10 @@ fn impl_struct_by_extracting_each_field( .impl_generics() .collect::>(); - let trait_generics = state - .trait_generics() - .collect::>(); - let state_bounds = state.bounds(); Ok(match tr { Trait::FromRequest => quote! { - #[::axum::async_trait] #[automatically_derived] impl<#impl_generics> ::axum::extract::FromRequest<#trait_generics> for #ident where @@ -390,7 +390,6 @@ fn impl_struct_by_extracting_each_field( } }, Trait::FromRequestParts => quote! { - #[::axum::async_trait] #[automatically_derived] impl<#impl_generics> ::axum::extract::FromRequestParts<#trait_generics> for #ident where @@ -421,6 +420,7 @@ fn extract_fields( fields: &syn::Fields, rejection: &Option, tr: Trait, + trait_generics: &Punctuated, ) -> syn::Result> { fn member(field: &syn::Field, index: usize) -> TokenStream { match &field.ident { @@ -435,7 +435,7 @@ fn extract_fields( } } - fn into_inner(via: Option<(attr::kw::via, syn::Path)>, ty_span: Span) -> TokenStream { + fn into_inner(via: &Option<(attr::kw::via, syn::Path)>, ty_span: Span) -> TokenStream { if let Some((_, path)) = via { let span = path.span(); quote_spanned! {span=> @@ -448,6 +448,23 @@ fn extract_fields( } } + fn into_outer( + via: &Option<(attr::kw::via, syn::Path)>, + ty_span: Span, + field_ty: &Type, + ) -> TokenStream { + if let Some((_, path)) = via { + let span = path.span(); + quote_spanned! {span=> + #path<#field_ty> + } + } else { + quote_spanned! {ty_span=> + #field_ty + } + } + } + let mut fields_iter = fields.iter(); let last = match tr { @@ -464,16 +481,17 @@ fn extract_fields( let member = member(field, index); let ty_span = field.ty.span(); - let into_inner = into_inner(via, ty_span); + let into_inner = into_inner(&via, ty_span); if peel_option(&field.ty).is_some() { + let field_ty = into_outer(&via, ty_span, peel_option(&field.ty).unwrap()); let tokens = match tr { Trait::FromRequest => { quote_spanned! {ty_span=> #member: { let (mut parts, body) = req.into_parts(); let value = - ::axum::extract::FromRequestParts::from_request_parts( + <#field_ty as ::axum::extract::FromRequestParts<#trait_generics>>::from_request_parts( &mut parts, state, ) @@ -488,7 +506,7 @@ fn extract_fields( Trait::FromRequestParts => { quote_spanned! {ty_span=> #member: { - ::axum::extract::FromRequestParts::from_request_parts( + <#field_ty as ::axum::extract::FromRequestParts<#trait_generics>>::from_request_parts( parts, state, ) @@ -501,13 +519,14 @@ fn extract_fields( }; Ok(tokens) } else if peel_result_ok(&field.ty).is_some() { + let field_ty = into_outer(&via,ty_span, peel_result_ok(&field.ty).unwrap()); let tokens = match tr { Trait::FromRequest => { quote_spanned! {ty_span=> #member: { let (mut parts, body) = req.into_parts(); let value = - ::axum::extract::FromRequestParts::from_request_parts( + <#field_ty as ::axum::extract::FromRequestParts<#trait_generics>>::from_request_parts( &mut parts, state, ) @@ -521,7 +540,7 @@ fn extract_fields( Trait::FromRequestParts => { quote_spanned! {ty_span=> #member: { - ::axum::extract::FromRequestParts::from_request_parts( + <#field_ty as ::axum::extract::FromRequestParts<#trait_generics>>::from_request_parts( parts, state, ) @@ -533,6 +552,7 @@ fn extract_fields( }; Ok(tokens) } else { + let field_ty = into_outer(&via,ty_span,&field.ty); let map_err = if let Some(rejection) = rejection { quote! { <#rejection as ::std::convert::From<_>>::from } } else { @@ -545,7 +565,7 @@ fn extract_fields( #member: { let (mut parts, body) = req.into_parts(); let value = - ::axum::extract::FromRequestParts::from_request_parts( + <#field_ty as ::axum::extract::FromRequestParts<#trait_generics>>::from_request_parts( &mut parts, state, ) @@ -560,7 +580,7 @@ fn extract_fields( Trait::FromRequestParts => { quote_spanned! {ty_span=> #member: { - ::axum::extract::FromRequestParts::from_request_parts( + <#field_ty as ::axum::extract::FromRequestParts<#trait_generics>>::from_request_parts( parts, state, ) @@ -582,26 +602,29 @@ fn extract_fields( let member = member(field, fields.len() - 1); let ty_span = field.ty.span(); - let into_inner = into_inner(via, ty_span); + let into_inner = into_inner(&via, ty_span); let item = if peel_option(&field.ty).is_some() { + let field_ty = into_outer(&via, ty_span, peel_option(&field.ty).unwrap()); quote_spanned! {ty_span=> #member: { - ::axum::extract::FromRequest::from_request(req, state) + <#field_ty as ::axum::extract::FromRequest<#trait_generics>>::from_request(req, state) .await .ok() .map(#into_inner) }, } } else if peel_result_ok(&field.ty).is_some() { + let field_ty = into_outer(&via, ty_span, peel_result_ok(&field.ty).unwrap()); quote_spanned! {ty_span=> #member: { - ::axum::extract::FromRequest::from_request(req, state) + <#field_ty as ::axum::extract::FromRequest<#trait_generics>>::from_request(req, state) .await .map(#into_inner) }, } } else { + let field_ty = into_outer(&via, ty_span, &field.ty); let map_err = if let Some(rejection) = rejection { quote! { <#rejection as ::std::convert::From<_>>::from } } else { @@ -610,7 +633,7 @@ fn extract_fields( quote_spanned! {ty_span=> #member: { - ::axum::extract::FromRequest::from_request(req, state) + <#field_ty as ::axum::extract::FromRequest<#trait_generics>>::from_request(req, state) .await .map(#into_inner) .map_err(#map_err)? @@ -807,7 +830,6 @@ fn impl_struct_by_extracting_all_at_once( let tokens = match tr { Trait::FromRequest => { quote_spanned! {path_span=> - #[::axum::async_trait] #[automatically_derived] impl<#impl_generics> ::axum::extract::FromRequest<#trait_generics> for #ident #ident_generics where @@ -821,7 +843,7 @@ fn impl_struct_by_extracting_all_at_once( req: ::axum::http::Request<::axum::body::Body>, state: &#state, ) -> ::std::result::Result { - ::axum::extract::FromRequest::from_request(req, state) + <#via_path<#via_type_generics> as ::axum::extract::FromRequest<#trait_generics>>::from_request(req, state) .await .map(|#via_path(value)| #value_to_self) .map_err(#map_err) @@ -831,7 +853,6 @@ fn impl_struct_by_extracting_all_at_once( } Trait::FromRequestParts => { quote_spanned! {path_span=> - #[::axum::async_trait] #[automatically_derived] impl<#impl_generics> ::axum::extract::FromRequestParts<#trait_generics> for #ident #ident_generics where @@ -845,7 +866,7 @@ fn impl_struct_by_extracting_all_at_once( parts: &mut ::axum::http::request::Parts, state: &#state, ) -> ::std::result::Result { - ::axum::extract::FromRequestParts::from_request_parts(parts, state) + <#via_path<#via_type_generics> as ::axum::extract::FromRequestParts<#trait_generics>>::from_request_parts(parts, state) .await .map(|#via_path(value)| #value_to_self) .map_err(#map_err) @@ -920,7 +941,6 @@ fn impl_enum_by_extracting_all_at_once( let tokens = match tr { Trait::FromRequest => { quote_spanned! {path_span=> - #[::axum::async_trait] #[automatically_derived] impl<#impl_generics> ::axum::extract::FromRequest<#trait_generics> for #ident where @@ -932,7 +952,7 @@ fn impl_enum_by_extracting_all_at_once( req: ::axum::http::Request<::axum::body::Body>, state: &#state, ) -> ::std::result::Result { - ::axum::extract::FromRequest::from_request(req, state) + <#path::<#ident> as ::axum::extract::FromRequest<#trait_generics>>::from_request(req, state) .await .map(|#path(inner)| inner) .map_err(#map_err) @@ -942,7 +962,6 @@ fn impl_enum_by_extracting_all_at_once( } Trait::FromRequestParts => { quote_spanned! {path_span=> - #[::axum::async_trait] #[automatically_derived] impl<#impl_generics> ::axum::extract::FromRequestParts<#trait_generics> for #ident where @@ -954,7 +973,7 @@ fn impl_enum_by_extracting_all_at_once( parts: &mut ::axum::http::request::Parts, state: &#state, ) -> ::std::result::Result { - ::axum::extract::FromRequestParts::from_request_parts(parts, state) + <#path::<#ident> as FromRequestParts<#trait_generics>>::from_request_parts(parts, state) .await .map(|#path(inner)| inner) .map_err(#map_err) diff --git a/axum-macros/src/typed_path.rs b/axum-macros/src/typed_path.rs index 61db3eb9..61e1695b 100644 --- a/axum-macros/src/typed_path.rs +++ b/axum-macros/src/typed_path.rs @@ -133,7 +133,6 @@ fn expand_named_fields( let map_err_rejection = map_err_rejection(&rejection); let from_request_impl = quote! { - #[::axum::async_trait] #[automatically_derived] impl ::axum::extract::FromRequestParts for #ident where @@ -238,7 +237,6 @@ fn expand_unnamed_fields( let map_err_rejection = map_err_rejection(&rejection); let from_request_impl = quote! { - #[::axum::async_trait] #[automatically_derived] impl ::axum::extract::FromRequestParts for #ident where @@ -322,7 +320,6 @@ fn expand_unit_fields( }; let from_request_impl = quote! { - #[::axum::async_trait] #[automatically_derived] impl ::axum::extract::FromRequestParts for #ident where diff --git a/axum-macros/tests/debug_handler/fail/extract_self_mut.rs b/axum-macros/tests/debug_handler/fail/extract_self_mut.rs index 21ae99d6..eb17c1df 100644 --- a/axum-macros/tests/debug_handler/fail/extract_self_mut.rs +++ b/axum-macros/tests/debug_handler/fail/extract_self_mut.rs @@ -1,12 +1,8 @@ -use axum::{ - async_trait, - extract::{Request, FromRequest}, -}; +use axum::extract::{FromRequest, Request}; use axum_macros::debug_handler; struct A; -#[async_trait] impl FromRequest for A where S: Send + Sync, diff --git a/axum-macros/tests/debug_handler/fail/extract_self_ref.rs b/axum-macros/tests/debug_handler/fail/extract_self_ref.rs index 8e328119..d70c5f23 100644 --- a/axum-macros/tests/debug_handler/fail/extract_self_ref.rs +++ b/axum-macros/tests/debug_handler/fail/extract_self_ref.rs @@ -1,12 +1,8 @@ -use axum::{ - async_trait, - extract::{Request, FromRequest}, -}; +use axum::extract::{FromRequest, Request}; use axum_macros::debug_handler; struct A; -#[async_trait] impl FromRequest for A where S: Send + Sync, diff --git a/axum-macros/tests/debug_handler/pass/result_impl_into_response.rs b/axum-macros/tests/debug_handler/pass/result_impl_into_response.rs index 782fc930..f23c9b62 100644 --- a/axum-macros/tests/debug_handler/pass/result_impl_into_response.rs +++ b/axum-macros/tests/debug_handler/pass/result_impl_into_response.rs @@ -1,4 +1,4 @@ -use axum::{async_trait, extract::FromRequestParts, http::request::Parts, response::IntoResponse}; +use axum::{extract::FromRequestParts, http::request::Parts, response::IntoResponse}; use axum_macros::debug_handler; fn main() {} @@ -115,7 +115,6 @@ impl A { } } -#[async_trait] impl FromRequestParts for A where S: Send + Sync, diff --git a/axum-macros/tests/debug_handler/pass/self_receiver.rs b/axum-macros/tests/debug_handler/pass/self_receiver.rs index 9b722845..3fbcc4e0 100644 --- a/axum-macros/tests/debug_handler/pass/self_receiver.rs +++ b/axum-macros/tests/debug_handler/pass/self_receiver.rs @@ -1,12 +1,8 @@ -use axum::{ - async_trait, - extract::{Request, FromRequest}, -}; +use axum::extract::{FromRequest, Request}; use axum_macros::debug_handler; struct A; -#[async_trait] impl FromRequest for A where S: Send + Sync, @@ -18,7 +14,6 @@ where } } -#[async_trait] impl FromRequest for Box where S: Send + Sync, diff --git a/axum-macros/tests/debug_handler/pass/set_state.rs b/axum-macros/tests/debug_handler/pass/set_state.rs index 60a7a330..72bba5ae 100644 --- a/axum-macros/tests/debug_handler/pass/set_state.rs +++ b/axum-macros/tests/debug_handler/pass/set_state.rs @@ -1,6 +1,5 @@ +use axum::extract::{FromRef, FromRequest, Request}; use axum_macros::debug_handler; -use axum::extract::{Request, FromRef, FromRequest}; -use axum::async_trait; #[debug_handler(state = AppState)] async fn handler(_: A) {} @@ -10,7 +9,6 @@ struct AppState; struct A; -#[async_trait] impl FromRequest for A where S: Send + Sync, diff --git a/axum-macros/tests/from_request/pass/override_rejection.rs b/axum-macros/tests/from_request/pass/override_rejection.rs index 779058b9..736006ed 100644 --- a/axum-macros/tests/from_request/pass/override_rejection.rs +++ b/axum-macros/tests/from_request/pass/override_rejection.rs @@ -1,10 +1,9 @@ use axum::{ - async_trait, - extract::{Request, rejection::ExtensionRejection, FromRequest}, + body::Body, + extract::{rejection::ExtensionRejection, FromRequest, Request}, http::StatusCode, response::{IntoResponse, Response}, routing::get, - body::Body, Extension, Router, }; @@ -27,7 +26,6 @@ struct MyExtractor { struct OtherExtractor; -#[async_trait] impl FromRequest for OtherExtractor where S: Send + Sync, diff --git a/axum-macros/tests/from_request/pass/override_rejection_parts.rs b/axum-macros/tests/from_request/pass/override_rejection_parts.rs index 8ef9cb22..7cc27de2 100644 --- a/axum-macros/tests/from_request/pass/override_rejection_parts.rs +++ b/axum-macros/tests/from_request/pass/override_rejection_parts.rs @@ -1,5 +1,4 @@ use axum::{ - async_trait, extract::{rejection::ExtensionRejection, FromRequestParts}, http::{request::Parts, StatusCode}, response::{IntoResponse, Response}, @@ -26,7 +25,6 @@ struct MyExtractor { struct OtherExtractor; -#[async_trait] impl FromRequestParts for OtherExtractor where S: Send + Sync, diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index 6d52cd6c..d3cc8c05 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -577,7 +577,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ```rust struct MyExtractor { /* ... */ } - #[async_trait] impl FromRequest for MyExtractor where B: Send, @@ -596,13 +595,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 use axum::{ extract::{FromRequest, FromRequestParts}, http::{StatusCode, Request, request::Parts}, - async_trait, }; struct MyExtractor { /* ... */ } // implement `FromRequestParts` if you don't need to consume the request body - #[async_trait] impl FromRequestParts for MyExtractor where S: Send + Sync, @@ -615,7 +612,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 } // implement `FromRequest` if you do need to consume the request body - #[async_trait] impl FromRequest for MyExtractor where S: Send + Sync, @@ -1163,7 +1159,6 @@ Yanked, as it didn't compile in release mode. ```rust struct MyExtractor { /* ... */ } - #[async_trait] impl FromRequest for MyExtractor where B: Send, @@ -1182,13 +1177,11 @@ Yanked, as it didn't compile in release mode. use axum::{ extract::{FromRequest, FromRequestParts}, http::{StatusCode, Request, request::Parts}, - async_trait, }; struct MyExtractor { /* ... */ } // implement `FromRequestParts` if you don't need to consume the request body - #[async_trait] impl FromRequestParts for MyExtractor where S: Send + Sync, @@ -1201,7 +1194,6 @@ Yanked, as it didn't compile in release mode. } // implement `FromRequest` if you do need to consume the request body - #[async_trait] impl FromRequest for MyExtractor where S: Send + Sync, diff --git a/axum/Cargo.toml b/axum/Cargo.toml index da8f5ef2..5b08f9dc 100644 --- a/axum/Cargo.toml +++ b/axum/Cargo.toml @@ -41,7 +41,6 @@ ws = ["dep:hyper", "tokio", "dep:tokio-tungstenite", "dep:sha1", "dep:base64"] __private_docs = ["tower/full", "dep:tower-http"] [dependencies] -async-trait = "0.1.67" axum-core = { path = "../axum-core", version = "0.4.2" } bytes = "1.0" futures-util = { version = "0.3", default-features = false, features = ["alloc"] } @@ -201,7 +200,6 @@ allowed = [ "tower_service", # >=1.0 - "async_trait", "bytes", "http", "http_body", diff --git a/axum/src/docs/extract.md b/axum/src/docs/extract.md index 13d27171..dc2e618a 100644 --- a/axum/src/docs/extract.md +++ b/axum/src/docs/extract.md @@ -421,7 +421,6 @@ request body: ```rust,no_run use axum::{ - async_trait, extract::FromRequestParts, routing::get, Router, @@ -434,7 +433,6 @@ use axum::{ struct ExtractUserAgent(HeaderValue); -#[async_trait] impl FromRequestParts for ExtractUserAgent where S: Send + Sync, @@ -464,7 +462,6 @@ If your extractor needs to consume the request body you must implement [`FromReq ```rust,no_run use axum::{ - async_trait, extract::{Request, FromRequest}, response::{Response, IntoResponse}, body::{Bytes, Body}, @@ -478,7 +475,6 @@ use axum::{ struct ValidatedBody(Bytes); -#[async_trait] impl FromRequest for ValidatedBody where Bytes: FromRequest, @@ -518,7 +514,6 @@ use axum::{ extract::{FromRequest, Request, FromRequestParts}, http::request::Parts, body::Body, - async_trait, }; use std::convert::Infallible; @@ -526,7 +521,6 @@ use std::convert::Infallible; struct MyExtractor; // `MyExtractor` implements both `FromRequest` -#[async_trait] impl FromRequest for MyExtractor where S: Send + Sync, @@ -540,7 +534,6 @@ where } // and `FromRequestParts` -#[async_trait] impl FromRequestParts for MyExtractor where S: Send + Sync, @@ -574,7 +567,6 @@ in your implementation. ```rust use axum::{ - async_trait, extract::{Extension, FromRequestParts}, http::{StatusCode, HeaderMap, request::Parts}, response::{IntoResponse, Response}, @@ -591,7 +583,6 @@ struct AuthenticatedUser { // ... } -#[async_trait] impl FromRequestParts for AuthenticatedUser where S: Send + Sync, @@ -645,7 +636,6 @@ use axum::{ routing::get, extract::{Request, FromRequest, FromRequestParts}, http::{HeaderMap, request::Parts}, - async_trait, }; use std::time::{Instant, Duration}; @@ -656,7 +646,6 @@ struct Timing { } // we must implement both `FromRequestParts` -#[async_trait] impl FromRequestParts for Timing where S: Send + Sync, @@ -676,7 +665,6 @@ where } // and `FromRequest` -#[async_trait] impl FromRequest for Timing where S: Send + Sync, diff --git a/axum/src/extension.rs b/axum/src/extension.rs index a72568ac..59ca93c8 100644 --- a/axum/src/extension.rs +++ b/axum/src/extension.rs @@ -1,5 +1,4 @@ use crate::{extract::rejection::*, response::IntoResponseParts}; -use async_trait::async_trait; use axum_core::{ extract::FromRequestParts, response::{IntoResponse, Response, ResponseParts}, @@ -70,7 +69,6 @@ use tower_service::Service; #[must_use] pub struct Extension(pub T); -#[async_trait] impl FromRequestParts for Extension where T: Clone + Send + Sync + 'static, diff --git a/axum/src/extract/connect_info.rs b/axum/src/extract/connect_info.rs index 3036d375..3252af1c 100644 --- a/axum/src/extract/connect_info.rs +++ b/axum/src/extract/connect_info.rs @@ -7,7 +7,6 @@ use crate::extension::AddExtension; use super::{Extension, FromRequestParts}; -use async_trait::async_trait; use http::request::Parts; use std::{ convert::Infallible, @@ -139,7 +138,6 @@ opaque_future! { #[derive(Clone, Copy, Debug)] pub struct ConnectInfo(pub T); -#[async_trait] impl FromRequestParts for ConnectInfo where S: Send + Sync, diff --git a/axum/src/extract/host.rs b/axum/src/extract/host.rs index d5be6a97..5ffb7929 100644 --- a/axum/src/extract/host.rs +++ b/axum/src/extract/host.rs @@ -2,7 +2,6 @@ use super::{ rejection::{FailedToResolveHost, HostRejection}, FromRequestParts, }; -use async_trait::async_trait; use http::{ header::{HeaderMap, FORWARDED}, request::Parts, @@ -23,7 +22,6 @@ const X_FORWARDED_HOST_HEADER_KEY: &str = "X-Forwarded-Host"; #[derive(Debug, Clone)] pub struct Host(pub String); -#[async_trait] impl FromRequestParts for Host where S: Send + Sync, diff --git a/axum/src/extract/matched_path.rs b/axum/src/extract/matched_path.rs index cdb49b4d..7cd819d5 100644 --- a/axum/src/extract/matched_path.rs +++ b/axum/src/extract/matched_path.rs @@ -1,6 +1,5 @@ use super::{rejection::*, FromRequestParts}; use crate::routing::{RouteId, NEST_TAIL_PARAM_CAPTURE}; -use async_trait::async_trait; use http::request::Parts; use std::{collections::HashMap, sync::Arc}; @@ -63,7 +62,6 @@ impl MatchedPath { } } -#[async_trait] impl FromRequestParts for MatchedPath where S: Send + Sync, diff --git a/axum/src/extract/multipart.rs b/axum/src/extract/multipart.rs index 2c592ece..486c8554 100644 --- a/axum/src/extract/multipart.rs +++ b/axum/src/extract/multipart.rs @@ -4,7 +4,6 @@ use super::{FromRequest, Request}; use crate::body::Bytes; -use async_trait::async_trait; use axum_core::{ __composite_rejection as composite_rejection, __define_rejection as define_rejection, response::{IntoResponse, Response}, @@ -65,7 +64,6 @@ pub struct Multipart { inner: multer::Multipart<'static>, } -#[async_trait] impl FromRequest for Multipart where S: Send + Sync, diff --git a/axum/src/extract/nested_path.rs b/axum/src/extract/nested_path.rs index f31fe3fa..9477f15a 100644 --- a/axum/src/extract/nested_path.rs +++ b/axum/src/extract/nested_path.rs @@ -4,7 +4,6 @@ use std::{ }; use crate::extract::Request; -use async_trait::async_trait; use axum_core::extract::FromRequestParts; use http::request::Parts; use tower_layer::{layer_fn, Layer}; @@ -47,7 +46,6 @@ impl NestedPath { } } -#[async_trait] impl FromRequestParts for NestedPath where S: Send + Sync, diff --git a/axum/src/extract/path/mod.rs b/axum/src/extract/path/mod.rs index 0be90088..e8bcb0a9 100644 --- a/axum/src/extract/path/mod.rs +++ b/axum/src/extract/path/mod.rs @@ -8,7 +8,6 @@ use crate::{ routing::url_params::UrlParams, util::PercentDecodedStr, }; -use async_trait::async_trait; use axum_core::response::{IntoResponse, Response}; use http::{request::Parts, StatusCode}; use serde::de::DeserializeOwned; @@ -145,7 +144,6 @@ pub struct Path(pub T); axum_core::__impl_deref!(Path); -#[async_trait] impl FromRequestParts for Path where T: DeserializeOwned + Send, @@ -445,7 +443,6 @@ impl std::error::Error for FailedToDeserializePathParams {} #[derive(Debug)] pub struct RawPathParams(Vec<(Arc, PercentDecodedStr)>); -#[async_trait] impl FromRequestParts for RawPathParams where S: Send + Sync, diff --git a/axum/src/extract/query.rs b/axum/src/extract/query.rs index 37a40771..fb14b334 100644 --- a/axum/src/extract/query.rs +++ b/axum/src/extract/query.rs @@ -1,5 +1,4 @@ use super::{rejection::*, FromRequestParts}; -use async_trait::async_trait; use http::{request::Parts, Uri}; use serde::de::DeserializeOwned; @@ -46,7 +45,6 @@ use serde::de::DeserializeOwned; #[derive(Debug, Clone, Copy, Default)] pub struct Query(pub T); -#[async_trait] impl FromRequestParts for Query where T: DeserializeOwned, diff --git a/axum/src/extract/raw_form.rs b/axum/src/extract/raw_form.rs index a4e0d6c5..29cb4c6d 100644 --- a/axum/src/extract/raw_form.rs +++ b/axum/src/extract/raw_form.rs @@ -1,4 +1,3 @@ -use async_trait::async_trait; use axum_core::extract::{FromRequest, Request}; use bytes::Bytes; use http::Method; @@ -30,7 +29,6 @@ use super::{ #[derive(Debug)] pub struct RawForm(pub Bytes); -#[async_trait] impl FromRequest for RawForm where S: Send + Sync, diff --git a/axum/src/extract/raw_query.rs b/axum/src/extract/raw_query.rs index d8c56f84..c792960a 100644 --- a/axum/src/extract/raw_query.rs +++ b/axum/src/extract/raw_query.rs @@ -1,5 +1,4 @@ use super::FromRequestParts; -use async_trait::async_trait; use http::request::Parts; use std::convert::Infallible; @@ -25,7 +24,6 @@ use std::convert::Infallible; #[derive(Debug)] pub struct RawQuery(pub Option); -#[async_trait] impl FromRequestParts for RawQuery where S: Send + Sync, diff --git a/axum/src/extract/request_parts.rs b/axum/src/extract/request_parts.rs index 9756665b..a05093da 100644 --- a/axum/src/extract/request_parts.rs +++ b/axum/src/extract/request_parts.rs @@ -1,5 +1,4 @@ use super::{Extension, FromRequestParts}; -use async_trait::async_trait; use http::{request::Parts, Uri}; use std::convert::Infallible; @@ -70,7 +69,6 @@ use std::convert::Infallible; pub struct OriginalUri(pub Uri); #[cfg(feature = "original-uri")] -#[async_trait] impl FromRequestParts for OriginalUri where S: Send + Sync, diff --git a/axum/src/extract/state.rs b/axum/src/extract/state.rs index fb401c00..e72c2e11 100644 --- a/axum/src/extract/state.rs +++ b/axum/src/extract/state.rs @@ -1,4 +1,3 @@ -use async_trait::async_trait; use axum_core::extract::{FromRef, FromRequestParts}; use http::request::Parts; use std::{ @@ -219,13 +218,11 @@ use std::{ /// ```rust /// use axum_core::extract::{FromRequestParts, FromRef}; /// use http::request::Parts; -/// use async_trait::async_trait; /// use std::convert::Infallible; /// /// // the extractor your library provides /// struct MyLibraryExtractor; /// -/// #[async_trait] /// impl FromRequestParts for MyLibraryExtractor /// where /// // keep `S` generic but require that it can produce a `MyLibraryState` @@ -344,7 +341,6 @@ use std::{ #[derive(Debug, Default, Clone, Copy)] pub struct State(pub S); -#[async_trait] impl FromRequestParts for State where InnerState: FromRef, diff --git a/axum/src/extract/ws.rs b/axum/src/extract/ws.rs index 7781d6c2..69850d0d 100644 --- a/axum/src/extract/ws.rs +++ b/axum/src/extract/ws.rs @@ -93,7 +93,6 @@ use self::rejection::*; use super::FromRequestParts; use crate::{body::Bytes, response::Response, Error}; -use async_trait::async_trait; use axum_core::body::Body; use futures_util::{ sink::{Sink, SinkExt}, @@ -381,7 +380,6 @@ impl OnFailedUpgrade for DefaultOnFailedUpgrade { fn call(self, _error: Error) {} } -#[async_trait] impl FromRequestParts for WebSocketUpgrade where S: Send + Sync, diff --git a/axum/src/form.rs b/axum/src/form.rs index 5f42b303..79ae80e7 100644 --- a/axum/src/form.rs +++ b/axum/src/form.rs @@ -1,6 +1,5 @@ use crate::extract::Request; use crate::extract::{rejection::*, FromRequest, RawForm}; -use async_trait::async_trait; use axum_core::response::{IntoResponse, Response}; use axum_core::RequestExt; use http::header::CONTENT_TYPE; @@ -72,7 +71,6 @@ use serde::Serialize; #[must_use] pub struct Form(pub T); -#[async_trait] impl FromRequest for Form where T: DeserializeOwned, diff --git a/axum/src/json.rs b/axum/src/json.rs index e96be5b8..b24446ad 100644 --- a/axum/src/json.rs +++ b/axum/src/json.rs @@ -1,6 +1,5 @@ use crate::extract::Request; use crate::extract::{rejection::*, FromRequest}; -use async_trait::async_trait; use axum_core::response::{IntoResponse, Response}; use bytes::{BufMut, Bytes, BytesMut}; use http::{ @@ -92,7 +91,6 @@ use serde::{de::DeserializeOwned, Serialize}; #[must_use] pub struct Json(pub T); -#[async_trait] impl FromRequest for Json where T: DeserializeOwned, diff --git a/axum/src/lib.rs b/axum/src/lib.rs index 42d5d41a..a30922b6 100644 --- a/axum/src/lib.rs +++ b/axum/src/lib.rs @@ -443,8 +443,6 @@ pub mod serve; #[cfg(test)] mod test_helpers; -#[doc(no_inline)] -pub use async_trait::async_trait; #[doc(no_inline)] pub use http; diff --git a/axum/src/middleware/from_extractor.rs b/axum/src/middleware/from_extractor.rs index e120ffc1..b5983443 100644 --- a/axum/src/middleware/from_extractor.rs +++ b/axum/src/middleware/from_extractor.rs @@ -39,12 +39,10 @@ use tower_service::Service; /// Router, /// http::{header, StatusCode, request::Parts}, /// }; -/// use async_trait::async_trait; /// /// // An extractor that performs authorization. /// struct RequireAuth; /// -/// #[async_trait] /// impl FromRequestParts for RequireAuth /// where /// S: Send + Sync, @@ -303,7 +301,7 @@ where #[cfg(test)] mod tests { use super::*; - use crate::{async_trait, handler::Handler, routing::get, test_helpers::*, Router}; + use crate::{handler::Handler, routing::get, test_helpers::*, Router}; use axum_core::extract::FromRef; use http::{header, request::Parts, StatusCode}; use tower_http::limit::RequestBodyLimitLayer; @@ -315,7 +313,6 @@ mod tests { struct RequireAuth; - #[async_trait::async_trait] impl FromRequestParts for RequireAuth where S: Send + Sync, @@ -368,7 +365,6 @@ mod tests { fn works_with_request_body_limit() { struct MyExtractor; - #[async_trait] impl FromRequestParts for MyExtractor where S: Send + Sync, diff --git a/examples/consume-body-in-extractor-or-middleware/src/main.rs b/examples/consume-body-in-extractor-or-middleware/src/main.rs index 107edb6f..96ad49cd 100644 --- a/examples/consume-body-in-extractor-or-middleware/src/main.rs +++ b/examples/consume-body-in-extractor-or-middleware/src/main.rs @@ -5,7 +5,6 @@ //! ``` use axum::{ - async_trait, body::{Body, Bytes}, extract::{FromRequest, Request}, http::StatusCode, @@ -74,7 +73,6 @@ async fn handler(BufferRequestBody(body): BufferRequestBody) { struct BufferRequestBody(Bytes); // we must implement `FromRequest` (and not `FromRequestParts`) to consume the body -#[async_trait] impl FromRequest for BufferRequestBody where S: Send + Sync, diff --git a/examples/customize-extractor-error/src/custom_extractor.rs b/examples/customize-extractor-error/src/custom_extractor.rs index 3611fba7..4f75fb44 100644 --- a/examples/customize-extractor-error/src/custom_extractor.rs +++ b/examples/customize-extractor-error/src/custom_extractor.rs @@ -5,7 +5,6 @@ //! - Boilerplate: Requires creating a new extractor for every custom rejection //! - Complexity: Manually implementing `FromRequest` results on more complex code use axum::{ - async_trait, extract::{rejection::JsonRejection, FromRequest, MatchedPath, Request}, http::StatusCode, response::IntoResponse, @@ -20,7 +19,6 @@ pub async fn handler(Json(value): Json) -> impl IntoResponse { // We define our own `Json` extractor that customizes the error from `axum::Json` pub struct Json(pub T); -#[async_trait] impl FromRequest for Json where axum::Json: FromRequest, diff --git a/examples/customize-path-rejection/src/main.rs b/examples/customize-path-rejection/src/main.rs index 4231eabf..a8ba05d5 100644 --- a/examples/customize-path-rejection/src/main.rs +++ b/examples/customize-path-rejection/src/main.rs @@ -5,7 +5,6 @@ //! ``` use axum::{ - async_trait, extract::{path::ErrorKind, rejection::PathRejection, FromRequestParts}, http::{request::Parts, StatusCode}, response::IntoResponse, @@ -49,7 +48,6 @@ struct Params { // We define our own `Path` extractor that customizes the error from `axum::extract::Path` struct Path(T); -#[async_trait] impl FromRequestParts for Path where // these trait bounds are copied from `impl FromRequest for axum::extract::path::Path` diff --git a/examples/diesel-async-postgres/src/main.rs b/examples/diesel-async-postgres/src/main.rs index ee42ac10..d018bda1 100644 --- a/examples/diesel-async-postgres/src/main.rs +++ b/examples/diesel-async-postgres/src/main.rs @@ -13,7 +13,6 @@ //! for a real world application using axum and diesel use axum::{ - async_trait, extract::{FromRef, FromRequestParts, State}, http::{request::Parts, StatusCode}, response::Json, @@ -102,7 +101,6 @@ struct DatabaseConnection( bb8::PooledConnection<'static, AsyncDieselConnectionManager>, ); -#[async_trait] impl FromRequestParts for DatabaseConnection where S: Send + Sync, diff --git a/examples/jwt/src/main.rs b/examples/jwt/src/main.rs index 85211851..f39cb696 100644 --- a/examples/jwt/src/main.rs +++ b/examples/jwt/src/main.rs @@ -7,7 +7,6 @@ //! ``` use axum::{ - async_trait, extract::FromRequestParts, http::{request::Parts, StatusCode}, response::{IntoResponse, Response}, @@ -122,7 +121,6 @@ impl AuthBody { } } -#[async_trait] impl FromRequestParts for Claims where S: Send + Sync, diff --git a/examples/oauth/src/main.rs b/examples/oauth/src/main.rs index 6ceffe8f..880e7e25 100644 --- a/examples/oauth/src/main.rs +++ b/examples/oauth/src/main.rs @@ -11,7 +11,6 @@ use anyhow::{Context, Result}; use async_session::{MemoryStore, Session, SessionStore}; use axum::{ - async_trait, extract::{FromRef, FromRequestParts, Query, State}, http::{header::SET_COOKIE, HeaderMap}, response::{IntoResponse, Redirect, Response}, @@ -249,7 +248,6 @@ impl IntoResponse for AuthRedirect { } } -#[async_trait] impl FromRequestParts for User where MemoryStore: FromRef, diff --git a/examples/parse-body-based-on-content-type/src/main.rs b/examples/parse-body-based-on-content-type/src/main.rs index bae4ec1d..ced01252 100644 --- a/examples/parse-body-based-on-content-type/src/main.rs +++ b/examples/parse-body-based-on-content-type/src/main.rs @@ -7,7 +7,6 @@ //! ``` use axum::{ - async_trait, extract::{FromRequest, Request}, http::{header::CONTENT_TYPE, StatusCode}, response::{IntoResponse, Response}, @@ -48,7 +47,6 @@ async fn handler(JsonOrForm(payload): JsonOrForm) { struct JsonOrForm(T); -#[async_trait] impl FromRequest for JsonOrForm where S: Send + Sync, diff --git a/examples/sqlx-postgres/src/main.rs b/examples/sqlx-postgres/src/main.rs index 46571115..e92d1ea0 100644 --- a/examples/sqlx-postgres/src/main.rs +++ b/examples/sqlx-postgres/src/main.rs @@ -14,7 +14,6 @@ //! ``` use axum::{ - async_trait, extract::{FromRef, FromRequestParts, State}, http::{request::Parts, StatusCode}, routing::get, @@ -75,7 +74,6 @@ async fn using_connection_pool_extractor( // which setup is appropriate depends on your application struct DatabaseConnection(sqlx::pool::PoolConnection); -#[async_trait] impl FromRequestParts for DatabaseConnection where PgPool: FromRef, diff --git a/examples/tokio-postgres/src/main.rs b/examples/tokio-postgres/src/main.rs index effc0320..c6f38085 100644 --- a/examples/tokio-postgres/src/main.rs +++ b/examples/tokio-postgres/src/main.rs @@ -5,7 +5,6 @@ //! ``` use axum::{ - async_trait, extract::{FromRef, FromRequestParts, State}, http::{request::Parts, StatusCode}, routing::get, @@ -68,7 +67,6 @@ async fn using_connection_pool_extractor( // which setup is appropriate depends on your application struct DatabaseConnection(PooledConnection<'static, PostgresConnectionManager>); -#[async_trait] impl FromRequestParts for DatabaseConnection where ConnectionPool: FromRef, diff --git a/examples/validator/Cargo.toml b/examples/validator/Cargo.toml index a1adc075..98d00cec 100644 --- a/examples/validator/Cargo.toml +++ b/examples/validator/Cargo.toml @@ -5,7 +5,6 @@ publish = false version = "0.1.0" [dependencies] -async-trait = "0.1.67" axum = { path = "../../axum" } http-body = "1.0.0" serde = { version = "1.0", features = ["derive"] } diff --git a/examples/validator/src/main.rs b/examples/validator/src/main.rs index 85c4ac18..d2aa5807 100644 --- a/examples/validator/src/main.rs +++ b/examples/validator/src/main.rs @@ -10,7 +10,6 @@ //! ->

Hello, LT!

//! ``` -use async_trait::async_trait; use axum::{ extract::{rejection::FormRejection, Form, FromRequest, Request}, http::StatusCode, @@ -56,7 +55,6 @@ async fn handler(ValidatedForm(input): ValidatedForm) -> Html #[derive(Debug, Clone, Copy, Default)] pub struct ValidatedForm(pub T); -#[async_trait] impl FromRequest for ValidatedForm where T: DeserializeOwned + Validate, diff --git a/examples/versioning/src/main.rs b/examples/versioning/src/main.rs index a1d96e83..f8317bf5 100644 --- a/examples/versioning/src/main.rs +++ b/examples/versioning/src/main.rs @@ -5,7 +5,6 @@ //! ``` use axum::{ - async_trait, extract::{FromRequestParts, Path}, http::{request::Parts, StatusCode}, response::{IntoResponse, Response}, @@ -47,7 +46,6 @@ enum Version { V3, } -#[async_trait] impl FromRequestParts for Version where S: Send + Sync,