Skip to content

Commit dfb06e7

Browse files
author
Kai Jewson
authored
Introduce Response type alias as a shorthand for Response<BoxBody> (#590)
* Introduce `Response` type alias as a shorthand * Don't re-export `Response` at the crate root
1 parent cbb3486 commit dfb06e7

File tree

41 files changed

+208
-235
lines changed

Some content is hidden

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

41 files changed

+208
-235
lines changed

axum-core/src/extract/tuple.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use super::{FromRequest, RequestParts};
2-
use crate::{body::BoxBody, response::IntoResponse};
2+
use crate::response::{IntoResponse, Response};
33
use async_trait::async_trait;
4-
use http::Response;
54
use std::convert::Infallible;
65

76
#[async_trait]
@@ -27,7 +26,7 @@ macro_rules! impl_from_request {
2726
$( $ty: FromRequest<B> + Send, )*
2827
B: Send,
2928
{
30-
type Rejection = Response<BoxBody>;
29+
type Rejection = Response;
3130

3231
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
3332
$( let $ty = $ty::from_request(req).await.map_err(|err| err.into_response())?; )*

axum-core/src/macros.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ macro_rules! define_rejection {
1212

1313
#[allow(deprecated)]
1414
impl $crate::response::IntoResponse for $name {
15-
fn into_response(self) -> http::Response<$crate::body::BoxBody> {
15+
fn into_response(self) -> $crate::response::Response {
1616
let mut res = http::Response::new($crate::body::boxed(http_body::Full::from($body)));
1717
*res.status_mut() = http::StatusCode::$status;
1818
res
@@ -54,7 +54,7 @@ macro_rules! define_rejection {
5454
}
5555

5656
impl crate::response::IntoResponse for $name {
57-
fn into_response(self) -> http::Response<$crate::body::BoxBody> {
57+
fn into_response(self) -> $crate::response::Response {
5858
let body = http_body::Full::from(format!(concat!($body, ": {}"), self.0));
5959
let body = $crate::body::boxed(body);
6060
let mut res =
@@ -97,7 +97,7 @@ macro_rules! composite_rejection {
9797
}
9898

9999
impl $crate::response::IntoResponse for $name {
100-
fn into_response(self) -> http::Response<$crate::body::BoxBody> {
100+
fn into_response(self) -> $crate::response::Response {
101101
match self {
102102
$(
103103
Self::$variant(inner) => inner.into_response(),

axum-core/src/response/headers.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use super::IntoResponse;
2-
use crate::body::{boxed, BoxBody};
1+
use super::{IntoResponse, Response};
2+
use crate::body::boxed;
33
use bytes::Bytes;
44
use http::{
55
header::{HeaderMap, HeaderName, HeaderValue},
6-
Response, StatusCode,
6+
StatusCode,
77
};
88
use http_body::{Empty, Full};
99
use std::{convert::TryInto, fmt};
@@ -55,7 +55,7 @@ use std::{convert::TryInto, fmt};
5555
pub struct Headers<H>(pub H);
5656

5757
impl<H> Headers<H> {
58-
fn try_into_header_map<K, V>(self) -> Result<HeaderMap, Response<BoxBody>>
58+
fn try_into_header_map<K, V>(self) -> Result<HeaderMap, Response>
5959
where
6060
H: IntoIterator<Item = (K, V)>,
6161
K: TryInto<HeaderName>,
@@ -93,7 +93,7 @@ where
9393
V: TryInto<HeaderValue>,
9494
V::Error: fmt::Display,
9595
{
96-
fn into_response(self) -> http::Response<BoxBody> {
96+
fn into_response(self) -> Response {
9797
let headers = self.try_into_header_map();
9898

9999
match headers {
@@ -116,7 +116,7 @@ where
116116
V: TryInto<HeaderValue>,
117117
V::Error: fmt::Display,
118118
{
119-
fn into_response(self) -> Response<BoxBody> {
119+
fn into_response(self) -> Response {
120120
let headers = match self.0.try_into_header_map() {
121121
Ok(headers) => headers,
122122
Err(res) => return res,
@@ -135,7 +135,7 @@ where
135135
V: TryInto<HeaderValue>,
136136
V::Error: fmt::Display,
137137
{
138-
fn into_response(self) -> Response<BoxBody> {
138+
fn into_response(self) -> Response {
139139
let headers = match self.1.try_into_header_map() {
140140
Ok(headers) => headers,
141141
Err(res) => return res,

axum-core/src/response/mod.rs

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
use bytes::Bytes;
1212
use http::{
1313
header::{self, HeaderMap, HeaderValue},
14-
Response, StatusCode,
14+
StatusCode,
1515
};
1616
use http_body::{
1717
combinators::{MapData, MapErr},
@@ -24,6 +24,10 @@ mod headers;
2424
#[doc(inline)]
2525
pub use self::headers::Headers;
2626

27+
/// Type alias for [`http::Response`] whose body type defaults to [`BoxBody`], the most common body
28+
/// type used with Axum.
29+
pub type Response<T = BoxBody> = http::Response<T>;
30+
2731
/// Trait for generating responses.
2832
///
2933
/// Types that implement `IntoResponse` can be returned from handlers.
@@ -39,10 +43,10 @@ pub use self::headers::Headers;
3943
/// ```rust
4044
/// use axum::{
4145
/// Router,
42-
/// body::{self, BoxBody, Bytes},
46+
/// body::{self, Bytes},
4347
/// routing::get,
44-
/// http::{Response, StatusCode},
45-
/// response::IntoResponse,
48+
/// http::StatusCode,
49+
/// response::{IntoResponse, Response},
4650
/// };
4751
///
4852
/// enum MyError {
@@ -51,7 +55,7 @@ pub use self::headers::Headers;
5155
/// }
5256
///
5357
/// impl IntoResponse for MyError {
54-
/// fn into_response(self) -> Response<BoxBody> {
58+
/// fn into_response(self) -> Response {
5559
/// let body = match self {
5660
/// MyError::SomethingWentWrong => {
5761
/// body::boxed(body::Full::from("something went wrong"))
@@ -84,13 +88,13 @@ pub use self::headers::Headers;
8488
///
8589
/// ```rust
8690
/// use axum::{
87-
/// body::{self, BoxBody},
91+
/// body,
8892
/// routing::get,
89-
/// response::IntoResponse,
93+
/// response::{IntoResponse, Response},
9094
/// Router,
9195
/// };
9296
/// use http_body::Body;
93-
/// use http::{Response, HeaderMap};
97+
/// use http::HeaderMap;
9498
/// use bytes::Bytes;
9599
/// use std::{
96100
/// convert::Infallible,
@@ -125,7 +129,7 @@ pub use self::headers::Headers;
125129
///
126130
/// // Now we can implement `IntoResponse` directly for `MyBody`
127131
/// impl IntoResponse for MyBody {
128-
/// fn into_response(self) -> Response<BoxBody> {
132+
/// fn into_response(self) -> Response {
129133
/// Response::new(body::boxed(self))
130134
/// }
131135
/// }
@@ -141,17 +145,17 @@ pub use self::headers::Headers;
141145
/// ```
142146
pub trait IntoResponse {
143147
/// Create a response.
144-
fn into_response(self) -> Response<BoxBody>;
148+
fn into_response(self) -> Response;
145149
}
146150

147151
impl IntoResponse for () {
148-
fn into_response(self) -> Response<BoxBody> {
152+
fn into_response(self) -> Response {
149153
Response::new(boxed(Empty::new()))
150154
}
151155
}
152156

153157
impl IntoResponse for Infallible {
154-
fn into_response(self) -> Response<BoxBody> {
158+
fn into_response(self) -> Response {
155159
match self {}
156160
}
157161
}
@@ -161,7 +165,7 @@ where
161165
T: IntoResponse,
162166
E: IntoResponse,
163167
{
164-
fn into_response(self) -> Response<BoxBody> {
168+
fn into_response(self) -> Response {
165169
match self {
166170
Ok(value) => value.into_response(),
167171
Err(err) => err.into_response(),
@@ -174,15 +178,15 @@ where
174178
B: http_body::Body<Data = Bytes> + Send + 'static,
175179
B::Error: Into<BoxError>,
176180
{
177-
fn into_response(self) -> Response<BoxBody> {
181+
fn into_response(self) -> Response {
178182
self.map(boxed)
179183
}
180184
}
181185

182186
macro_rules! impl_into_response_for_body {
183187
($body:ty) => {
184188
impl IntoResponse for $body {
185-
fn into_response(self) -> Response<BoxBody> {
189+
fn into_response(self) -> Response {
186190
Response::new(boxed(self))
187191
}
188192
}
@@ -193,7 +197,7 @@ impl_into_response_for_body!(Full<Bytes>);
193197
impl_into_response_for_body!(Empty<Bytes>);
194198

195199
impl IntoResponse for http::response::Parts {
196-
fn into_response(self) -> Response<BoxBody> {
200+
fn into_response(self) -> Response {
197201
Response::from_parts(self, boxed(Empty::new()))
198202
}
199203
}
@@ -202,7 +206,7 @@ impl<E> IntoResponse for http_body::combinators::BoxBody<Bytes, E>
202206
where
203207
E: Into<BoxError> + 'static,
204208
{
205-
fn into_response(self) -> Response<BoxBody> {
209+
fn into_response(self) -> Response {
206210
Response::new(boxed(self))
207211
}
208212
}
@@ -211,7 +215,7 @@ impl<E> IntoResponse for http_body::combinators::UnsyncBoxBody<Bytes, E>
211215
where
212216
E: Into<BoxError> + 'static,
213217
{
214-
fn into_response(self) -> Response<BoxBody> {
218+
fn into_response(self) -> Response {
215219
Response::new(boxed(self))
216220
}
217221
}
@@ -222,7 +226,7 @@ where
222226
F: FnMut(B::Data) -> Bytes + Send + 'static,
223227
B::Error: Into<BoxError>,
224228
{
225-
fn into_response(self) -> Response<BoxBody> {
229+
fn into_response(self) -> Response {
226230
Response::new(boxed(self))
227231
}
228232
}
@@ -233,27 +237,27 @@ where
233237
F: FnMut(B::Error) -> E + Send + 'static,
234238
E: Into<BoxError>,
235239
{
236-
fn into_response(self) -> Response<BoxBody> {
240+
fn into_response(self) -> Response {
237241
Response::new(boxed(self))
238242
}
239243
}
240244

241245
impl IntoResponse for &'static str {
242246
#[inline]
243-
fn into_response(self) -> Response<BoxBody> {
247+
fn into_response(self) -> Response {
244248
Cow::Borrowed(self).into_response()
245249
}
246250
}
247251

248252
impl IntoResponse for String {
249253
#[inline]
250-
fn into_response(self) -> Response<BoxBody> {
254+
fn into_response(self) -> Response {
251255
Cow::<'static, str>::Owned(self).into_response()
252256
}
253257
}
254258

255259
impl IntoResponse for Cow<'static, str> {
256-
fn into_response(self) -> Response<BoxBody> {
260+
fn into_response(self) -> Response {
257261
let mut res = Response::new(boxed(Full::from(self)));
258262
res.headers_mut().insert(
259263
header::CONTENT_TYPE,
@@ -264,7 +268,7 @@ impl IntoResponse for Cow<'static, str> {
264268
}
265269

266270
impl IntoResponse for Bytes {
267-
fn into_response(self) -> Response<BoxBody> {
271+
fn into_response(self) -> Response {
268272
let mut res = Response::new(boxed(Full::from(self)));
269273
res.headers_mut().insert(
270274
header::CONTENT_TYPE,
@@ -275,7 +279,7 @@ impl IntoResponse for Bytes {
275279
}
276280

277281
impl IntoResponse for &'static [u8] {
278-
fn into_response(self) -> Response<BoxBody> {
282+
fn into_response(self) -> Response {
279283
let mut res = Response::new(boxed(Full::from(self)));
280284
res.headers_mut().insert(
281285
header::CONTENT_TYPE,
@@ -286,7 +290,7 @@ impl IntoResponse for &'static [u8] {
286290
}
287291

288292
impl IntoResponse for Vec<u8> {
289-
fn into_response(self) -> Response<BoxBody> {
293+
fn into_response(self) -> Response {
290294
let mut res = Response::new(boxed(Full::from(self)));
291295
res.headers_mut().insert(
292296
header::CONTENT_TYPE,
@@ -297,7 +301,7 @@ impl IntoResponse for Vec<u8> {
297301
}
298302

299303
impl IntoResponse for Cow<'static, [u8]> {
300-
fn into_response(self) -> Response<BoxBody> {
304+
fn into_response(self) -> Response {
301305
let mut res = Response::new(boxed(Full::from(self)));
302306
res.headers_mut().insert(
303307
header::CONTENT_TYPE,
@@ -308,7 +312,7 @@ impl IntoResponse for Cow<'static, [u8]> {
308312
}
309313

310314
impl IntoResponse for StatusCode {
311-
fn into_response(self) -> Response<BoxBody> {
315+
fn into_response(self) -> Response {
312316
Response::builder()
313317
.status(self)
314318
.body(boxed(Empty::new()))
@@ -320,7 +324,7 @@ impl<T> IntoResponse for (StatusCode, T)
320324
where
321325
T: IntoResponse,
322326
{
323-
fn into_response(self) -> Response<BoxBody> {
327+
fn into_response(self) -> Response {
324328
let mut res = self.1.into_response();
325329
*res.status_mut() = self.0;
326330
res
@@ -331,7 +335,7 @@ impl<T> IntoResponse for (HeaderMap, T)
331335
where
332336
T: IntoResponse,
333337
{
334-
fn into_response(self) -> Response<BoxBody> {
338+
fn into_response(self) -> Response {
335339
let mut res = self.1.into_response();
336340
res.headers_mut().extend(self.0);
337341
res
@@ -342,7 +346,7 @@ impl<T> IntoResponse for (StatusCode, HeaderMap, T)
342346
where
343347
T: IntoResponse,
344348
{
345-
fn into_response(self) -> Response<BoxBody> {
349+
fn into_response(self) -> Response {
346350
let mut res = self.2.into_response();
347351
*res.status_mut() = self.0;
348352
res.headers_mut().extend(self.1);
@@ -351,7 +355,7 @@ where
351355
}
352356

353357
impl IntoResponse for HeaderMap {
354-
fn into_response(self) -> Response<BoxBody> {
358+
fn into_response(self) -> Response {
355359
let mut res = Response::new(boxed(Empty::new()));
356360
*res.headers_mut() = self;
357361
res

axum-debug/tests/pass/returns_self.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use axum::{
22
body::BoxBody,
3-
http::Response,
4-
response::IntoResponse,
3+
response::{IntoResponse, Response},
54
};
65
use axum_debug::debug_handler;
76

@@ -15,7 +14,7 @@ impl A {
1514
}
1615

1716
impl IntoResponse for A {
18-
fn into_response(self) -> Response<BoxBody> {
17+
fn into_response(self) -> Response {
1918
todo!()
2019
}
2120
}

0 commit comments

Comments
 (0)