Skip to content

Commit

Permalink
remove async-trait
Browse files Browse the repository at this point in the history
  • Loading branch information
lz1998 committed Nov 10, 2023
1 parent 3ff45d9 commit 5f9b379
Show file tree
Hide file tree
Showing 59 changed files with 52 additions and 224 deletions.
3 changes: 0 additions & 3 deletions axum-core/src/ext_traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S>(pub(crate) S);

#[async_trait]
impl<OuterState, InnerState> FromRequestParts<OuterState> for State<InnerState>
where
InnerState: FromRef<OuterState>,
Expand All @@ -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<S> FromRequestParts<S> for RequiresState
where
S: Send + Sync,
Expand Down
57 changes: 23 additions & 34 deletions axum-core/src/ext_traits/request.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::body::Body;
use crate::extract::{DefaultBodyLimitKind, FromRequest, FromRequestParts, Request};
use futures_util::future::BoxFuture;
use http_body::Limited;
use std::future::Future;

mod sealed {
pub trait Sealed {}
Expand All @@ -21,7 +21,6 @@ pub trait RequestExt: sealed::Sealed + Sized {
///
/// ```
/// use axum::{
/// async_trait,
/// extract::{Request, FromRequest},
/// body::Body,
/// http::{header::CONTENT_TYPE, StatusCode},
Expand All @@ -31,7 +30,6 @@ pub trait RequestExt: sealed::Sealed + Sized {
///
/// struct FormOrJson<T>(T);
///
/// #[async_trait]
/// impl<S, T> FromRequest<S> for FormOrJson<T>
/// where
/// Json<T>: FromRequest<()>,
Expand Down Expand Up @@ -68,7 +66,7 @@ pub trait RequestExt: sealed::Sealed + Sized {
/// }
/// }
/// ```
fn extract<E, M>(self) -> BoxFuture<'static, Result<E, E::Rejection>>
fn extract<E, M>(self) -> impl Future<Output = Result<E, E::Rejection>>
where
E: FromRequest<(), M> + 'static,
M: 'static;
Expand All @@ -84,7 +82,6 @@ pub trait RequestExt: sealed::Sealed + Sized {
///
/// ```
/// use axum::{
/// async_trait,
/// body::Body,
/// extract::{Request, FromRef, FromRequest},
/// RequestExt,
Expand All @@ -94,7 +91,6 @@ pub trait RequestExt: sealed::Sealed + Sized {
/// requires_state: RequiresState,
/// }
///
/// #[async_trait]
/// impl<S> FromRequest<S> for MyExtractor
/// where
/// String: FromRef<S>,
Expand All @@ -112,7 +108,6 @@ pub trait RequestExt: sealed::Sealed + Sized {
/// // some extractor that consumes the request body and requires state
/// struct RequiresState { /* ... */ }
///
/// #[async_trait]
/// impl<S> FromRequest<S> for RequiresState
/// where
/// String: FromRef<S>,
Expand All @@ -125,7 +120,10 @@ pub trait RequestExt: sealed::Sealed + Sized {
/// # }
/// }
/// ```
fn extract_with_state<E, S, M>(self, state: &S) -> BoxFuture<'_, Result<E, E::Rejection>>
fn extract_with_state<E, S, M>(
self,
state: &S,
) -> impl Future<Output = Result<E, E::Rejection>>
where
E: FromRequest<S, M> + 'static,
S: Send + Sync;
Expand All @@ -138,7 +136,6 @@ pub trait RequestExt: sealed::Sealed + Sized {
///
/// ```
/// use axum::{
/// async_trait,
/// extract::{Path, Request, FromRequest},
/// response::{IntoResponse, Response},
/// body::Body,
Expand All @@ -155,7 +152,6 @@ pub trait RequestExt: sealed::Sealed + Sized {
/// payload: T,
/// }
///
/// #[async_trait]
/// impl<S, T> FromRequest<S> for MyExtractor<T>
/// where
/// S: Send + Sync,
Expand All @@ -180,7 +176,7 @@ pub trait RequestExt: sealed::Sealed + Sized {
/// }
/// }
/// ```
fn extract_parts<E>(&mut self) -> BoxFuture<'_, Result<E, E::Rejection>>
fn extract_parts<E>(&mut self) -> impl Future<Output = Result<E, E::Rejection>>
where
E: FromRequestParts<()> + 'static;

Expand All @@ -192,7 +188,6 @@ pub trait RequestExt: sealed::Sealed + Sized {
///
/// ```
/// use axum::{
/// async_trait,
/// extract::{Request, FromRef, FromRequest, FromRequestParts},
/// http::request::Parts,
/// response::{IntoResponse, Response},
Expand All @@ -205,7 +200,6 @@ pub trait RequestExt: sealed::Sealed + Sized {
/// payload: T,
/// }
///
/// #[async_trait]
/// impl<S, T> FromRequest<S> for MyExtractor<T>
/// where
/// String: FromRef<S>,
Expand Down Expand Up @@ -235,7 +229,6 @@ pub trait RequestExt: sealed::Sealed + Sized {
///
/// struct RequiresState {}
///
/// #[async_trait]
/// impl<S> FromRequestParts<S> for RequiresState
/// where
/// String: FromRef<S>,
Expand All @@ -251,7 +244,7 @@ pub trait RequestExt: sealed::Sealed + Sized {
fn extract_parts_with_state<'a, E, S>(
&'a mut self,
state: &'a S,
) -> BoxFuture<'a, Result<E, E::Rejection>>
) -> impl Future<Output = Result<E, E::Rejection>> + 'a
where
E: FromRequestParts<S> + 'static,
S: Send + Sync;
Expand All @@ -268,33 +261,33 @@ pub trait RequestExt: sealed::Sealed + Sized {
}

impl RequestExt for Request {
fn extract<E, M>(self) -> BoxFuture<'static, Result<E, E::Rejection>>
async fn extract<E, M>(self) -> Result<E, E::Rejection>
where
E: FromRequest<(), M> + 'static,
M: 'static,
{
self.extract_with_state(&())
self.extract_with_state(&()).await
}

fn extract_with_state<E, S, M>(self, state: &S) -> BoxFuture<'_, Result<E, E::Rejection>>
async fn extract_with_state<E, S, M>(self, state: &S) -> Result<E, E::Rejection>
where
E: FromRequest<S, M> + 'static,
S: Send + Sync,
{
E::from_request(self, state)
E::from_request(self, state).await
}

fn extract_parts<E>(&mut self) -> BoxFuture<'_, Result<E, E::Rejection>>
async fn extract_parts<E>(&mut self) -> Result<E, E::Rejection>
where
E: FromRequestParts<()> + 'static,
{
self.extract_parts_with_state(&())
self.extract_parts_with_state(&()).await
}

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<E, E::Rejection>>
) -> Result<E, E::Rejection>
where
E: FromRequestParts<S> + 'static,
S: Send + Sync,
Expand All @@ -307,17 +300,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) -> Result<Request<Limited<Body>>, Request> {
Expand Down Expand Up @@ -348,7 +339,6 @@ mod tests {
ext_traits::tests::{RequiresState, State},
extract::FromRef,
};
use async_trait::async_trait;
use http::Method;

#[tokio::test]
Expand Down Expand Up @@ -417,7 +407,6 @@ mod tests {
body: String,
}

#[async_trait]
impl<S> FromRequest<S> for WorksForCustomExtractor
where
S: Send + Sync,
Expand Down
24 changes: 7 additions & 17 deletions axum-core/src/ext_traits/request_parts.rs
Original file line number Diff line number Diff line change
@@ -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 {}
Expand All @@ -21,7 +21,6 @@ pub trait RequestPartsExt: sealed::Sealed + Sized {
/// response::{Response, IntoResponse},
/// http::request::Parts,
/// RequestPartsExt,
/// async_trait,
/// };
/// use std::collections::HashMap;
///
Expand All @@ -30,7 +29,6 @@ pub trait RequestPartsExt: sealed::Sealed + Sized {
/// query_params: HashMap<String, String>,
/// }
///
/// #[async_trait]
/// impl<S> FromRequestParts<S> for MyExtractor
/// where
/// S: Send + Sync,
Expand All @@ -54,7 +52,7 @@ pub trait RequestPartsExt: sealed::Sealed + Sized {
/// }
/// }
/// ```
fn extract<E>(&mut self) -> BoxFuture<'_, Result<E, E::Rejection>>
fn extract<E>(&mut self) -> impl Future<Output = Result<E, E::Rejection>> + '_
where
E: FromRequestParts<()> + 'static;

Expand All @@ -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<S> FromRequestParts<S> for MyExtractor
/// where
/// String: FromRef<S>,
Expand All @@ -97,7 +93,6 @@ pub trait RequestPartsExt: sealed::Sealed + Sized {
/// struct RequiresState { /* ... */ }
///
/// // some extractor that requires a `String` in the state
/// #[async_trait]
/// impl<S> FromRequestParts<S> for RequiresState
/// where
/// String: FromRef<S>,
Expand All @@ -113,29 +108,26 @@ pub trait RequestPartsExt: sealed::Sealed + Sized {
fn extract_with_state<'a, E, S>(
&'a mut self,
state: &'a S,
) -> BoxFuture<'a, Result<E, E::Rejection>>
) -> impl Future<Output = Result<E, E::Rejection>> + 'a
where
E: FromRequestParts<S> + 'static,
S: Send + Sync;
}

impl RequestPartsExt for Parts {
fn extract<E>(&mut self) -> BoxFuture<'_, Result<E, E::Rejection>>
async fn extract<E>(&mut self) -> Result<E, E::Rejection>
where
E: FromRequestParts<()> + 'static,
{
self.extract_with_state(&())
self.extract_with_state(&()).await
}

fn extract_with_state<'a, E, S>(
&'a mut self,
state: &'a S,
) -> BoxFuture<'a, Result<E, E::Rejection>>
async fn extract_with_state<'a, E, S>(&'a mut self, state: &'a S) -> Result<E, E::Rejection>
where
E: FromRequestParts<S> + 'static,
S: Send + Sync,
{
E::from_request_parts(self, state)
E::from_request_parts(self, state).await
}
}

Expand All @@ -148,7 +140,6 @@ mod tests {
ext_traits::tests::{RequiresState, State},
extract::FromRef,
};
use async_trait::async_trait;
use http::{Method, Request};

#[tokio::test]
Expand Down Expand Up @@ -181,7 +172,6 @@ mod tests {
from_state: String,
}

#[async_trait]
impl<S> FromRequestParts<S> for WorksForCustomExtractor
where
S: Send + Sync,
Expand Down
Loading

0 comments on commit 5f9b379

Please sign in to comment.