Skip to content

Introduce Response type alias as a shorthand for Response<BoxBody> #590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions axum-core/src/extract/tuple.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::{FromRequest, RequestParts};
use crate::{body::BoxBody, response::IntoResponse};
use crate::response::{IntoResponse, Response};
use async_trait::async_trait;
use http::Response;
use std::convert::Infallible;

#[async_trait]
Expand All @@ -27,7 +26,7 @@ macro_rules! impl_from_request {
$( $ty: FromRequest<B> + Send, )*
B: Send,
{
type Rejection = Response<BoxBody>;
type Rejection = Response;

async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
$( let $ty = $ty::from_request(req).await.map_err(|err| err.into_response())?; )*
Expand Down
6 changes: 3 additions & 3 deletions axum-core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ macro_rules! define_rejection {

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

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

impl $crate::response::IntoResponse for $name {
fn into_response(self) -> http::Response<$crate::body::BoxBody> {
fn into_response(self) -> $crate::response::Response {
match self {
$(
Self::$variant(inner) => inner.into_response(),
Expand Down
14 changes: 7 additions & 7 deletions axum-core/src/response/headers.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::IntoResponse;
use crate::body::{boxed, BoxBody};
use super::{IntoResponse, Response};
use crate::body::boxed;
use bytes::Bytes;
use http::{
header::{HeaderMap, HeaderName, HeaderValue},
Response, StatusCode,
StatusCode,
};
use http_body::{Empty, Full};
use std::{convert::TryInto, fmt};
Expand Down Expand Up @@ -55,7 +55,7 @@ use std::{convert::TryInto, fmt};
pub struct Headers<H>(pub H);

impl<H> Headers<H> {
fn try_into_header_map<K, V>(self) -> Result<HeaderMap, Response<BoxBody>>
fn try_into_header_map<K, V>(self) -> Result<HeaderMap, Response>
where
H: IntoIterator<Item = (K, V)>,
K: TryInto<HeaderName>,
Expand Down Expand Up @@ -93,7 +93,7 @@ where
V: TryInto<HeaderValue>,
V::Error: fmt::Display,
{
fn into_response(self) -> http::Response<BoxBody> {
fn into_response(self) -> Response {
let headers = self.try_into_header_map();

match headers {
Expand All @@ -116,7 +116,7 @@ where
V: TryInto<HeaderValue>,
V::Error: fmt::Display,
{
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
let headers = match self.0.try_into_header_map() {
Ok(headers) => headers,
Err(res) => return res,
Expand All @@ -135,7 +135,7 @@ where
V: TryInto<HeaderValue>,
V::Error: fmt::Display,
{
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
let headers = match self.1.try_into_header_map() {
Ok(headers) => headers,
Err(res) => return res,
Expand Down
68 changes: 36 additions & 32 deletions axum-core/src/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
use bytes::Bytes;
use http::{
header::{self, HeaderMap, HeaderValue},
Response, StatusCode,
StatusCode,
};
use http_body::{
combinators::{MapData, MapErr},
Expand All @@ -24,6 +24,10 @@ mod headers;
#[doc(inline)]
pub use self::headers::Headers;

/// Type alias for [`http::Response`] whose body type defaults to [`BoxBody`], the most common body
/// type used with Axum.
pub type Response<T = BoxBody> = http::Response<T>;

/// Trait for generating responses.
///
/// Types that implement `IntoResponse` can be returned from handlers.
Expand All @@ -39,10 +43,10 @@ pub use self::headers::Headers;
/// ```rust
/// use axum::{
/// Router,
/// body::{self, BoxBody, Bytes},
/// body::{self, Bytes},
/// routing::get,
/// http::{Response, StatusCode},
/// response::IntoResponse,
/// http::StatusCode,
/// response::{IntoResponse, Response},
/// };
///
/// enum MyError {
Expand All @@ -51,7 +55,7 @@ pub use self::headers::Headers;
/// }
///
/// impl IntoResponse for MyError {
/// fn into_response(self) -> Response<BoxBody> {
/// fn into_response(self) -> Response {
/// let body = match self {
/// MyError::SomethingWentWrong => {
/// body::boxed(body::Full::from("something went wrong"))
Expand Down Expand Up @@ -84,13 +88,13 @@ pub use self::headers::Headers;
///
/// ```rust
/// use axum::{
/// body::{self, BoxBody},
/// body,
/// routing::get,
/// response::IntoResponse,
/// response::{IntoResponse, Response},
/// Router,
/// };
/// use http_body::Body;
/// use http::{Response, HeaderMap};
/// use http::HeaderMap;
/// use bytes::Bytes;
/// use std::{
/// convert::Infallible,
Expand Down Expand Up @@ -125,7 +129,7 @@ pub use self::headers::Headers;
///
/// // Now we can implement `IntoResponse` directly for `MyBody`
/// impl IntoResponse for MyBody {
/// fn into_response(self) -> Response<BoxBody> {
/// fn into_response(self) -> Response {
/// Response::new(body::boxed(self))
/// }
/// }
Expand All @@ -141,17 +145,17 @@ pub use self::headers::Headers;
/// ```
pub trait IntoResponse {
/// Create a response.
fn into_response(self) -> Response<BoxBody>;
fn into_response(self) -> Response;
}

impl IntoResponse for () {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
Response::new(boxed(Empty::new()))
}
}

impl IntoResponse for Infallible {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
match self {}
}
}
Expand All @@ -161,7 +165,7 @@ where
T: IntoResponse,
E: IntoResponse,
{
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
match self {
Ok(value) => value.into_response(),
Err(err) => err.into_response(),
Expand All @@ -174,15 +178,15 @@ where
B: http_body::Body<Data = Bytes> + Send + 'static,
B::Error: Into<BoxError>,
{
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
self.map(boxed)
}
}

macro_rules! impl_into_response_for_body {
($body:ty) => {
impl IntoResponse for $body {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
Response::new(boxed(self))
}
}
Expand All @@ -193,7 +197,7 @@ impl_into_response_for_body!(Full<Bytes>);
impl_into_response_for_body!(Empty<Bytes>);

impl IntoResponse for http::response::Parts {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
Response::from_parts(self, boxed(Empty::new()))
}
}
Expand All @@ -202,7 +206,7 @@ impl<E> IntoResponse for http_body::combinators::BoxBody<Bytes, E>
where
E: Into<BoxError> + 'static,
{
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
Response::new(boxed(self))
}
}
Expand All @@ -211,7 +215,7 @@ impl<E> IntoResponse for http_body::combinators::UnsyncBoxBody<Bytes, E>
where
E: Into<BoxError> + 'static,
{
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
Response::new(boxed(self))
}
}
Expand All @@ -222,7 +226,7 @@ where
F: FnMut(B::Data) -> Bytes + Send + 'static,
B::Error: Into<BoxError>,
{
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
Response::new(boxed(self))
}
}
Expand All @@ -233,27 +237,27 @@ where
F: FnMut(B::Error) -> E + Send + 'static,
E: Into<BoxError>,
{
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
Response::new(boxed(self))
}
}

impl IntoResponse for &'static str {
#[inline]
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
Cow::Borrowed(self).into_response()
}
}

impl IntoResponse for String {
#[inline]
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
Cow::<'static, str>::Owned(self).into_response()
}
}

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

impl IntoResponse for Bytes {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
let mut res = Response::new(boxed(Full::from(self)));
res.headers_mut().insert(
header::CONTENT_TYPE,
Expand All @@ -275,7 +279,7 @@ impl IntoResponse for Bytes {
}

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

impl IntoResponse for Vec<u8> {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
let mut res = Response::new(boxed(Full::from(self)));
res.headers_mut().insert(
header::CONTENT_TYPE,
Expand All @@ -297,7 +301,7 @@ impl IntoResponse for Vec<u8> {
}

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

impl IntoResponse for StatusCode {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
Response::builder()
.status(self)
.body(boxed(Empty::new()))
Expand All @@ -320,7 +324,7 @@ impl<T> IntoResponse for (StatusCode, T)
where
T: IntoResponse,
{
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
let mut res = self.1.into_response();
*res.status_mut() = self.0;
res
Expand All @@ -331,7 +335,7 @@ impl<T> IntoResponse for (HeaderMap, T)
where
T: IntoResponse,
{
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
let mut res = self.1.into_response();
res.headers_mut().extend(self.0);
res
Expand All @@ -342,7 +346,7 @@ impl<T> IntoResponse for (StatusCode, HeaderMap, T)
where
T: IntoResponse,
{
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
let mut res = self.2.into_response();
*res.status_mut() = self.0;
res.headers_mut().extend(self.1);
Expand All @@ -351,7 +355,7 @@ where
}

impl IntoResponse for HeaderMap {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
let mut res = Response::new(boxed(Empty::new()));
*res.headers_mut() = self;
res
Expand Down
5 changes: 2 additions & 3 deletions axum-debug/tests/pass/returns_self.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use axum::{
body::BoxBody,
http::Response,
response::IntoResponse,
response::{IntoResponse, Response},
};
use axum_debug::debug_handler;

Expand All @@ -15,7 +14,7 @@ impl A {
}

impl IntoResponse for A {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
todo!()
}
}
Expand Down
Loading