From dfb06e721c509f5fdb785b125156bd7bb8983d68 Mon Sep 17 00:00:00 2001 From: Kai Jewson Date: Sun, 5 Dec 2021 18:16:46 +0000 Subject: [PATCH] Introduce `Response` type alias as a shorthand for `Response` (#590) * Introduce `Response` type alias as a shorthand * Don't re-export `Response` at the crate root --- axum-core/src/extract/tuple.rs | 5 +- axum-core/src/macros.rs | 6 +- axum-core/src/response/headers.rs | 14 ++-- axum-core/src/response/mod.rs | 68 ++++++++++--------- axum-debug/tests/pass/returns_self.rs | 5 +- axum-extra/src/extract/cached.rs | 12 ++-- axum-extra/src/response/erased_json.rs | 8 +-- axum-extra/src/routing/resource.rs | 20 ++---- axum/CHANGELOG.md | 2 +- axum/src/body/stream_body.rs | 8 +-- axum/src/docs/error_handling.md | 4 +- axum/src/docs/middleware.md | 22 +++--- axum/src/error_handling/mod.rs | 17 +++-- axum/src/extract/extractor_middleware.rs | 11 +-- axum/src/extract/mod.rs | 1 - axum/src/extract/path/mod.rs | 8 +-- axum/src/extract/rejection.rs | 10 +-- axum/src/extract/typed_header.rs | 4 +- axum/src/extract/ws.rs | 8 +-- axum/src/handler/future.rs | 7 +- axum/src/handler/into_service.rs | 6 +- axum/src/handler/mod.rs | 14 ++-- axum/src/json.rs | 7 +- axum/src/macros.rs | 6 +- axum/src/response/mod.rs | 10 +-- axum/src/response/redirect.rs | 8 +-- axum/src/response/sse.rs | 7 +- axum/src/routing/future.rs | 7 +- axum/src/routing/method_routing.rs | 21 +++--- axum/src/routing/mod.rs | 22 ++---- axum/src/routing/not_found.rs | 8 +-- axum/src/routing/route.rs | 21 +++--- .../src/main.rs | 7 +- examples/http-proxy/src/main.rs | 8 +-- examples/jwt/src/main.rs | 7 +- examples/oauth/src/main.rs | 7 +- examples/print-request-response/src/main.rs | 7 +- examples/templates/src/main.rs | 8 +-- examples/tracing-aka-logging/src/main.rs | 12 ++-- examples/validator/src/main.rs | 7 +- examples/versioning/src/main.rs | 7 +- 41 files changed, 210 insertions(+), 237 deletions(-) diff --git a/axum-core/src/extract/tuple.rs b/axum-core/src/extract/tuple.rs index 1c28412d..8c781a8d 100644 --- a/axum-core/src/extract/tuple.rs +++ b/axum-core/src/extract/tuple.rs @@ -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 + Send, )* B: Send, { - type Rejection = Response; + type Rejection = Response; async fn from_request(req: &mut RequestParts) -> Result { $( let $ty = $ty::from_request(req).await.map_err(|err| err.into_response())?; )* diff --git a/axum-core/src/macros.rs b/axum-core/src/macros.rs index 5e5a76cd..77c42888 100644 --- a/axum-core/src/macros.rs +++ b/axum-core/src/macros.rs @@ -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(), diff --git a/axum-core/src/response/headers.rs b/axum-core/src/response/headers.rs index a82f6a4a..c314948f 100644 --- a/axum-core/src/response/headers.rs +++ b/axum-core/src/response/headers.rs @@ -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(pub H); impl Headers { - fn try_into_header_map(self) -> Result> + fn try_into_header_map(self) -> Result where H: IntoIterator, K: TryInto, @@ -93,7 +93,7 @@ where V: TryInto, V::Error: fmt::Display, { - fn into_response(self) -> http::Response { + fn into_response(self) -> Response { let headers = self.try_into_header_map(); match headers { @@ -116,7 +116,7 @@ where V: TryInto, V::Error: fmt::Display, { - fn into_response(self) -> Response { + 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, V::Error: fmt::Display, { - fn into_response(self) -> Response { + fn into_response(self) -> Response { let headers = match self.1.try_into_header_map() { Ok(headers) => headers, Err(res) => return res, diff --git a/axum-core/src/response/mod.rs b/axum-core/src/response/mod.rs index f5d58c32..9f782e31 100644 --- a/axum-core/src/response/mod.rs +++ b/axum-core/src/response/mod.rs @@ -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 = http::Response; + /// 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 { +/// 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 { +/// 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; + fn into_response(self) -> Response; } impl IntoResponse for () { - fn into_response(self) -> Response { + fn into_response(self) -> Response { Response::new(boxed(Empty::new())) } } impl IntoResponse for Infallible { - fn into_response(self) -> Response { + fn into_response(self) -> Response { match self {} } } @@ -161,7 +165,7 @@ where T: IntoResponse, E: IntoResponse, { - fn into_response(self) -> Response { + 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 + Send + 'static, B::Error: Into, { - fn into_response(self) -> Response { + 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 { + fn into_response(self) -> Response { Response::new(boxed(self)) } } @@ -193,7 +197,7 @@ impl_into_response_for_body!(Full); impl_into_response_for_body!(Empty); impl IntoResponse for http::response::Parts { - fn into_response(self) -> Response { + fn into_response(self) -> Response { Response::from_parts(self, boxed(Empty::new())) } } @@ -202,7 +206,7 @@ impl IntoResponse for http_body::combinators::BoxBody where E: Into + 'static, { - fn into_response(self) -> Response { + fn into_response(self) -> Response { Response::new(boxed(self)) } } @@ -211,7 +215,7 @@ impl IntoResponse for http_body::combinators::UnsyncBoxBody where E: Into + 'static, { - fn into_response(self) -> Response { + fn into_response(self) -> Response { Response::new(boxed(self)) } } @@ -222,7 +226,7 @@ where F: FnMut(B::Data) -> Bytes + Send + 'static, B::Error: Into, { - fn into_response(self) -> Response { + fn into_response(self) -> Response { Response::new(boxed(self)) } } @@ -233,27 +237,27 @@ where F: FnMut(B::Error) -> E + Send + 'static, E: Into, { - fn into_response(self) -> Response { + fn into_response(self) -> Response { Response::new(boxed(self)) } } impl IntoResponse for &'static str { #[inline] - fn into_response(self) -> Response { + fn into_response(self) -> Response { Cow::Borrowed(self).into_response() } } impl IntoResponse for String { #[inline] - fn into_response(self) -> Response { + fn into_response(self) -> Response { Cow::<'static, str>::Owned(self).into_response() } } impl IntoResponse for Cow<'static, str> { - fn into_response(self) -> Response { + 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 { + 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 { + 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 { - fn into_response(self) -> Response { + 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 { } impl IntoResponse for Cow<'static, [u8]> { - fn into_response(self) -> Response { + 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 { + fn into_response(self) -> Response { Response::builder() .status(self) .body(boxed(Empty::new())) @@ -320,7 +324,7 @@ impl IntoResponse for (StatusCode, T) where T: IntoResponse, { - fn into_response(self) -> Response { + fn into_response(self) -> Response { let mut res = self.1.into_response(); *res.status_mut() = self.0; res @@ -331,7 +335,7 @@ impl IntoResponse for (HeaderMap, T) where T: IntoResponse, { - fn into_response(self) -> Response { + fn into_response(self) -> Response { let mut res = self.1.into_response(); res.headers_mut().extend(self.0); res @@ -342,7 +346,7 @@ impl IntoResponse for (StatusCode, HeaderMap, T) where T: IntoResponse, { - fn into_response(self) -> Response { + 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 { + fn into_response(self) -> Response { let mut res = Response::new(boxed(Empty::new())); *res.headers_mut() = self; res diff --git a/axum-debug/tests/pass/returns_self.rs b/axum-debug/tests/pass/returns_self.rs index eb520fbd..cff39c85 100644 --- a/axum-debug/tests/pass/returns_self.rs +++ b/axum-debug/tests/pass/returns_self.rs @@ -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 { + fn into_response(self) -> Response { todo!() } } diff --git a/axum-extra/src/extract/cached.rs b/axum-extra/src/extract/cached.rs index a45ed397..333b64ff 100644 --- a/axum-extra/src/extract/cached.rs +++ b/axum-extra/src/extract/cached.rs @@ -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; +/// type Rejection = Response; /// /// async fn from_request(req: &mut RequestParts) -> Result { /// // loading a `CurrentUser` requires first loading the `Session` @@ -157,7 +155,7 @@ impl IntoResponse for CachedRejection where R: IntoResponse, { - fn into_response(self) -> Response { + fn into_response(self) -> Response { match self { Self::ExtensionsAlreadyExtracted(inner) => inner.into_response(), Self::Inner(inner) => inner.into_response(), diff --git a/axum-extra/src/response/erased_json.rs b/axum-extra/src/response/erased_json.rs index d8f860ac..c66ee461 100644 --- a/axum-extra/src/response/erased_json.rs +++ b/axum-extra/src/response/erased_json.rs @@ -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 { + fn into_response(self) -> Response { let bytes = match self.0 { Ok(res) => res, Err(err) => { diff --git a/axum-extra/src/routing/resource.rs b/axum-extra/src/routing/resource.rs index adff26e4..6cfa41ad 100644 --- a/axum-extra/src/routing/resource.rs +++ b/axum-extra/src/routing/resource.rs @@ -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 Resource { /// The routes will be nested at `/{resource_name}/:{resource_name}_id`. pub fn nest(mut self, svc: T) -> Self where - T: Service, Response = Response, Error = Infallible> - + Clone - + Send - + 'static, + T: Service, Response = Response, Error = Infallible> + Clone + Send + 'static, T::Future: Send + 'static, { let path = self.show_update_destroy_path(); @@ -155,10 +153,7 @@ impl Resource { /// The routes will be nested at `/{resource_name}`. pub fn nest_collection(mut self, svc: T) -> Self where - T: Service, Response = Response, Error = Infallible> - + Clone - + Send - + 'static, + T: Service, Response = Response, Error = Infallible> + Clone + Send + 'static, T::Future: Send + 'static, { let path = self.index_create_path(); @@ -176,10 +171,7 @@ impl Resource { fn route(mut self, path: &str, svc: T) -> Self where - T: Service, Response = Response, Error = Infallible> - + Clone - + Send - + 'static, + T: Service, Response = Response, Error = Infallible> + Clone + Send + 'static, T::Future: Send + 'static, { self.router = self.router.route(path, svc); diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index d9470100..9d45e1a4 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -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`. # 0.4.0 (02. December, 2021) diff --git a/axum/src/body/stream_body.rs b/axum/src/body/stream_body.rs index d2d2f510..bb3da56d 100644 --- a/axum/src/body/stream_body.rs +++ b/axum/src/body/stream_body.rs @@ -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, S::Error: Into, { - fn into_response(self) -> Response { + fn into_response(self) -> Response { Response::new(body::boxed(self)) } } diff --git a/axum/src/docs/error_handling.md b/axum/src/docs/error_handling.md index 608e6a2a..3b342121 100644 --- a/axum/src/docs/error_handling.md +++ b/axum/src/docs/error_handling.md @@ -24,7 +24,7 @@ async fn handler() -> Result { 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 diff --git a/axum/src/docs/middleware.md b/axum/src/docs/middleware.md index 058285aa..5592dc0f 100644 --- a/axum/src/docs/middleware.md +++ b/axum/src/docs/middleware.md @@ -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) -> Result, Infallible> { Ok(req) } -async fn map_response(res: Response) -> Result, Infallible> { +async fn map_response(res: Response) -> Result { 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 { impl Service> for MyMiddleware where - S: Service, Response = Response> + Clone + Send + 'static, + S: Service, 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 = inner.call(req).await?; + let res: Response = inner.call(req).await?; println!("`MyMiddleware` received the response"); diff --git a/axum/src/error_handling/mod.rs b/axum/src/error_handling/mod.rs index 716a06ff..0594018b 100644 --- a/axum/src/error_handling/mod.rs +++ b/axum/src/error_handling/mod.rs @@ -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 + Send + 'static, ResBody::Error: Into, { - type Response = Response; + type Response = Response; type Error = Infallible; type Future = future::HandleErrorFuture; @@ -168,7 +168,7 @@ macro_rules! impl_service { ResBody: HttpBody + Send + 'static, ResBody::Error: Into, { - type Response = Response; + 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, Infallible>> + pub(super) future: Pin> + Send + 'static >>, @@ -243,7 +242,7 @@ pub mod future { } impl Future for HandleErrorFuture { - type Output = Result, Infallible>; + type Output = Result; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { self.project().future.poll(cx) diff --git a/axum/src/extract/extractor_middleware.rs b/axum/src/extract/extractor_middleware.rs index 5e6a6671..2fd1f1fe 100644 --- a/axum/src/extract/extractor_middleware.rs +++ b/axum/src/extract/extractor_middleware.rs @@ -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 + Send + 'static, ResBody::Error: Into, { - type Response = Response; + type Response = Response; type Error = S::Error; type Future = ResponseFuture; @@ -229,7 +232,7 @@ where ResBody: http_body::Body + Send + 'static, ResBody::Error: Into, { - type Output = Result, S::Error>; + type Output = Result; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { loop { diff --git a/axum/src/extract/mod.rs b/axum/src/extract/mod.rs index ee6488c1..4fdd0ba0 100644 --- a/axum/src/extract/mod.rs +++ b/axum/src/extract/mod.rs @@ -1,6 +1,5 @@ #![doc = include_str!("../docs/extract.md")] -use crate::response::IntoResponse; use http::header; use rejection::*; diff --git a/axum/src/extract/path/mod.rs b/axum/src/extract/path/mod.rs index c1ee0bb3..0760ac42 100644 --- a/axum/src/extract/path/mod.rs +++ b/axum/src/extract/path/mod.rs @@ -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 { + 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 } diff --git a/axum/src/extract/rejection.rs b/axum/src/extract/rejection.rs index cb99785e..b449e777 100644 --- a/axum/src/extract/rejection.rs +++ b/axum/src/extract/rejection.rs @@ -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 { - 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 IntoResponse for ContentLengthLimitRejection where T: IntoResponse, { - fn into_response(self) -> http::Response { + fn into_response(self) -> Response { match self { Self::PayloadTooLarge(inner) => inner.into_response(), Self::LengthRequired(inner) => inner.into_response(), diff --git a/axum/src/extract/typed_header.rs b/axum/src/extract/typed_header.rs index 76b757bc..4e5e525d 100644 --- a/axum/src/extract/typed_header.rs +++ b/axum/src/extract/typed_header.rs @@ -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 { + fn into_response(self) -> Response { let mut res = self.to_string().into_response(); *res.status_mut() = http::StatusCode::BAD_REQUEST; res diff --git a/axum/src/extract/ws.rs b/axum/src/extract/ws.rs index 0b5e1a69..74f2e86e 100644 --- a/axum/src/extract/ws.rs +++ b/axum/src/extract/ws.rs @@ -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 { + fn into_response(self) -> Response { // check requested protocols let protocol = self .extractor diff --git a/axum/src/handler/future.rs b/axum/src/handler/future.rs index 144110a4..6bd39b35 100644 --- a/axum/src/handler/future.rs +++ b/axum/src/handler/future.rs @@ -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>, - fn(Response) -> Result, Infallible>, + BoxFuture<'static, Response>, + fn(Response) -> Result, >; } diff --git a/axum/src/handler/into_service.rs b/axum/src/handler/into_service.rs index 613fd159..1cacfa20 100644 --- a/axum/src/handler/into_service.rs +++ b/axum/src/handler/into_service.rs @@ -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 + Clone + Send + 'static, B: Send + 'static, { - type Response = Response; + type Response = Response; type Error = Infallible; type Future = super::future::IntoServiceFuture; diff --git a/axum/src/handler/mod.rs b/axum/src/handler/mod.rs index 4ecd4c44..ff44c061 100644 --- a/axum/src/handler/mod.rs +++ b/axum/src/handler/mod.rs @@ -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: Clone + Send + Sized + 'static { type Sealed: sealed::HiddenTrait; /// Call the handler with the given request. - async fn call(self, req: Request) -> Response; + async fn call(self, req: Request) -> Response; /// Apply a [`tower::Layer`] to the handler. /// @@ -271,7 +271,7 @@ where { type Sealed = sealed::Hidden; - async fn call(self, _req: Request) -> Response { + async fn call(self, _req: Request) -> Response { self().await.into_response() } } @@ -290,7 +290,7 @@ macro_rules! impl_handler { { type Sealed = sealed::Hidden; - async fn call(self, req: Request) -> Response { + async fn call(self, req: Request) -> Response { let mut req = RequestParts::new(req); $( @@ -349,7 +349,7 @@ where { type Sealed = sealed::Hidden; - async fn call(self, req: Request) -> Response { + async fn call(self, req: Request) -> Response { match self.svc.oneshot(req).await { Ok(res) => res.map(boxed), Err(res) => res.into_response(), diff --git a/axum/src/json.rs b/axum/src/json.rs index 5c5c88ec..371be46f 100644 --- a/axum/src/json.rs +++ b/axum/src/json.rs @@ -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 IntoResponse for Json where T: Serialize, { - fn into_response(self) -> Response { + fn into_response(self) -> Response { let bytes = match serde_json::to_vec(&self.0) { Ok(res) => res, Err(err) => { diff --git a/axum/src/macros.rs b/axum/src/macros.rs index 7b8ce5a7..26748f3a 100644 --- a/axum/src/macros.rs +++ b/axum/src/macros.rs @@ -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(), diff --git a/axum/src/response/mod.rs b/axum/src/response/mod.rs index 1ac275f4..3682b40d 100644 --- a/axum/src/response/mod.rs +++ b/axum/src/response/mod.rs @@ -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 IntoResponse for Html where T: Into>, { - fn into_response(self) -> Response { + 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 { + 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")); diff --git a/axum/src/response/redirect.rs b/axum/src/response/redirect.rs index b99d91f1..2ba7bfb7 100644 --- a/axum/src/response/redirect.rs +++ b/axum/src/response/redirect.rs @@ -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 { + 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); diff --git a/axum/src/response/sse.rs b/axum/src/response/sse.rs index be374d5d..33c61650 100644 --- a/axum/src/response/sse.rs +++ b/axum/src/response/sse.rs @@ -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> + Send + 'static, E: Into, { - fn into_response(self) -> Response { + fn into_response(self) -> Response { let body = body::boxed(Body { event_stream: SyncWrapper::new(self.stream), keep_alive: self.keep_alive.map(KeepAliveStream::new), diff --git a/axum/src/routing/future.rs b/axum/src/routing/future.rs index 221fc887..c195e12e 100644 --- a/axum/src/routing/future.rs +++ b/axum/src/routing/future.rs @@ -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 = futures_util::future::Either< RouteFuture, - std::future::Ready, Infallible>>, + std::future::Ready>, >; } @@ -21,7 +20,7 @@ impl RouterFuture { Self::new(Either::Left(future)) } - pub(super) fn from_response(response: Response) -> Self { + pub(super) fn from_response(response: Response) -> Self { Self::new(Either::Right(ready(Ok(response)))) } } diff --git a/axum/src/routing/method_routing.rs b/axum/src/routing/method_routing.rs index 67656eee..a556d30a 100644 --- a/axum/src/routing/method_routing.rs +++ b/axum/src/routing/method_routing.rs @@ -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 MethodRouter { fn fallback_boxed_response_body(mut self, svc: S) -> Self where - S: Service, Response = Response, Error = E> - + Clone - + Send - + 'static, + S: Service, Response = Response, Error = E> + Clone + Send + 'static, S::Future: Send + 'static, { self.fallback = Fallback::Custom(Route::new(svc)); @@ -799,7 +797,7 @@ impl MethodRouter { where F: Clone + Send + 'static, HandleError, F, T>: - Service, Response = Response, Error = Infallible>, + Service, Response = Response, Error = Infallible>, , F, T> as Service>>::Future: Send, T: 'static, E: 'static, @@ -810,10 +808,7 @@ impl MethodRouter { fn on_service_boxed_response_body(self, filter: MethodFilter, svc: S) -> Self where - S: Service, Response = Response, Error = E> - + Clone - + Send - + 'static, + S: Service, 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 Service> for MethodRouter { - type Response = Response; + type Response = Response; type Error = E; type Future = RouteFuture; @@ -1070,7 +1065,7 @@ mod tests { async fn call(method: Method, svc: &mut S) -> (StatusCode, String) where - S: Service, Response = Response, Error = Infallible>, + S: Service, Response = Response, Error = Infallible>, { let request = Request::builder() .uri("/") diff --git a/axum/src/routing/mod.rs b/axum/src/routing/mod.rs index f384999a..2fb937dd 100644 --- a/axum/src/routing/mod.rs +++ b/axum/src/routing/mod.rs @@ -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(mut self, path: &str, service: T) -> Self where - T: Service, Response = Response, Error = Infallible> - + Clone - + Send - + 'static, + T: Service, 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(mut self, path: &str, svc: T) -> Self where - T: Service, Response = Response, Error = Infallible> - + Clone - + Send - + 'static, + T: Service, 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(mut self, svc: T) -> Self where - T: Service, Response = Response, Error = Infallible> - + Clone - + Send - + 'static, + T: Service, Response = Response, Error = Infallible> + Clone + Send + 'static, T::Future: Send + 'static, { self.fallback = Fallback::Custom(Route::new(svc)); @@ -461,7 +453,7 @@ impl Service> for Router where B: Send + 'static, { - type Response = Response; + type Response = Response; type Error = Infallible; type Future = RouterFuture; diff --git a/axum/src/routing/not_found.rs b/axum/src/routing/not_found.rs index 8c56d241..7935f923 100644 --- a/axum/src/routing/not_found.rs +++ b/axum/src/routing/not_found.rs @@ -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 Service> for NotFound where B: Send + 'static, { - type Response = Response; + type Response = Response; type Error = Infallible; - type Future = std::future::Ready, Self::Error>>; + type Future = std::future::Ready>; #[inline] fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { diff --git a/axum/src/routing/route.rs b/axum/src/routing/route.rs index 6a1b590c..3c6fab1d 100644 --- a/axum/src/routing/route.rs +++ b/axum/src/routing/route.rs @@ -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( - pub(crate) BoxCloneService, Response, E>, -); +pub struct Route(pub(crate) BoxCloneService, Response, E>); impl Route { pub(super) fn new(svc: T) -> Self where - T: Service, Response = Response, Error = E> + Clone + Send + 'static, + T: Service, Response = Response, Error = E> + Clone + Send + 'static, T::Future: Send + 'static, { Self(BoxCloneService::new(svc)) @@ -46,7 +47,7 @@ impl fmt::Debug for Route { } impl Service> for Route { - type Response = Response; + type Response = Response; type Error = E; type Future = RouteFuture; @@ -66,7 +67,7 @@ pin_project! { pub struct RouteFuture { #[pin] future: Oneshot< - BoxCloneService, Response, E>, + BoxCloneService, Response, E>, Request, >, strip_body: bool, @@ -75,7 +76,7 @@ pin_project! { impl RouteFuture { pub(crate) fn new( - future: Oneshot, Response, E>, Request>, + future: Oneshot, Response, E>, Request>, ) -> Self { RouteFuture { future, @@ -90,7 +91,7 @@ impl RouteFuture { } impl Future for RouteFuture { - type Output = Result, E>; + type Output = Result; #[inline] fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { diff --git a/examples/error-handling-and-dependency-injection/src/main.rs b/examples/error-handling-and-dependency-injection/src/main.rs index 0da8c566..14faebce 100644 --- a/examples/error-handling-and-dependency-injection/src/main.rs +++ b/examples/error-handling-and-dependency-injection/src/main.rs @@ -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 for AppError { } impl IntoResponse for AppError { - fn into_response(self) -> Response { + fn into_response(self) -> Response { let (status, error_message) = match self { AppError::UserRepo(UserRepoError::NotFound) => { (StatusCode::NOT_FOUND, "User not found") diff --git a/examples/http-proxy/src/main.rs b/examples/http-proxy/src/main.rs index 3d3b9b26..88c2f1b9 100644 --- a/examples/http-proxy/src/main.rs +++ b/examples/http-proxy/src/main.rs @@ -13,9 +13,9 @@ //! Example is based on 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) -> Result, hyper::Error> { +async fn proxy(req: Request) -> Result { tracing::trace!(?req); if let Some(host_addr) = req.uri().authority().map(|auth| auth.to_string()) { diff --git a/examples/jwt/src/main.rs b/examples/jwt/src/main.rs index 609a8d34..6e3568b6 100644 --- a/examples/jwt/src/main.rs +++ b/examples/jwt/src/main.rs @@ -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 { + fn into_response(self) -> Response { let (status, error_message) = match self { AuthError::WrongCredentials => (StatusCode::UNAUTHORIZED, "Wrong credentials"), AuthError::MissingCredentials => (StatusCode::BAD_REQUEST, "Missing credentials"), diff --git a/examples/oauth/src/main.rs b/examples/oauth/src/main.rs index 250e9836..a6a70f19 100644 --- a/examples/oauth/src/main.rs +++ b/examples/oauth/src/main.rs @@ -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 { + fn into_response(self) -> Response { Redirect::found("/auth/discord".parse().unwrap()).into_response() } } diff --git a/examples/print-request-response/src/main.rs b/examples/print-request-response/src/main.rs index 277171f9..6ebf4f70 100644 --- a/examples/print-request-response/src/main.rs +++ b/examples/print-request-response/src/main.rs @@ -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) -> Result, BoxError> { Ok(req) } -async fn map_response(res: Response) -> Result, BoxError> { +async fn map_response(res: Response) -> Result, BoxError> { let (parts, body) = res.into_parts(); let bytes = buffer_and_print("response", body).await?; let res = Response::from_parts(parts, Body::from(bytes)); diff --git a/examples/templates/src/main.rs b/examples/templates/src/main.rs index 3bf1ce21..a3395fb8 100644 --- a/examples/templates/src/main.rs +++ b/examples/templates/src/main.rs @@ -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 IntoResponse for HtmlTemplate where T: Template, { - fn into_response(self) -> Response { + fn into_response(self) -> Response { match self.0.render() { Ok(html) => Html(html).into_response(), Err(err) => Response::builder() diff --git a/examples/tracing-aka-logging/src/main.rs b/examples/tracing-aka-logging/src/main.rs index 863e686d..da21a2f3 100644 --- a/examples/tracing-aka-logging/src/main.rs +++ b/examples/tracing-aka-logging/src/main.rs @@ -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| { // .. }) diff --git a/examples/validator/src/main.rs b/examples/validator/src/main.rs index bb1523b5..32771ddf 100644 --- a/examples/validator/src/main.rs +++ b/examples/validator/src/main.rs @@ -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 { + fn into_response(self) -> Response { match self { ServerError::ValidationError(_) => { let message = format!("Input validation error: [{}]", self).replace("\n", ", "); diff --git a/examples/versioning/src/main.rs b/examples/versioning/src/main.rs index aec6f385..2266b9cc 100644 --- a/examples/versioning/src/main.rs +++ b/examples/versioning/src/main.rs @@ -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 FromRequest for Version where B: Send, { - type Rejection = Response; + type Rejection = Response; async fn from_request(req: &mut RequestParts) -> Result { let params = Path::>::from_request(req)