Replace async_trait with AFIT / RPITIT (#2308)

Co-authored-by: Jonas Platte <[email protected]>
This commit is contained in:
Zheng Li
2024-09-28 21:27:11 +00:00
committed by GitHub
co-authored by Jonas Platte
parent dda5a273b5
commit 19101f624d
69 changed files with 116 additions and 267 deletions
-3
View File
@@ -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>,
@@ -33,7 +31,6 @@ mod tests {
#[allow(dead_code)]
pub(crate) struct RequiresState(pub(crate) String);
#[async_trait]
impl<S> FromRequestParts<S> for RequiresState
where
S: Send + Sync,
+23 -31
View File
@@ -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>(T);
///
/// #[async_trait]
/// impl<S, T> FromRequest<S> for FormOrJson<T>
/// where
/// Json<T>: FromRequest<()>,
@@ -67,7 +65,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>> + 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<S> FromRequest<S> for MyExtractor
/// where
/// String: FromRef<S>,
@@ -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<S> FromRequest<S> for RequiresState
/// where
/// String: FromRef<S>,
@@ -124,7 +119,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>> + Send
where
E: FromRequest<S, M> + '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<S, T> FromRequest<S> for MyExtractor<T>
/// where
/// S: Send + Sync,
@@ -179,7 +175,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>> + 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<S, T> FromRequest<S> for MyExtractor<T>
/// where
/// String: FromRef<S>,
@@ -234,7 +228,6 @@ pub trait RequestExt: sealed::Sealed + Sized {
///
/// struct RequiresState {}
///
/// #[async_trait]
/// impl<S> FromRequestParts<S> for RequiresState
/// where
/// String: FromRef<S>,
@@ -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<E, E::Rejection>>
) -> impl Future<Output = Result<E, E::Rejection>> + Send + 'a
where
E: FromRequestParts<S> + 'static,
S: Send + Sync;
@@ -267,7 +260,7 @@ pub trait RequestExt: sealed::Sealed + Sized {
}
impl RequestExt for Request {
fn extract<E, M>(self) -> BoxFuture<'static, Result<E, E::Rejection>>
fn extract<E, M>(self) -> impl Future<Output = Result<E, E::Rejection>> + Send
where
E: FromRequest<(), M> + 'static,
M: 'static,
@@ -275,7 +268,10 @@ impl RequestExt for Request {
self.extract_with_state(&())
}
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>> + Send
where
E: FromRequest<S, M> + 'static,
S: Send + Sync,
@@ -283,17 +279,17 @@ impl RequestExt for Request {
E::from_request(self, state)
}
fn extract_parts<E>(&mut self) -> BoxFuture<'_, Result<E, E::Rejection>>
fn extract_parts<E>(&mut self) -> impl Future<Output = Result<E, E::Rejection>> + 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<E, E::Rejection>>
) -> Result<E, E::Rejection>
where
E: FromRequestParts<S> + '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<S> FromRequest<S> for WorksForCustomExtractor
where
S: Send + Sync,
+5 -12
View File
@@ -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<String, String>,
/// }
///
/// #[async_trait]
/// impl<S> FromRequestParts<S> for MyExtractor
/// where
/// S: Send + Sync,
@@ -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>> + 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<S> FromRequestParts<S> for MyExtractor
/// where
/// String: FromRef<S>,
@@ -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>,
@@ -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<E, E::Rejection>>
) -> impl Future<Output = Result<E, E::Rejection>> + Send + 'a
where
E: FromRequestParts<S> + 'static,
S: Send + Sync;
}
impl RequestPartsExt for Parts {
fn extract<E>(&mut self) -> BoxFuture<'_, Result<E, E::Rejection>>
fn extract<E>(&mut self) -> impl Future<Output = Result<E, E::Rejection>> + 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<E, E::Rejection>>
) -> impl Future<Output = Result<E, E::Rejection>> + Send + 'a
where
E: FromRequestParts<S> + '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<S> FromRequestParts<S> for WorksForCustomExtractor
where
S: Send + Sync,
+9 -10
View File
@@ -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]
#[rustversion::attr(
since(1.78),
diagnostic::on_unimplemented(
@@ -55,7 +54,10 @@ pub trait FromRequestParts<S>: Sized {
type Rejection: IntoResponse;
/// Perform the extraction.
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection>;
fn from_request_parts(
parts: &mut Parts,
state: &S,
) -> impl Future<Output = Result<Self, Self::Rejection>> + Send;
}
/// Types that can be created from requests.
@@ -69,7 +71,6 @@ pub trait FromRequestParts<S>: Sized {
/// See [`axum::extract`] for more general docs about extractors.
///
/// [`axum::extract`]: https://docs.rs/axum/0.7/axum/extract/index.html
#[async_trait]
#[rustversion::attr(
since(1.78),
diagnostic::on_unimplemented(
@@ -82,10 +83,12 @@ pub trait FromRequest<S, M = private::ViaRequest>: Sized {
type Rejection: IntoResponse;
/// Perform the extraction.
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection>;
fn from_request(
req: Request,
state: &S,
) -> impl Future<Output = Result<Self, Self::Rejection>> + Send;
}
#[async_trait]
impl<S, T> FromRequest<S, private::ViaParts> for T
where
S: Send + Sync,
@@ -99,7 +102,6 @@ where
}
}
#[async_trait]
impl<S, T> FromRequestParts<S> for Option<T>
where
T: FromRequestParts<S>,
@@ -115,7 +117,6 @@ where
}
}
#[async_trait]
impl<S, T> FromRequest<S> for Option<T>
where
T: FromRequest<S>,
@@ -128,7 +129,6 @@ where
}
}
#[async_trait]
impl<S, T> FromRequestParts<S> for Result<T, T::Rejection>
where
T: FromRequestParts<S>,
@@ -141,7 +141,6 @@ where
}
}
#[async_trait]
impl<S, T> FromRequest<S> for Result<T, T::Rejection>
where
T: FromRequest<S>,
-12
View File
@@ -1,12 +1,10 @@
use super::{rejection::*, FromRequest, FromRequestParts, Request};
use crate::{body::Body, RequestExt};
use async_trait::async_trait;
use bytes::{BufMut, Bytes, BytesMut};
use http::{request::Parts, Extensions, HeaderMap, Method, Uri, Version};
use http_body_util::BodyExt;
use std::convert::Infallible;
#[async_trait]
impl<S> FromRequest<S> for Request
where
S: Send + Sync,
@@ -18,7 +16,6 @@ where
}
}
#[async_trait]
impl<S> FromRequestParts<S> for Method
where
S: Send + Sync,
@@ -30,7 +27,6 @@ where
}
}
#[async_trait]
impl<S> FromRequestParts<S> for Uri
where
S: Send + Sync,
@@ -42,7 +38,6 @@ where
}
}
#[async_trait]
impl<S> FromRequestParts<S> 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<S> FromRequestParts<S> for HeaderMap
where
S: Send + Sync,
@@ -71,7 +65,6 @@ where
}
}
#[async_trait]
impl<S> FromRequest<S> for BytesMut
where
S: Send + Sync,
@@ -102,7 +95,6 @@ async fn body_to_bytes_mut(body: &mut Body, bytes: &mut BytesMut) -> Result<(),
Ok(())
}
#[async_trait]
impl<S> FromRequest<S> for Bytes
where
S: Send + Sync,
@@ -121,7 +113,6 @@ where
}
}
#[async_trait]
impl<S> FromRequest<S> for String
where
S: Send + Sync,
@@ -143,7 +134,6 @@ where
}
}
#[async_trait]
impl<S> FromRequestParts<S> for Parts
where
S: Send + Sync,
@@ -155,7 +145,6 @@ where
}
}
#[async_trait]
impl<S> FromRequestParts<S> for Extensions
where
S: Send + Sync,
@@ -167,7 +156,6 @@ where
}
}
#[async_trait]
impl<S> FromRequest<S> for Body
where
S: Send + Sync,
-4
View File
@@ -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<S> FromRequestParts<S> 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<S, $($ty,)* $last> FromRequestParts<S> 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<S, Mut>` for `T: FromRequestParts<S>`.
#[async_trait]
#[allow(non_snake_case, unused_mut, unused_variables)]
impl<S, $($ty,)* $last> FromRequest<S> for ($($ty,)* $last,)
where