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:
Kai Jewson 2021-12-05 18:16:46 +00:00 committed by GitHub
parent cbb34869d8
commit dfb06e721c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
41 changed files with 210 additions and 237 deletions

View file

@ -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())?; )*

View file

@ -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(),

View file

@ -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,

View file

@ -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

View file

@ -1,7 +1,6 @@
use axum::{
body::BoxBody,
http::Response,
response::IntoResponse,
response::{IntoResponse, Response},
};
use axum_debug::debug_handler;
@ -15,7 +14,7 @@ impl A {
}
impl IntoResponse for A {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
todo!()
}
}

View file

@ -1,12 +1,10 @@
use axum::{
async_trait,
body::BoxBody,
extract::{
rejection::{ExtensionRejection, ExtensionsAlreadyExtracted},
Extension, FromRequest, RequestParts,
},
http::Response,
response::IntoResponse,
response::{IntoResponse, Response},
};
use std::{
fmt,
@ -31,8 +29,8 @@ use std::{
/// async_trait,
/// extract::{FromRequest, RequestParts},
/// body::BoxBody,
/// response::IntoResponse,
/// http::{StatusCode, Response},
/// response::{IntoResponse, Response},
/// http::StatusCode,
/// };
///
/// #[derive(Clone)]
@ -58,7 +56,7 @@ use std::{
/// where
/// B: Send,
/// {
/// type Rejection = Response<BoxBody>;
/// type Rejection = Response;
///
/// async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
/// // loading a `CurrentUser` requires first loading the `Session`
@ -157,7 +155,7 @@ impl<R> IntoResponse for CachedRejection<R>
where
R: IntoResponse,
{
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
match self {
Self::ExtensionsAlreadyExtracted(inner) => inner.into_response(),
Self::Inner(inner) => inner.into_response(),

View file

@ -1,7 +1,7 @@
use axum::{
body::{self, BoxBody, Full},
http::{header, HeaderValue, Response, StatusCode},
response::IntoResponse,
body::{self, Full},
http::{header, HeaderValue, StatusCode},
response::{IntoResponse, Response},
};
use serde::Serialize;
@ -39,7 +39,7 @@ impl ErasedJson {
}
impl IntoResponse for ErasedJson {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
let bytes = match self.0 {
Ok(res) => res,
Err(err) => {

View file

@ -1,8 +1,9 @@
use super::HasRoutes;
use axum::{
body::{Body, BoxBody},
body::Body,
handler::Handler,
http::{Request, Response},
http::Request,
response::Response,
routing::{delete, get, on, post, MethodFilter},
Router,
};
@ -139,10 +140,7 @@ impl<B: Send + 'static> Resource<B> {
/// The routes will be nested at `/{resource_name}/:{resource_name}_id`.
pub fn nest<T>(mut self, svc: T) -> Self
where
T: Service<Request<B>, Response = Response<BoxBody>, Error = Infallible>
+ Clone
+ Send
+ 'static,
T: Service<Request<B>, Response = Response, Error = Infallible> + Clone + Send + 'static,
T::Future: Send + 'static,
{
let path = self.show_update_destroy_path();
@ -155,10 +153,7 @@ impl<B: Send + 'static> Resource<B> {
/// The routes will be nested at `/{resource_name}`.
pub fn nest_collection<T>(mut self, svc: T) -> Self
where
T: Service<Request<B>, Response = Response<BoxBody>, Error = Infallible>
+ Clone
+ Send
+ 'static,
T: Service<Request<B>, Response = Response, Error = Infallible> + Clone + Send + 'static,
T::Future: Send + 'static,
{
let path = self.index_create_path();
@ -176,10 +171,7 @@ impl<B: Send + 'static> Resource<B> {
fn route<T>(mut self, path: &str, svc: T) -> Self
where
T: Service<Request<B>, Response = Response<BoxBody>, Error = Infallible>
+ Clone
+ Send
+ 'static,
T: Service<Request<B>, Response = Response, Error = Infallible> + Clone + Send + 'static,
T::Future: Send + 'static,
{
self.router = self.router.route(path, svc);

View file

@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
# Unreleased
- None.
- **added:** `axum::response::Response` now exists as a shorthand for writing `Response<BoxBody>`.
# 0.4.0 (02. December, 2021)

View file

@ -1,6 +1,6 @@
use crate::{
body::{self, BoxBody},
response::IntoResponse,
body,
response::{IntoResponse, Response},
BoxError, Error,
};
use bytes::Bytes;
@ -8,7 +8,7 @@ use futures_util::{
ready,
stream::{self, TryStream},
};
use http::{HeaderMap, Response};
use http::HeaderMap;
use http_body::Body;
use pin_project_lite::pin_project;
use std::{
@ -81,7 +81,7 @@ where
S::Ok: Into<Bytes>,
S::Error: Into<BoxError>,
{
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
Response::new(body::boxed(self))
}
}

View file

@ -24,7 +24,7 @@ async fn handler() -> Result<String, StatusCode> {
While it looks like it might fail with a `StatusCode` this actually isn't an
"error". If this handler returns `Err(some_status_code)` that will still be
converted into a [`Response<_>`] and sent back to the client. This is done
converted into a [`Response`] and sent back to the client. This is done
through `StatusCode`'s [`IntoResponse`] implementation.
It doesn't matter whether you return `Err(StatusCode::NOT_FOUND)` or
@ -171,5 +171,5 @@ async fn handle_timeout_error(
[`tower::Service`]: `tower::Service`
[`Infallible`]: std::convert::Infallible
[`Response<_>`]: http::Response
[`Response`]: crate::response::Response
[`IntoResponse`]: crate::response::IntoResponse

View file

@ -55,11 +55,12 @@ compatible with axum. Some commonly used middleware are:
```rust,no_run
use axum::{
body::{Body, BoxBody},
routing::get,
http::{Request, Response},
error_handling::HandleErrorLayer,
response::Response,
Router,
body::{Body, BoxBody},
error_handling::HandleErrorLayer,
http::Request,
routing::get,
};
use tower::{
filter::AsyncFilterLayer,
@ -90,7 +91,7 @@ async fn map_request(req: Request<Body>) -> Result<Request<Body>, Infallible> {
Ok(req)
}
async fn map_response(res: Response<BoxBody>) -> Result<Response<BoxBody>, Infallible> {
async fn map_response(res: Response) -> Result<Response, Infallible> {
Ok(res)
}
@ -112,10 +113,11 @@ You can also write you own middleware by implementing [`tower::Service`]:
```rust
use axum::{
body::{Body, BoxBody},
routing::get,
http::{Request, Response},
response::Response,
Router,
body::{Body, BoxBody},
http::Request,
routing::get,
};
use futures::future::BoxFuture;
use tower::{Service, layer::layer_fn};
@ -128,7 +130,7 @@ struct MyMiddleware<S> {
impl<S> Service<Request<Body>> for MyMiddleware<S>
where
S: Service<Request<Body>, Response = Response<BoxBody>> + Clone + Send + 'static,
S: Service<Request<Body>, Response = Response> + Clone + Send + 'static,
S::Future: Send + 'static,
{
type Response = S::Response;
@ -148,7 +150,7 @@ where
let mut inner = std::mem::replace(&mut self.inner, clone);
Box::pin(async move {
let res: Response<BoxBody> = inner.call(req).await?;
let res: Response = inner.call(req).await?;
println!("`MyMiddleware` received the response");

View file

@ -1,10 +1,10 @@
#![doc = include_str!("../docs/error_handling.md")]
use crate::{
body::{boxed, BoxBody, Bytes, Full, HttpBody},
body::{boxed, Bytes, Full, HttpBody},
extract::{FromRequest, RequestParts},
http::{Request, Response, StatusCode},
response::IntoResponse,
http::{Request, StatusCode},
response::{IntoResponse, Response},
BoxError,
};
use std::{
@ -126,7 +126,7 @@ where
ResBody: HttpBody<Data = Bytes> + Send + 'static,
ResBody::Error: Into<BoxError>,
{
type Response = Response<BoxBody>;
type Response = Response;
type Error = Infallible;
type Future = future::HandleErrorFuture;
@ -168,7 +168,7 @@ macro_rules! impl_service {
ResBody: HttpBody<Data = Bytes> + Send + 'static,
ResBody::Error: Into<BoxError>,
{
type Response = Response<BoxBody>;
type Response = Response;
type Error = Infallible;
type Future = future::HandleErrorFuture;
@ -221,8 +221,7 @@ all_the_tuples!(impl_service);
pub mod future {
//! Future types.
use crate::body::BoxBody;
use http::Response;
use crate::response::Response;
use pin_project_lite::pin_project;
use std::{
convert::Infallible,
@ -235,7 +234,7 @@ pub mod future {
/// Response future for [`HandleError`].
pub struct HandleErrorFuture {
#[pin]
pub(super) future: Pin<Box<dyn Future<Output = Result<Response<BoxBody>, Infallible>>
pub(super) future: Pin<Box<dyn Future<Output = Result<Response, Infallible>>
+ Send
+ 'static
>>,
@ -243,7 +242,7 @@ pub mod future {
}
impl Future for HandleErrorFuture {
type Output = Result<Response<BoxBody>, Infallible>;
type Output = Result<Response, Infallible>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.project().future.poll(cx)

View file

@ -3,10 +3,13 @@
//! See [`extractor_middleware`] for more details.
use super::{FromRequest, RequestParts};
use crate::{body::BoxBody, response::IntoResponse, BoxError};
use crate::{
response::{IntoResponse, Response},
BoxError,
};
use bytes::Bytes;
use futures_util::{future::BoxFuture, ready};
use http::{Request, Response};
use http::Request;
use pin_project_lite::pin_project;
use std::{
fmt,
@ -170,7 +173,7 @@ where
ResBody: http_body::Body<Data = Bytes> + Send + 'static,
ResBody::Error: Into<BoxError>,
{
type Response = Response<BoxBody>;
type Response = Response;
type Error = S::Error;
type Future = ResponseFuture<ReqBody, S, E>;
@ -229,7 +232,7 @@ where
ResBody: http_body::Body<Data = Bytes> + Send + 'static,
ResBody::Error: Into<BoxError>,
{
type Output = Result<Response<BoxBody>, S::Error>;
type Output = Result<Response, S::Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
loop {

View file

@ -1,6 +1,5 @@
#![doc = include_str!("../docs/extract.md")]
use crate::response::IntoResponse;
use http::header;
use rejection::*;

View file

@ -4,9 +4,9 @@
mod de;
use crate::{
body::{boxed, BoxBody, Full},
body::{boxed, Full},
extract::{rejection::*, FromRequest, RequestParts},
response::IntoResponse,
response::{IntoResponse, Response},
routing::{InvalidUtf8InPathParam, UrlParams},
};
use async_trait::async_trait;
@ -370,7 +370,7 @@ impl FailedToDeserializePathParams {
}
impl IntoResponse for FailedToDeserializePathParams {
fn into_response(self) -> http::Response<BoxBody> {
fn into_response(self) -> Response {
let (status, body) = match self.0.kind {
ErrorKind::Message(_)
| ErrorKind::InvalidUtf8InPathParam { .. }
@ -385,7 +385,7 @@ impl IntoResponse for FailedToDeserializePathParams {
(StatusCode::INTERNAL_SERVER_ERROR, self.0.kind.to_string())
}
};
let mut res = http::Response::new(boxed(Full::from(body)));
let mut res = Response::new(boxed(Full::from(body)));
*res.status_mut() = status;
res
}

View file

@ -1,8 +1,8 @@
//! Rejection response types.
use super::IntoResponse;
use crate::{
body::{boxed, BoxBody},
body::boxed,
response::{IntoResponse, Response},
BoxError, Error,
};
use http_body::Full;
@ -87,8 +87,8 @@ impl FailedToDeserializeQueryString {
}
impl IntoResponse for FailedToDeserializeQueryString {
fn into_response(self) -> http::Response<BoxBody> {
let mut res = http::Response::new(boxed(Full::from(self.to_string())));
fn into_response(self) -> Response {
let mut res = Response::new(boxed(Full::from(self.to_string())));
*res.status_mut() = http::StatusCode::BAD_REQUEST;
res
}
@ -204,7 +204,7 @@ impl<T> IntoResponse for ContentLengthLimitRejection<T>
where
T: IntoResponse,
{
fn into_response(self) -> http::Response<BoxBody> {
fn into_response(self) -> Response {
match self {
Self::PayloadTooLarge(inner) => inner.into_response(),
Self::LengthRequired(inner) => inner.into_response(),

View file

@ -1,5 +1,5 @@
use super::{FromRequest, RequestParts};
use crate::{body::BoxBody, response::IntoResponse};
use crate::response::{IntoResponse, Response};
use async_trait::async_trait;
use headers::HeaderMapExt;
use std::ops::Deref;
@ -106,7 +106,7 @@ pub enum TypedHeaderRejectionReason {
}
impl IntoResponse for TypedHeaderRejection {
fn into_response(self) -> http::Response<BoxBody> {
fn into_response(self) -> Response {
let mut res = self.to_string().into_response();
*res.status_mut() = http::StatusCode::BAD_REQUEST;
res

View file

@ -66,8 +66,8 @@
use self::rejection::*;
use super::{rejection::*, FromRequest, RequestParts};
use crate::{
body::{self, BoxBody},
response::IntoResponse,
body,
response::{IntoResponse, Response},
Error,
};
use async_trait::async_trait;
@ -78,7 +78,7 @@ use futures_util::{
};
use http::{
header::{self, HeaderName, HeaderValue},
Method, Response, StatusCode,
Method, StatusCode,
};
use hyper::upgrade::{OnUpgrade, Upgraded};
use sha1::{Digest, Sha1};
@ -296,7 +296,7 @@ where
F: FnOnce(WebSocket) -> Fut + Send + 'static,
Fut: Future + Send + 'static,
{
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
// check requested protocols
let protocol = self
.extractor

View file

@ -1,15 +1,14 @@
//! Handler future types.
use crate::body::BoxBody;
use crate::response::Response;
use futures_util::future::{BoxFuture, Map};
use http::Response;
use std::convert::Infallible;
opaque_future! {
/// The response future for [`IntoService`](super::IntoService).
pub type IntoServiceFuture =
Map<
BoxFuture<'static, Response<BoxBody>>,
fn(Response<BoxBody>) -> Result<Response<BoxBody>, Infallible>,
BoxFuture<'static, Response>,
fn(Response) -> Result<Response, Infallible>,
>;
}

View file

@ -1,6 +1,6 @@
use super::Handler;
use crate::body::BoxBody;
use http::{Request, Response};
use crate::response::Response;
use http::Request;
use std::{
convert::Infallible,
fmt,
@ -58,7 +58,7 @@ where
H: Handler<T, B> + Clone + Send + 'static,
B: Send + 'static,
{
type Response = Response<BoxBody>;
type Response = Response;
type Error = Infallible;
type Future = super::future::IntoServiceFuture;

View file

@ -71,18 +71,18 @@
//! [axum-debug]: https://docs.rs/axum-debug
use crate::{
body::{boxed, Body, BoxBody},
body::{boxed, Body},
extract::{
connect_info::{Connected, IntoMakeServiceWithConnectInfo},
FromRequest, RequestParts,
},
response::IntoResponse,
response::{IntoResponse, Response},
routing::IntoMakeService,
BoxError,
};
use async_trait::async_trait;
use bytes::Bytes;
use http::{Request, Response};
use http::Request;
use std::{fmt, future::Future, marker::PhantomData};
use tower::ServiceExt;
use tower_layer::Layer;
@ -115,7 +115,7 @@ pub trait Handler<T, B = Body>: Clone + Send + Sized + 'static {
type Sealed: sealed::HiddenTrait;
/// Call the handler with the given request.
async fn call(self, req: Request<B>) -> Response<BoxBody>;
async fn call(self, req: Request<B>) -> Response;
/// Apply a [`tower::Layer`] to the handler.
///
@ -271,7 +271,7 @@ where
{
type Sealed = sealed::Hidden;
async fn call(self, _req: Request<B>) -> Response<BoxBody> {
async fn call(self, _req: Request<B>) -> Response {
self().await.into_response()
}
}
@ -290,7 +290,7 @@ macro_rules! impl_handler {
{
type Sealed = sealed::Hidden;
async fn call(self, req: Request<B>) -> Response<BoxBody> {
async fn call(self, req: Request<B>) -> Response {
let mut req = RequestParts::new(req);
$(
@ -349,7 +349,7 @@ where
{
type Sealed = sealed::Hidden;
async fn call(self, req: Request<ReqBody>) -> Response<BoxBody> {
async fn call(self, req: Request<ReqBody>) -> Response {
match self.svc.oneshot(req).await {
Ok(res) => res.map(boxed),
Err(res) => res.into_response(),

View file

@ -1,7 +1,7 @@
use crate::{
body::{self, BoxBody},
body,
extract::{rejection::*, FromRequest, RequestParts},
response::IntoResponse,
response::{IntoResponse, Response},
BoxError,
};
use async_trait::async_trait;
@ -10,7 +10,6 @@ use http::{
StatusCode,
};
use http_body::Full;
use hyper::Response;
use serde::{de::DeserializeOwned, Serialize};
use std::ops::{Deref, DerefMut};
@ -163,7 +162,7 @@ impl<T> IntoResponse for Json<T>
where
T: Serialize,
{
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
let bytes = match serde_json::to_vec(&self.0) {
Ok(res) => res,
Err(err) => {

View file

@ -59,7 +59,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
@ -101,7 +101,7 @@ macro_rules! define_rejection {
}
impl 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(format!(concat!($body, ": {}"), self.0))));
*res.status_mut() = http::StatusCode::$status;
@ -142,7 +142,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(),

View file

@ -1,8 +1,8 @@
#![doc = include_str!("../docs/response.md")]
use axum_core::body::{boxed, BoxBody};
use axum_core::body::boxed;
use bytes::Bytes;
use http::{header, HeaderValue, Response};
use http::{header, HeaderValue};
use http_body::Full;
mod redirect;
@ -14,7 +14,7 @@ pub mod sse;
pub use crate::Json;
#[doc(inline)]
pub use axum_core::response::{Headers, IntoResponse};
pub use axum_core::response::{Headers, IntoResponse, Response};
#[doc(inline)]
pub use self::{redirect::Redirect, sse::Sse};
@ -29,7 +29,7 @@ impl<T> IntoResponse for Html<T>
where
T: Into<Full<Bytes>>,
{
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
let mut res = Response::new(boxed(self.0.into()));
res.headers_mut().insert(
header::CONTENT_TYPE,
@ -59,7 +59,7 @@ mod tests {
struct MyResponse;
impl IntoResponse for MyResponse {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
let mut resp = Response::new(boxed(Empty::new()));
resp.headers_mut()
.insert(HeaderName::from_static("a"), HeaderValue::from_static("1"));

View file

@ -1,6 +1,6 @@
use super::IntoResponse;
use crate::body::{boxed, BoxBody};
use http::{header::LOCATION, HeaderValue, Response, StatusCode, Uri};
use super::{IntoResponse, Response};
use crate::body::boxed;
use http::{header::LOCATION, HeaderValue, StatusCode, Uri};
use http_body::Empty;
use std::convert::TryFrom;
@ -105,7 +105,7 @@ impl Redirect {
}
impl IntoResponse for Redirect {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
let mut res = Response::new(boxed(Empty::new()));
*res.status_mut() = self.status_code;
res.headers_mut().insert(LOCATION, self.location);

View file

@ -28,8 +28,8 @@
//! ```
use crate::{
body::{self, BoxBody},
response::IntoResponse,
body,
response::{IntoResponse, Response},
BoxError,
};
use bytes::Bytes;
@ -37,7 +37,6 @@ use futures_util::{
ready,
stream::{Stream, TryStream},
};
use http::Response;
use http_body::Body as HttpBody;
use pin_project_lite::pin_project;
use std::{
@ -98,7 +97,7 @@ where
S: Stream<Item = Result<Event, E>> + Send + 'static,
E: Into<BoxError>,
{
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
let body = body::boxed(Body {
event_stream: SyncWrapper::new(self.stream),
keep_alive: self.keep_alive.map(KeepAliveStream::new),

View file

@ -1,8 +1,7 @@
//! Future types.
use crate::body::BoxBody;
use crate::response::Response;
use futures_util::future::Either;
use http::Response;
use std::{convert::Infallible, future::ready};
pub use super::{into_make_service::IntoMakeServiceFuture, route::RouteFuture};
@ -12,7 +11,7 @@ opaque_future! {
pub type RouterFuture<B> =
futures_util::future::Either<
RouteFuture<B, Infallible>,
std::future::Ready<Result<Response<BoxBody>, Infallible>>,
std::future::Ready<Result<Response, Infallible>>,
>;
}
@ -21,7 +20,7 @@ impl<B> RouterFuture<B> {
Self::new(Either::Left(future))
}
pub(super) fn from_response(response: Response<BoxBody>) -> Self {
pub(super) fn from_response(response: Response) -> Self {
Self::new(Either::Right(ready(Ok(response))))
}
}

View file

@ -1,8 +1,9 @@
use crate::{
body::{boxed, Body, BoxBody, Bytes},
body::{boxed, Body, Bytes},
error_handling::{HandleError, HandleErrorLayer},
handler::Handler,
http::{Method, Request, Response, StatusCode},
http::{Method, Request, StatusCode},
response::Response,
routing::{Fallback, MethodFilter, Route},
BoxError,
};
@ -638,10 +639,7 @@ impl<ReqBody, E> MethodRouter<ReqBody, E> {
fn fallback_boxed_response_body<S>(mut self, svc: S) -> Self
where
S: Service<Request<ReqBody>, Response = Response<BoxBody>, Error = E>
+ Clone
+ Send
+ 'static,
S: Service<Request<ReqBody>, Response = Response, Error = E> + Clone + Send + 'static,
S::Future: Send + 'static,
{
self.fallback = Fallback::Custom(Route::new(svc));
@ -799,7 +797,7 @@ impl<ReqBody, E> MethodRouter<ReqBody, E> {
where
F: Clone + Send + 'static,
HandleError<Route<ReqBody, E>, F, T>:
Service<Request<ReqBody>, Response = Response<BoxBody>, Error = Infallible>,
Service<Request<ReqBody>, Response = Response, Error = Infallible>,
<HandleError<Route<ReqBody, E>, F, T> as Service<Request<ReqBody>>>::Future: Send,
T: 'static,
E: 'static,
@ -810,10 +808,7 @@ impl<ReqBody, E> MethodRouter<ReqBody, E> {
fn on_service_boxed_response_body<S>(self, filter: MethodFilter, svc: S) -> Self
where
S: Service<Request<ReqBody>, Response = Response<BoxBody>, Error = E>
+ Clone
+ Send
+ 'static,
S: Service<Request<ReqBody>, Response = Response, Error = E> + Clone + Send + 'static,
S::Future: Send + 'static,
{
// written with a pattern match like this to ensure we update all fields
@ -898,7 +893,7 @@ where
use crate::routing::future::RouteFuture;
impl<B, E> Service<Request<B>> for MethodRouter<B, E> {
type Response = Response<BoxBody>;
type Response = Response;
type Error = E;
type Future = RouteFuture<B, E>;
@ -1070,7 +1065,7 @@ mod tests {
async fn call<S>(method: Method, svc: &mut S) -> (StatusCode, String)
where
S: Service<Request<Body>, Response = Response<BoxBody>, Error = Infallible>,
S: Service<Request<Body>, Response = Response, Error = Infallible>,
{
let request = Request::builder()
.uri("/")

View file

@ -2,17 +2,18 @@
use self::{future::RouterFuture, not_found::NotFound};
use crate::{
body::{boxed, Body, BoxBody},
body::{boxed, Body},
extract::{
connect_info::{Connected, IntoMakeServiceWithConnectInfo},
MatchedPath, OriginalUri,
},
response::Response,
routing::strip_prefix::StripPrefix,
util::{try_downcast, ByteStr, PercentDecodedByteStr},
BoxError,
};
use bytes::Bytes;
use http::{Request, Response, StatusCode, Uri};
use http::{Request, StatusCode, Uri};
use std::{
borrow::Cow,
collections::HashMap,
@ -109,10 +110,7 @@ where
#[doc = include_str!("../docs/routing/route.md")]
pub fn route<T>(mut self, path: &str, service: T) -> Self
where
T: Service<Request<B>, Response = Response<BoxBody>, Error = Infallible>
+ Clone
+ Send
+ 'static,
T: Service<Request<B>, Response = Response, Error = Infallible> + Clone + Send + 'static,
T::Future: Send + 'static,
{
if path.is_empty() {
@ -161,10 +159,7 @@ where
#[doc = include_str!("../docs/routing/nest.md")]
pub fn nest<T>(mut self, path: &str, svc: T) -> Self
where
T: Service<Request<B>, Response = Response<BoxBody>, Error = Infallible>
+ Clone
+ Send
+ 'static,
T: Service<Request<B>, Response = Response, Error = Infallible> + Clone + Send + 'static,
T::Future: Send + 'static,
{
if path.is_empty() {
@ -352,10 +347,7 @@ where
#[doc = include_str!("../docs/routing/fallback.md")]
pub fn fallback<T>(mut self, svc: T) -> Self
where
T: Service<Request<B>, Response = Response<BoxBody>, Error = Infallible>
+ Clone
+ Send
+ 'static,
T: Service<Request<B>, Response = Response, Error = Infallible> + Clone + Send + 'static,
T::Future: Send + 'static,
{
self.fallback = Fallback::Custom(Route::new(svc));
@ -461,7 +453,7 @@ impl<B> Service<Request<B>> for Router<B>
where
B: Send + 'static,
{
type Response = Response<BoxBody>;
type Response = Response;
type Error = Infallible;
type Future = RouterFuture<B>;

View file

@ -1,5 +1,5 @@
use crate::body::BoxBody;
use http::{Request, Response, StatusCode};
use crate::response::Response;
use http::{Request, StatusCode};
use std::{
convert::Infallible,
future::ready,
@ -18,9 +18,9 @@ impl<B> Service<Request<B>> for NotFound
where
B: Send + 'static,
{
type Response = Response<BoxBody>;
type Response = Response;
type Error = Infallible;
type Future = std::future::Ready<Result<Response<BoxBody>, Self::Error>>;
type Future = std::future::Ready<Result<Response, Self::Error>>;
#[inline]
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {

View file

@ -1,5 +1,8 @@
use crate::body::{boxed, Body, BoxBody};
use http::{Request, Response};
use crate::{
body::{boxed, Body},
response::Response,
};
use http::Request;
use http_body::Empty;
use pin_project_lite::pin_project;
use std::{
@ -19,14 +22,12 @@ use tower_service::Service;
///
/// You normally shouldn't need to care about this type. It's used in
/// [`Router::layer`](super::Router::layer).
pub struct Route<B = Body, E = Infallible>(
pub(crate) BoxCloneService<Request<B>, Response<BoxBody>, E>,
);
pub struct Route<B = Body, E = Infallible>(pub(crate) BoxCloneService<Request<B>, Response, E>);
impl<B, E> Route<B, E> {
pub(super) fn new<T>(svc: T) -> Self
where
T: Service<Request<B>, Response = Response<BoxBody>, Error = E> + Clone + Send + 'static,
T: Service<Request<B>, Response = Response, Error = E> + Clone + Send + 'static,
T::Future: Send + 'static,
{
Self(BoxCloneService::new(svc))
@ -46,7 +47,7 @@ impl<ReqBody, E> fmt::Debug for Route<ReqBody, E> {
}
impl<B, E> Service<Request<B>> for Route<B, E> {
type Response = Response<BoxBody>;
type Response = Response;
type Error = E;
type Future = RouteFuture<B, E>;
@ -66,7 +67,7 @@ pin_project! {
pub struct RouteFuture<B, E> {
#[pin]
future: Oneshot<
BoxCloneService<Request<B>, Response<BoxBody>, E>,
BoxCloneService<Request<B>, Response, E>,
Request<B>,
>,
strip_body: bool,
@ -75,7 +76,7 @@ pin_project! {
impl<B, E> RouteFuture<B, E> {
pub(crate) fn new(
future: Oneshot<BoxCloneService<Request<B>, Response<BoxBody>, E>, Request<B>>,
future: Oneshot<BoxCloneService<Request<B>, Response, E>, Request<B>>,
) -> Self {
RouteFuture {
future,
@ -90,7 +91,7 @@ impl<B, E> RouteFuture<B, E> {
}
impl<B, E> Future for RouteFuture<B, E> {
type Output = Result<Response<BoxBody>, E>;
type Output = Result<Response, E>;
#[inline]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {

View file

@ -9,10 +9,9 @@
use axum::{
async_trait,
body::BoxBody,
extract::{Extension, Path},
http::{Response, StatusCode},
response::IntoResponse,
http::StatusCode,
response::{IntoResponse, Response},
routing::{get, post},
AddExtensionLayer, Json, Router,
};
@ -92,7 +91,7 @@ impl From<UserRepoError> for AppError {
}
impl IntoResponse for AppError {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
let (status, error_message) = match self {
AppError::UserRepo(UserRepoError::NotFound) => {
(StatusCode::NOT_FOUND, "User not found")

View file

@ -13,9 +13,9 @@
//! Example is based on <https://github.com/hyperium/hyper/blob/master/examples/http_proxy.rs>
use axum::{
body::{self, Body, BoxBody},
http::{Method, Request, Response, StatusCode},
response::IntoResponse,
body::{self, Body},
http::{Method, Request, StatusCode},
response::{IntoResponse, Response},
routing::get,
Router,
};
@ -55,7 +55,7 @@ async fn main() {
.unwrap();
}
async fn proxy(req: Request<Body>) -> Result<Response<BoxBody>, hyper::Error> {
async fn proxy(req: Request<Body>) -> Result<Response, hyper::Error> {
tracing::trace!(?req);
if let Some(host_addr) = req.uri().authority().map(|auth| auth.to_string()) {

View file

@ -8,10 +8,9 @@
use axum::{
async_trait,
body::BoxBody,
extract::{FromRequest, RequestParts, TypedHeader},
http::{Response, StatusCode},
response::IntoResponse,
http::StatusCode,
response::{IntoResponse, Response},
routing::{get, post},
Json, Router,
};
@ -141,7 +140,7 @@ where
}
impl IntoResponse for AuthError {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
let (status, error_message) = match self {
AuthError::WrongCredentials => (StatusCode::UNAUTHORIZED, "Wrong credentials"),
AuthError::MissingCredentials => (StatusCode::BAD_REQUEST, "Missing credentials"),

View file

@ -9,10 +9,9 @@
use async_session::{MemoryStore, Session, SessionStore};
use axum::{
async_trait,
body::BoxBody,
extract::{Extension, FromRequest, Query, RequestParts, TypedHeader},
http::{header::SET_COOKIE, HeaderMap, Response},
response::{IntoResponse, Redirect},
http::{header::SET_COOKIE, HeaderMap},
response::{IntoResponse, Redirect, Response},
routing::get,
AddExtensionLayer, Router,
};
@ -199,7 +198,7 @@ async fn login_authorized(
struct AuthRedirect;
impl IntoResponse for AuthRedirect {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
Redirect::found("/auth/discord".parse().unwrap()).into_response()
}
}

View file

@ -5,9 +5,10 @@
//! ```
use axum::{
body::{Body, BoxBody, Bytes},
body::{Body, Bytes},
error_handling::HandleErrorLayer,
http::{Request, Response, StatusCode},
http::{Request, StatusCode},
response::Response,
routing::post,
Router,
};
@ -54,7 +55,7 @@ async fn map_request(req: Request<Body>) -> Result<Request<Body>, BoxError> {
Ok(req)
}
async fn map_response(res: Response<BoxBody>) -> Result<Response<Body>, BoxError> {
async fn map_response(res: Response) -> Result<Response<Body>, BoxError> {
let (parts, body) = res.into_parts();
let bytes = buffer_and_print("response", body).await?;
let res = Response::from_parts(parts, Body::from(bytes));

View file

@ -6,10 +6,10 @@
use askama::Template;
use axum::{
body::{self, BoxBody, Full},
body::{self, Full},
extract,
http::{Response, StatusCode},
response::{Html, IntoResponse},
http::StatusCode,
response::{Html, IntoResponse, Response},
routing::get,
Router,
};
@ -52,7 +52,7 @@ impl<T> IntoResponse for HtmlTemplate<T>
where
T: Template,
{
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
match self.0.render() {
Ok(html) => Html(html).into_response(),
Err(err) => Response::builder()

View file

@ -6,8 +6,8 @@
use axum::{
body::Bytes,
http::{HeaderMap, Request, Response},
response::Html,
http::{HeaderMap, Request},
response::{Html, Response},
routing::get,
Router,
};
@ -42,11 +42,9 @@ async fn main() {
.on_request(|_request: &Request<_>, _span: &Span| {
// ...
})
.on_response(
|_response: &Response<_>, _latency: Duration, _span: &Span| {
// ...
},
)
.on_response(|_response: &Response, _latency: Duration, _span: &Span| {
// ...
})
.on_body_chunk(|_chunk: &Bytes, _latency: Duration, _span: &Span| {
// ..
})

View file

@ -12,10 +12,9 @@
use async_trait::async_trait;
use axum::{
body::BoxBody,
extract::{Form, FromRequest, RequestParts},
http::{Response, StatusCode},
response::{Html, IntoResponse},
http::StatusCode,
response::{Html, IntoResponse, Response},
routing::get,
BoxError, Router,
};
@ -84,7 +83,7 @@ pub enum ServerError {
}
impl IntoResponse for ServerError {
fn into_response(self) -> Response<BoxBody> {
fn into_response(self) -> Response {
match self {
ServerError::ValidationError(_) => {
let message = format!("Input validation error: [{}]", self).replace("\n", ", ");

View file

@ -6,10 +6,9 @@
use axum::{
async_trait,
body::BoxBody,
extract::{FromRequest, Path, RequestParts},
http::{Response, StatusCode},
response::IntoResponse,
http::StatusCode,
response::{IntoResponse, Response},
routing::get,
Router,
};
@ -51,7 +50,7 @@ impl<B> FromRequest<B> for Version
where
B: Send,
{
type Rejection = Response<BoxBody>;
type Rejection = Response;
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
let params = Path::<HashMap<String, String>>::from_request(req)