Skip to content

Commit dea73f8

Browse files
davidpdrsnjplatte
andauthored
Remove B type param: axum-extra (#1775)
Co-authored-by: Jonas Platte <[email protected]>
1 parent a95da74 commit dea73f8

File tree

10 files changed

+108
-149
lines changed

10 files changed

+108
-149
lines changed

axum-extra/src/extract/cookie/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ mod tests {
255255
custom_key: CustomKey(Key::generate()),
256256
};
257257

258-
let app = Router::<_, Body>::new()
258+
let app = Router::new()
259259
.route("/set", get(set_cookie))
260260
.route("/get", get(get_cookie))
261261
.route("/remove", get(remove_cookie))
@@ -352,7 +352,7 @@ mod tests {
352352
custom_key: CustomKey(Key::generate()),
353353
};
354354

355-
let app = Router::<_, Body>::new()
355+
let app = Router::new()
356356
.route("/get", get(get_cookie))
357357
.with_state(state);
358358

axum-extra/src/extract/form.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use axum::{
22
async_trait,
3-
body::HttpBody,
3+
body::Body,
44
extract::{rejection::RawFormRejection, FromRequest, RawForm},
55
response::{IntoResponse, Response},
6-
BoxError, Error, RequestExt,
6+
Error, RequestExt,
77
};
88
use http::{Request, StatusCode};
99
use serde::de::DeserializeOwned;
@@ -52,17 +52,14 @@ impl<T> Deref for Form<T> {
5252
}
5353

5454
#[async_trait]
55-
impl<T, S, B> FromRequest<S, B> for Form<T>
55+
impl<T, S> FromRequest<S> for Form<T>
5656
where
5757
T: DeserializeOwned,
58-
B: HttpBody + Send + 'static,
59-
B::Data: Send,
60-
B::Error: Into<BoxError>,
6158
S: Send + Sync,
6259
{
6360
type Rejection = FormRejection;
6461

65-
async fn from_request(req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> {
62+
async fn from_request(req: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
6663
let RawForm(bytes) = req
6764
.extract()
6865
.await

axum-extra/src/extract/with_rejection.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use axum::async_trait;
2+
use axum::body::Body;
23
use axum::extract::{FromRequest, FromRequestParts};
34
use axum::response::IntoResponse;
45
use http::request::Parts;
@@ -109,16 +110,15 @@ impl<E, R> DerefMut for WithRejection<E, R> {
109110
}
110111

111112
#[async_trait]
112-
impl<B, E, R, S> FromRequest<S, B> for WithRejection<E, R>
113+
impl<E, R, S> FromRequest<S> for WithRejection<E, R>
113114
where
114-
B: Send + 'static,
115115
S: Send + Sync,
116-
E: FromRequest<S, B>,
116+
E: FromRequest<S>,
117117
R: From<E::Rejection> + IntoResponse,
118118
{
119119
type Rejection = R;
120120

121-
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
121+
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
122122
let extractor = E::from_request(req, state).await?;
123123
Ok(WithRejection(extractor, PhantomData))
124124
}
@@ -180,7 +180,7 @@ mod tests {
180180
}
181181
}
182182

183-
let req = Request::new(());
183+
let req = Request::new(Body::empty());
184184
let result = WithRejection::<TestExtractor, TestRejection>::from_request(req, &()).await;
185185
assert!(matches!(result, Err(TestRejection)));
186186

axum-extra/src/handler/mod.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Additional handler utilities.
22
3+
use axum::body::Body;
34
use axum::{
45
extract::FromRequest,
56
handler::Handler,
@@ -19,15 +20,15 @@ pub use self::or::Or;
1920
///
2021
/// The drawbacks of this trait is that you cannot apply middleware to individual handlers like you
2122
/// can with [`Handler::layer`].
22-
pub trait HandlerCallWithExtractors<T, S, B>: Sized {
23+
pub trait HandlerCallWithExtractors<T, S>: Sized {
2324
/// The type of future calling this handler returns.
2425
type Future: Future<Output = Response> + Send + 'static;
2526

2627
/// Call the handler with the extracted inputs.
27-
fn call(self, extractors: T, state: S) -> <Self as HandlerCallWithExtractors<T, S, B>>::Future;
28+
fn call(self, extractors: T, state: S) -> <Self as HandlerCallWithExtractors<T, S>>::Future;
2829

2930
/// Conver this `HandlerCallWithExtractors` into [`Handler`].
30-
fn into_handler(self) -> IntoHandler<Self, T, S, B> {
31+
fn into_handler(self) -> IntoHandler<Self, T, S> {
3132
IntoHandler {
3233
handler: self,
3334
_marker: PhantomData,
@@ -102,9 +103,9 @@ pub trait HandlerCallWithExtractors<T, S, B>: Sized {
102103
/// );
103104
/// # let _: Router = app;
104105
/// ```
105-
fn or<R, Rt>(self, rhs: R) -> Or<Self, R, T, Rt, S, B>
106+
fn or<R, Rt>(self, rhs: R) -> Or<Self, R, T, Rt, S>
106107
where
107-
R: HandlerCallWithExtractors<Rt, S, B>,
108+
R: HandlerCallWithExtractors<Rt, S>,
108109
{
109110
Or {
110111
lhs: self,
@@ -117,7 +118,7 @@ pub trait HandlerCallWithExtractors<T, S, B>: Sized {
117118
macro_rules! impl_handler_call_with {
118119
( $($ty:ident),* $(,)? ) => {
119120
#[allow(non_snake_case)]
120-
impl<F, Fut, S, B, $($ty,)*> HandlerCallWithExtractors<($($ty,)*), S, B> for F
121+
impl<F, Fut, S, $($ty,)*> HandlerCallWithExtractors<($($ty,)*), S> for F
121122
where
122123
F: FnOnce($($ty,)*) -> Fut,
123124
Fut: Future + Send + 'static,
@@ -130,7 +131,7 @@ macro_rules! impl_handler_call_with {
130131
self,
131132
($($ty,)*): ($($ty,)*),
132133
_state: S,
133-
) -> <Self as HandlerCallWithExtractors<($($ty,)*), S, B>>::Future {
134+
) -> <Self as HandlerCallWithExtractors<($($ty,)*), S>>::Future {
134135
self($($ty,)*).map(IntoResponse::into_response)
135136
}
136137
}
@@ -159,22 +160,22 @@ impl_handler_call_with!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,
159160
///
160161
/// Created with [`HandlerCallWithExtractors::into_handler`].
161162
#[allow(missing_debug_implementations)]
162-
pub struct IntoHandler<H, T, S, B> {
163+
pub struct IntoHandler<H, T, S> {
163164
handler: H,
164-
_marker: PhantomData<fn() -> (T, S, B)>,
165+
_marker: PhantomData<fn() -> (T, S)>,
165166
}
166167

167-
impl<H, T, S, B> Handler<T, S, B> for IntoHandler<H, T, S, B>
168+
impl<H, T, S> Handler<T, S> for IntoHandler<H, T, S>
168169
where
169-
H: HandlerCallWithExtractors<T, S, B> + Clone + Send + 'static,
170-
T: FromRequest<S, B> + Send + 'static,
170+
H: HandlerCallWithExtractors<T, S> + Clone + Send + 'static,
171+
T: FromRequest<S> + Send + 'static,
171172
T::Rejection: Send,
172-
B: Send + 'static,
173173
S: Send + Sync + 'static,
174174
{
175175
type Future = BoxFuture<'static, Response>;
176176

177-
fn call(self, req: http::Request<B>, state: S) -> Self::Future {
177+
fn call(self, req: http::Request<Body>, state: S) -> Self::Future {
178+
let req = req.map(Body::new);
178179
Box::pin(async move {
179180
match T::from_request(req, &state).await {
180181
Ok(t) => self.handler.call(t, state).await,
@@ -184,9 +185,9 @@ where
184185
}
185186
}
186187

187-
impl<H, T, S, B> Copy for IntoHandler<H, T, S, B> where H: Copy {}
188+
impl<H, T, S> Copy for IntoHandler<H, T, S> where H: Copy {}
188189

189-
impl<H, T, S, B> Clone for IntoHandler<H, T, S, B>
190+
impl<H, T, S> Clone for IntoHandler<H, T, S>
190191
where
191192
H: Clone,
192193
{

axum-extra/src/handler/or.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::HandlerCallWithExtractors;
22
use crate::either::Either;
33
use axum::{
4+
body::Body,
45
extract::{FromRequest, FromRequestParts},
56
handler::Handler,
67
http::Request,
@@ -14,19 +15,18 @@ use std::{future::Future, marker::PhantomData};
1415
///
1516
/// Created with [`HandlerCallWithExtractors::or`](super::HandlerCallWithExtractors::or).
1617
#[allow(missing_debug_implementations)]
17-
pub struct Or<L, R, Lt, Rt, S, B> {
18+
pub struct Or<L, R, Lt, Rt, S> {
1819
pub(super) lhs: L,
1920
pub(super) rhs: R,
20-
pub(super) _marker: PhantomData<fn() -> (Lt, Rt, S, B)>,
21+
pub(super) _marker: PhantomData<fn() -> (Lt, Rt, S)>,
2122
}
2223

23-
impl<S, B, L, R, Lt, Rt> HandlerCallWithExtractors<Either<Lt, Rt>, S, B> for Or<L, R, Lt, Rt, S, B>
24+
impl<S, L, R, Lt, Rt> HandlerCallWithExtractors<Either<Lt, Rt>, S> for Or<L, R, Lt, Rt, S>
2425
where
25-
L: HandlerCallWithExtractors<Lt, S, B> + Send + 'static,
26-
R: HandlerCallWithExtractors<Rt, S, B> + Send + 'static,
26+
L: HandlerCallWithExtractors<Lt, S> + Send + 'static,
27+
R: HandlerCallWithExtractors<Rt, S> + Send + 'static,
2728
Rt: Send + 'static,
2829
Lt: Send + 'static,
29-
B: Send + 'static,
3030
{
3131
// this puts `futures_util` in our public API but thats fine in axum-extra
3232
type Future = EitherFuture<
@@ -38,7 +38,7 @@ where
3838
self,
3939
extractors: Either<Lt, Rt>,
4040
state: S,
41-
) -> <Self as HandlerCallWithExtractors<Either<Lt, Rt>, S, B>>::Future {
41+
) -> <Self as HandlerCallWithExtractors<Either<Lt, Rt>, S>>::Future {
4242
match extractors {
4343
Either::E1(lt) => self
4444
.lhs
@@ -54,21 +54,20 @@ where
5454
}
5555
}
5656

57-
impl<S, B, L, R, Lt, Rt, M> Handler<(M, Lt, Rt), S, B> for Or<L, R, Lt, Rt, S, B>
57+
impl<S, L, R, Lt, Rt, M> Handler<(M, Lt, Rt), S> for Or<L, R, Lt, Rt, S>
5858
where
59-
L: HandlerCallWithExtractors<Lt, S, B> + Clone + Send + 'static,
60-
R: HandlerCallWithExtractors<Rt, S, B> + Clone + Send + 'static,
59+
L: HandlerCallWithExtractors<Lt, S> + Clone + Send + 'static,
60+
R: HandlerCallWithExtractors<Rt, S> + Clone + Send + 'static,
6161
Lt: FromRequestParts<S> + Send + 'static,
62-
Rt: FromRequest<S, B, M> + Send + 'static,
62+
Rt: FromRequest<S, M> + Send + 'static,
6363
Lt::Rejection: Send,
6464
Rt::Rejection: Send,
65-
B: Send + 'static,
6665
S: Send + Sync + 'static,
6766
{
6867
// this puts `futures_util` in our public API but thats fine in axum-extra
6968
type Future = BoxFuture<'static, Response>;
7069

71-
fn call(self, req: Request<B>, state: S) -> Self::Future {
70+
fn call(self, req: Request<Body>, state: S) -> Self::Future {
7271
Box::pin(async move {
7372
let (mut parts, body) = req.into_parts();
7473

@@ -86,14 +85,14 @@ where
8685
}
8786
}
8887

89-
impl<L, R, Lt, Rt, S, B> Copy for Or<L, R, Lt, Rt, S, B>
88+
impl<L, R, Lt, Rt, S> Copy for Or<L, R, Lt, Rt, S>
9089
where
9190
L: Copy,
9291
R: Copy,
9392
{
9493
}
9594

96-
impl<L, R, Lt, Rt, S, B> Clone for Or<L, R, Lt, Rt, S, B>
95+
impl<L, R, Lt, Rt, S> Clone for Or<L, R, Lt, Rt, S>
9796
where
9897
L: Clone,
9998
R: Clone,

axum-extra/src/json_lines.rs

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
33
use axum::{
44
async_trait,
5-
body::{HttpBody, StreamBody},
5+
body::{Body, StreamBody},
66
extract::FromRequest,
77
response::{IntoResponse, Response},
88
BoxError,
99
};
10-
use bytes::{BufMut, Bytes, BytesMut};
10+
use bytes::{BufMut, BytesMut};
1111
use futures_util::stream::{BoxStream, Stream, TryStream, TryStreamExt};
1212
use http::Request;
1313
use pin_project_lite::pin_project;
@@ -100,26 +100,19 @@ impl<S> JsonLines<S, AsResponse> {
100100
}
101101

102102
#[async_trait]
103-
impl<S, B, T> FromRequest<S, B> for JsonLines<T, AsExtractor>
103+
impl<S, T> FromRequest<S> for JsonLines<T, AsExtractor>
104104
where
105-
B: HttpBody + Send + 'static,
106-
B::Data: Into<Bytes>,
107-
B::Error: Into<BoxError>,
108105
T: DeserializeOwned,
109106
S: Send + Sync,
110107
{
111108
type Rejection = Infallible;
112109

113-
async fn from_request(req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> {
110+
async fn from_request(req: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
114111
// `Stream::lines` isn't a thing so we have to convert it into an `AsyncRead`
115112
// so we can call `AsyncRead::lines` and then convert it back to a `Stream`
116-
let body = BodyStream {
117-
body: req.into_body(),
118-
};
113+
let body = req.into_body();
119114

120-
let stream = body
121-
.map_ok(Into::into)
122-
.map_err(|err| io::Error::new(io::ErrorKind::Other, err));
115+
let stream = TryStreamExt::map_err(body, |err| io::Error::new(io::ErrorKind::Other, err));
123116
let read = StreamReader::new(stream);
124117
let lines_stream = LinesStream::new(read.lines());
125118

@@ -139,26 +132,6 @@ where
139132
}
140133
}
141134

142-
// like `axum::extract::BodyStream` except it doesn't box the inner body
143-
// we don't need that since we box the final stream in `Inner::Extractor`
144-
pin_project! {
145-
struct BodyStream<B> {
146-
#[pin]
147-
body: B,
148-
}
149-
}
150-
151-
impl<B> Stream for BodyStream<B>
152-
where
153-
B: HttpBody + Send + 'static,
154-
{
155-
type Item = Result<B::Data, B::Error>;
156-
157-
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
158-
self.project().body.poll_data(cx)
159-
}
160-
}
161-
162135
impl<T> Stream for JsonLines<T, AsExtractor> {
163136
type Item = Result<T, axum::Error>;
164137

axum-extra/src/protobuf.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
33
use axum::{
44
async_trait,
5-
body::{Bytes, HttpBody},
5+
body::Body,
66
extract::{rejection::BytesRejection, FromRequest},
77
response::{IntoResponse, Response},
8-
BoxError,
98
};
10-
use bytes::BytesMut;
9+
use bytes::{Bytes, BytesMut};
1110
use http::{Request, StatusCode};
1211
use prost::Message;
1312
use std::ops::{Deref, DerefMut};
@@ -97,17 +96,14 @@ use std::ops::{Deref, DerefMut};
9796
pub struct ProtoBuf<T>(pub T);
9897

9998
#[async_trait]
100-
impl<T, S, B> FromRequest<S, B> for ProtoBuf<T>
99+
impl<T, S> FromRequest<S> for ProtoBuf<T>
101100
where
102101
T: Message + Default,
103-
B: HttpBody + Send + 'static,
104-
B::Data: Send,
105-
B::Error: Into<BoxError>,
106102
S: Send + Sync,
107103
{
108104
type Rejection = ProtoBufRejection;
109105

110-
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
106+
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
111107
let mut bytes = Bytes::from_request(req, state).await?;
112108

113109
match T::decode(&mut bytes) {

0 commit comments

Comments
 (0)