mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-27 00:00:24 +02:00
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
This commit is contained in:
@@ -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]
|
||||
@@ -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())?; )*
|
||||
|
||||
@@ -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
|
||||
@@ -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 =
|
||||
@@ -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(),
|
||||
|
||||
@@ -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};
|
||||
@@ -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>,
|
||||
@@ -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 {
|
||||
@@ -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,
|
||||
@@ -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,
|
||||
|
||||
@@ -11,7 +11,7 @@ use crate::{
|
||||
use bytes::Bytes;
|
||||
use http::{
|
||||
header::{self, HeaderMap, HeaderValue},
|
||||
Response, StatusCode,
|
||||
StatusCode,
|
||||
};
|
||||
use http_body::{
|
||||
combinators::{MapData, MapErr},
|
||||
@@ -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.
|
||||
@@ -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 {
|
||||
@@ -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"))
|
||||
@@ -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,
|
||||
@@ -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))
|
||||
/// }
|
||||
/// }
|
||||
@@ -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 {}
|
||||
}
|
||||
}
|
||||
@@ -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(),
|
||||
@@ -174,7 +178,7 @@ 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)
|
||||
}
|
||||
}
|
||||
@@ -182,7 +186,7 @@ where
|
||||
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))
|
||||
}
|
||||
}
|
||||
@@ -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()))
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
@@ -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,
|
||||
@@ -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,
|
||||
@@ -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,
|
||||
@@ -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,
|
||||
@@ -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()))
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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);
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user