Skip to content

Commit 19101f6

Browse files
lz1998jplatte
andauthored
Replace async_trait with AFIT / RPITIT (#2308)
Co-authored-by: Jonas Platte <[email protected]>
1 parent dda5a27 commit 19101f6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+115
-266
lines changed

axum-core/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ tracing = ["dep:tracing"]
1818
__private_docs = ["dep:tower-http"]
1919

2020
[dependencies]
21-
async-trait = "0.1.67"
2221
bytes = "1.2"
2322
futures-util = { version = "0.3", default-features = false, features = ["alloc"] }
2423
http = "1.0.0"

axum-core/src/ext_traits/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ mod tests {
66
use std::convert::Infallible;
77

88
use crate::extract::{FromRef, FromRequestParts};
9-
use async_trait::async_trait;
109
use http::request::Parts;
1110

1211
#[derive(Debug, Default, Clone, Copy)]
1312
pub(crate) struct State<S>(pub(crate) S);
1413

15-
#[async_trait]
1614
impl<OuterState, InnerState> FromRequestParts<OuterState> for State<InnerState>
1715
where
1816
InnerState: FromRef<OuterState>,
@@ -33,7 +31,6 @@ mod tests {
3331
#[allow(dead_code)]
3432
pub(crate) struct RequiresState(pub(crate) String);
3533

36-
#[async_trait]
3734
impl<S> FromRequestParts<S> for RequiresState
3835
where
3936
S: Send + Sync,

axum-core/src/ext_traits/request.rs

+23-31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::body::Body;
22
use crate::extract::{DefaultBodyLimitKind, FromRequest, FromRequestParts, Request};
3-
use futures_util::future::BoxFuture;
3+
use std::future::Future;
44

55
mod sealed {
66
pub trait Sealed {}
@@ -20,7 +20,6 @@ pub trait RequestExt: sealed::Sealed + Sized {
2020
///
2121
/// ```
2222
/// use axum::{
23-
/// async_trait,
2423
/// extract::{Request, FromRequest},
2524
/// body::Body,
2625
/// http::{header::CONTENT_TYPE, StatusCode},
@@ -30,7 +29,6 @@ pub trait RequestExt: sealed::Sealed + Sized {
3029
///
3130
/// struct FormOrJson<T>(T);
3231
///
33-
/// #[async_trait]
3432
/// impl<S, T> FromRequest<S> for FormOrJson<T>
3533
/// where
3634
/// Json<T>: FromRequest<()>,
@@ -67,7 +65,7 @@ pub trait RequestExt: sealed::Sealed + Sized {
6765
/// }
6866
/// }
6967
/// ```
70-
fn extract<E, M>(self) -> BoxFuture<'static, Result<E, E::Rejection>>
68+
fn extract<E, M>(self) -> impl Future<Output = Result<E, E::Rejection>> + Send
7169
where
7270
E: FromRequest<(), M> + 'static,
7371
M: 'static;
@@ -83,7 +81,6 @@ pub trait RequestExt: sealed::Sealed + Sized {
8381
///
8482
/// ```
8583
/// use axum::{
86-
/// async_trait,
8784
/// body::Body,
8885
/// extract::{Request, FromRef, FromRequest},
8986
/// RequestExt,
@@ -93,7 +90,6 @@ pub trait RequestExt: sealed::Sealed + Sized {
9390
/// requires_state: RequiresState,
9491
/// }
9592
///
96-
/// #[async_trait]
9793
/// impl<S> FromRequest<S> for MyExtractor
9894
/// where
9995
/// String: FromRef<S>,
@@ -111,7 +107,6 @@ pub trait RequestExt: sealed::Sealed + Sized {
111107
/// // some extractor that consumes the request body and requires state
112108
/// struct RequiresState { /* ... */ }
113109
///
114-
/// #[async_trait]
115110
/// impl<S> FromRequest<S> for RequiresState
116111
/// where
117112
/// String: FromRef<S>,
@@ -124,7 +119,10 @@ pub trait RequestExt: sealed::Sealed + Sized {
124119
/// # }
125120
/// }
126121
/// ```
127-
fn extract_with_state<E, S, M>(self, state: &S) -> BoxFuture<'_, Result<E, E::Rejection>>
122+
fn extract_with_state<E, S, M>(
123+
self,
124+
state: &S,
125+
) -> impl Future<Output = Result<E, E::Rejection>> + Send
128126
where
129127
E: FromRequest<S, M> + 'static,
130128
S: Send + Sync;
@@ -137,7 +135,6 @@ pub trait RequestExt: sealed::Sealed + Sized {
137135
///
138136
/// ```
139137
/// use axum::{
140-
/// async_trait,
141138
/// extract::{Path, Request, FromRequest},
142139
/// response::{IntoResponse, Response},
143140
/// body::Body,
@@ -154,7 +151,6 @@ pub trait RequestExt: sealed::Sealed + Sized {
154151
/// payload: T,
155152
/// }
156153
///
157-
/// #[async_trait]
158154
/// impl<S, T> FromRequest<S> for MyExtractor<T>
159155
/// where
160156
/// S: Send + Sync,
@@ -179,7 +175,7 @@ pub trait RequestExt: sealed::Sealed + Sized {
179175
/// }
180176
/// }
181177
/// ```
182-
fn extract_parts<E>(&mut self) -> BoxFuture<'_, Result<E, E::Rejection>>
178+
fn extract_parts<E>(&mut self) -> impl Future<Output = Result<E, E::Rejection>> + Send
183179
where
184180
E: FromRequestParts<()> + 'static;
185181

@@ -191,7 +187,6 @@ pub trait RequestExt: sealed::Sealed + Sized {
191187
///
192188
/// ```
193189
/// use axum::{
194-
/// async_trait,
195190
/// extract::{Request, FromRef, FromRequest, FromRequestParts},
196191
/// http::request::Parts,
197192
/// response::{IntoResponse, Response},
@@ -204,7 +199,6 @@ pub trait RequestExt: sealed::Sealed + Sized {
204199
/// payload: T,
205200
/// }
206201
///
207-
/// #[async_trait]
208202
/// impl<S, T> FromRequest<S> for MyExtractor<T>
209203
/// where
210204
/// String: FromRef<S>,
@@ -234,7 +228,6 @@ pub trait RequestExt: sealed::Sealed + Sized {
234228
///
235229
/// struct RequiresState {}
236230
///
237-
/// #[async_trait]
238231
/// impl<S> FromRequestParts<S> for RequiresState
239232
/// where
240233
/// String: FromRef<S>,
@@ -250,7 +243,7 @@ pub trait RequestExt: sealed::Sealed + Sized {
250243
fn extract_parts_with_state<'a, E, S>(
251244
&'a mut self,
252245
state: &'a S,
253-
) -> BoxFuture<'a, Result<E, E::Rejection>>
246+
) -> impl Future<Output = Result<E, E::Rejection>> + Send + 'a
254247
where
255248
E: FromRequestParts<S> + 'static,
256249
S: Send + Sync;
@@ -267,33 +260,36 @@ pub trait RequestExt: sealed::Sealed + Sized {
267260
}
268261

269262
impl RequestExt for Request {
270-
fn extract<E, M>(self) -> BoxFuture<'static, Result<E, E::Rejection>>
263+
fn extract<E, M>(self) -> impl Future<Output = Result<E, E::Rejection>> + Send
271264
where
272265
E: FromRequest<(), M> + 'static,
273266
M: 'static,
274267
{
275268
self.extract_with_state(&())
276269
}
277270

278-
fn extract_with_state<E, S, M>(self, state: &S) -> BoxFuture<'_, Result<E, E::Rejection>>
271+
fn extract_with_state<E, S, M>(
272+
self,
273+
state: &S,
274+
) -> impl Future<Output = Result<E, E::Rejection>> + Send
279275
where
280276
E: FromRequest<S, M> + 'static,
281277
S: Send + Sync,
282278
{
283279
E::from_request(self, state)
284280
}
285281

286-
fn extract_parts<E>(&mut self) -> BoxFuture<'_, Result<E, E::Rejection>>
282+
fn extract_parts<E>(&mut self) -> impl Future<Output = Result<E, E::Rejection>> + Send
287283
where
288284
E: FromRequestParts<()> + 'static,
289285
{
290286
self.extract_parts_with_state(&())
291287
}
292288

293-
fn extract_parts_with_state<'a, E, S>(
289+
async fn extract_parts_with_state<'a, E, S>(
294290
&'a mut self,
295291
state: &'a S,
296-
) -> BoxFuture<'a, Result<E, E::Rejection>>
292+
) -> Result<E, E::Rejection>
297293
where
298294
E: FromRequestParts<S> + 'static,
299295
S: Send + Sync,
@@ -306,17 +302,15 @@ impl RequestExt for Request {
306302
*req.extensions_mut() = std::mem::take(self.extensions_mut());
307303
let (mut parts, ()) = req.into_parts();
308304

309-
Box::pin(async move {
310-
let result = E::from_request_parts(&mut parts, state).await;
305+
let result = E::from_request_parts(&mut parts, state).await;
311306

312-
*self.version_mut() = parts.version;
313-
*self.method_mut() = parts.method.clone();
314-
*self.uri_mut() = parts.uri.clone();
315-
*self.headers_mut() = std::mem::take(&mut parts.headers);
316-
*self.extensions_mut() = std::mem::take(&mut parts.extensions);
307+
*self.version_mut() = parts.version;
308+
*self.method_mut() = parts.method.clone();
309+
*self.uri_mut() = parts.uri.clone();
310+
*self.headers_mut() = std::mem::take(&mut parts.headers);
311+
*self.extensions_mut() = std::mem::take(&mut parts.extensions);
317312

318-
result
319-
})
313+
result
320314
}
321315

322316
fn with_limited_body(self) -> Request {
@@ -345,7 +339,6 @@ mod tests {
345339
ext_traits::tests::{RequiresState, State},
346340
extract::FromRef,
347341
};
348-
use async_trait::async_trait;
349342
use http::Method;
350343

351344
#[tokio::test]
@@ -414,7 +407,6 @@ mod tests {
414407
body: String,
415408
}
416409

417-
#[async_trait]
418410
impl<S> FromRequest<S> for WorksForCustomExtractor
419411
where
420412
S: Send + Sync,

axum-core/src/ext_traits/request_parts.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::extract::FromRequestParts;
2-
use futures_util::future::BoxFuture;
32
use http::request::Parts;
3+
use std::future::Future;
44

55
mod sealed {
66
pub trait Sealed {}
@@ -21,7 +21,6 @@ pub trait RequestPartsExt: sealed::Sealed + Sized {
2121
/// response::{Response, IntoResponse},
2222
/// http::request::Parts,
2323
/// RequestPartsExt,
24-
/// async_trait,
2524
/// };
2625
/// use std::collections::HashMap;
2726
///
@@ -30,7 +29,6 @@ pub trait RequestPartsExt: sealed::Sealed + Sized {
3029
/// query_params: HashMap<String, String>,
3130
/// }
3231
///
33-
/// #[async_trait]
3432
/// impl<S> FromRequestParts<S> for MyExtractor
3533
/// where
3634
/// S: Send + Sync,
@@ -54,7 +52,7 @@ pub trait RequestPartsExt: sealed::Sealed + Sized {
5452
/// }
5553
/// }
5654
/// ```
57-
fn extract<E>(&mut self) -> BoxFuture<'_, Result<E, E::Rejection>>
55+
fn extract<E>(&mut self) -> impl Future<Output = Result<E, E::Rejection>> + Send
5856
where
5957
E: FromRequestParts<()> + 'static;
6058

@@ -70,14 +68,12 @@ pub trait RequestPartsExt: sealed::Sealed + Sized {
7068
/// response::{Response, IntoResponse},
7169
/// http::request::Parts,
7270
/// RequestPartsExt,
73-
/// async_trait,
7471
/// };
7572
///
7673
/// struct MyExtractor {
7774
/// requires_state: RequiresState,
7875
/// }
7976
///
80-
/// #[async_trait]
8177
/// impl<S> FromRequestParts<S> for MyExtractor
8278
/// where
8379
/// String: FromRef<S>,
@@ -97,7 +93,6 @@ pub trait RequestPartsExt: sealed::Sealed + Sized {
9793
/// struct RequiresState { /* ... */ }
9894
///
9995
/// // some extractor that requires a `String` in the state
100-
/// #[async_trait]
10196
/// impl<S> FromRequestParts<S> for RequiresState
10297
/// where
10398
/// String: FromRef<S>,
@@ -113,14 +108,14 @@ pub trait RequestPartsExt: sealed::Sealed + Sized {
113108
fn extract_with_state<'a, E, S>(
114109
&'a mut self,
115110
state: &'a S,
116-
) -> BoxFuture<'a, Result<E, E::Rejection>>
111+
) -> impl Future<Output = Result<E, E::Rejection>> + Send + 'a
117112
where
118113
E: FromRequestParts<S> + 'static,
119114
S: Send + Sync;
120115
}
121116

122117
impl RequestPartsExt for Parts {
123-
fn extract<E>(&mut self) -> BoxFuture<'_, Result<E, E::Rejection>>
118+
fn extract<E>(&mut self) -> impl Future<Output = Result<E, E::Rejection>> + Send
124119
where
125120
E: FromRequestParts<()> + 'static,
126121
{
@@ -130,7 +125,7 @@ impl RequestPartsExt for Parts {
130125
fn extract_with_state<'a, E, S>(
131126
&'a mut self,
132127
state: &'a S,
133-
) -> BoxFuture<'a, Result<E, E::Rejection>>
128+
) -> impl Future<Output = Result<E, E::Rejection>> + Send + 'a
134129
where
135130
E: FromRequestParts<S> + 'static,
136131
S: Send + Sync,
@@ -148,7 +143,6 @@ mod tests {
148143
ext_traits::tests::{RequiresState, State},
149144
extract::FromRef,
150145
};
151-
use async_trait::async_trait;
152146
use http::{Method, Request};
153147

154148
#[tokio::test]
@@ -181,7 +175,6 @@ mod tests {
181175
from_state: String,
182176
}
183177

184-
#[async_trait]
185178
impl<S> FromRequestParts<S> for WorksForCustomExtractor
186179
where
187180
S: Send + Sync,

0 commit comments

Comments
 (0)