From 6703f8634c33ad9dd3ae1afb4460b99ad26ccd49 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Mon, 20 Mar 2023 21:02:40 +0100 Subject: [PATCH] Remove `B` type param: Follow ups (#1789) Co-authored-by: Jonas Platte Co-authored-by: Michael Scofield --- axum-core/src/body.rs | 16 +--- axum-core/src/ext_traits/request.rs | 35 ++++----- axum-core/src/extract/default_body_limit.rs | 9 +-- axum-core/src/extract/mod.rs | 14 ++-- axum-core/src/extract/request_parts.rs | 26 +++++-- axum-core/src/extract/tuple.rs | 7 +- axum-core/src/response/into_response.rs | 32 ++++---- axum-core/src/response/mod.rs | 6 +- axum-extra/src/extract/cached.rs | 1 - axum-extra/src/extract/form.rs | 7 +- axum-extra/src/extract/with_rejection.rs | 7 +- axum-extra/src/handler/mod.rs | 3 +- axum-extra/src/handler/or.rs | 6 +- axum-extra/src/json_lines.rs | 5 +- axum-extra/src/protobuf.rs | 25 +++--- axum-extra/src/routing/mod.rs | 7 +- axum-macros/CHANGELOG.md | 1 + axum-macros/src/debug_handler.rs | 3 +- .../debug_handler/fail/extract_self_mut.rs | 5 +- .../fail/extract_self_mut.stderr | 4 +- .../debug_handler/fail/extract_self_ref.rs | 5 +- .../fail/extract_self_ref.stderr | 4 +- .../tests/debug_handler/pass/request_last.rs | 4 +- .../tests/debug_handler/pass/self_receiver.rs | 7 +- .../tests/debug_handler/pass/set_state.rs | 5 +- .../debug_handler/pass/state_and_body.rs | 4 +- .../from_request/pass/override_rejection.rs | 6 +- axum/CHANGELOG.md | 21 +++-- axum/src/body/mod.rs | 4 +- axum/src/body/stream_body.rs | 5 +- axum/src/boxed.rs | 19 ++--- axum/src/docs/extract.md | 38 ++++----- axum/src/docs/middleware.md | 19 +++-- axum/src/docs/response.md | 6 +- axum/src/docs/routing/route_service.md | 8 +- axum/src/extract/matched_path.rs | 8 +- axum/src/extract/mod.rs | 3 +- axum/src/extract/multipart.rs | 6 +- axum/src/extract/raw_form.rs | 6 +- axum/src/extract/request_parts.rs | 48 +----------- axum/src/extract/ws.rs | 9 +-- axum/src/form.rs | 13 ++-- axum/src/handler/future.rs | 13 ++-- axum/src/handler/mod.rs | 36 ++++----- axum/src/json.rs | 10 +-- axum/src/middleware/from_fn.rs | 78 +++++++------------ axum/src/response/mod.rs | 4 +- axum/src/response/sse.rs | 8 +- axum/src/routing/into_make_service.rs | 3 +- axum/src/routing/method_routing.rs | 78 +++++++++---------- axum/src/routing/mod.rs | 38 ++++----- axum/src/routing/route.rs | 30 +++---- axum/src/routing/tests/get_to_head.rs | 2 +- axum/src/routing/tests/merge.rs | 6 +- axum/src/routing/tests/mod.rs | 60 ++++++-------- axum/src/routing/tests/nest.rs | 8 +- axum/src/test_helpers/test_client.rs | 7 +- .../src/main.rs | 13 ++-- .../src/custom_extractor.rs | 6 +- examples/http-proxy/src/main.rs | 9 ++- examples/low-level-rustls/src/main.rs | 4 +- .../src/main.rs | 7 +- examples/print-request-response/src/main.rs | 7 +- examples/prometheus-metrics/src/main.rs | 5 +- .../src/multiplex_service.rs | 17 ++-- examples/reverse-proxy/src/main.rs | 6 +- examples/static-file-server/src/main.rs | 8 +- examples/stream-to-file/src/main.rs | 8 +- examples/testing/src/main.rs | 15 ++-- examples/validator/src/main.rs | 7 +- 70 files changed, 429 insertions(+), 531 deletions(-) diff --git a/axum-core/src/body.rs b/axum-core/src/body.rs index f3c165857f..89e69218bd 100644 --- a/axum-core/src/body.rs +++ b/axum-core/src/body.rs @@ -1,6 +1,5 @@ //! HTTP body utilities. -use crate::response::{IntoResponse, Response}; use crate::{BoxError, Error}; use bytes::Bytes; use bytes::{Buf, BufMut}; @@ -13,14 +12,9 @@ use std::pin::Pin; use std::task::{Context, Poll}; use sync_wrapper::SyncWrapper; -/// A boxed [`Body`] trait object. -/// -/// This is used in axum as the response body type for applications. It's -/// necessary to unify multiple response bodies types into one. -pub type BoxBody = http_body::combinators::UnsyncBoxBody; +type BoxBody = http_body::combinators::UnsyncBoxBody; -/// Convert a [`http_body::Body`] into a [`BoxBody`]. -pub fn boxed(body: B) -> BoxBody +fn boxed(body: B) -> BoxBody where B: http_body::Body + Send + 'static, B::Error: Into, @@ -230,12 +224,6 @@ where } } -impl IntoResponse for Body { - fn into_response(self) -> Response { - Response::new(self.0) - } -} - #[test] fn test_try_downcast() { assert_eq!(try_downcast::(5_u32), Err(5_u32)); diff --git a/axum-core/src/ext_traits/request.rs b/axum-core/src/ext_traits/request.rs index 1c69abd54e..df41a3c943 100644 --- a/axum-core/src/ext_traits/request.rs +++ b/axum-core/src/ext_traits/request.rs @@ -1,7 +1,6 @@ use crate::body::Body; -use crate::extract::{DefaultBodyLimitKind, FromRequest, FromRequestParts}; +use crate::extract::{DefaultBodyLimitKind, FromRequest, FromRequestParts, Request}; use futures_util::future::BoxFuture; -use http::Request; use http_body::Limited; mod sealed { @@ -23,9 +22,9 @@ pub trait RequestExt: sealed::Sealed + Sized { /// ``` /// use axum::{ /// async_trait, - /// extract::FromRequest, + /// extract::{Request, FromRequest}, /// body::Body, - /// http::{header::CONTENT_TYPE, Request, StatusCode}, + /// http::{header::CONTENT_TYPE, StatusCode}, /// response::{IntoResponse, Response}, /// Form, Json, RequestExt, /// }; @@ -42,7 +41,7 @@ pub trait RequestExt: sealed::Sealed + Sized { /// { /// type Rejection = Response; /// - /// async fn from_request(req: Request, _state: &S) -> Result { + /// async fn from_request(req: Request, _state: &S) -> Result { /// let content_type = req /// .headers() /// .get(CONTENT_TYPE) @@ -87,8 +86,7 @@ pub trait RequestExt: sealed::Sealed + Sized { /// use axum::{ /// async_trait, /// body::Body, - /// extract::{FromRef, FromRequest}, - /// http::Request, + /// extract::{Request, FromRef, FromRequest}, /// RequestExt, /// }; /// @@ -104,7 +102,7 @@ pub trait RequestExt: sealed::Sealed + Sized { /// { /// type Rejection = std::convert::Infallible; /// - /// async fn from_request(req: Request, state: &S) -> Result { + /// async fn from_request(req: Request, state: &S) -> Result { /// let requires_state = req.extract_with_state::(state).await?; /// /// Ok(Self { requires_state }) @@ -122,7 +120,7 @@ pub trait RequestExt: sealed::Sealed + Sized { /// { /// // ... /// # type Rejection = std::convert::Infallible; - /// # async fn from_request(req: Request, _state: &S) -> Result { + /// # async fn from_request(req: Request, _state: &S) -> Result { /// # todo!() /// # } /// } @@ -141,9 +139,8 @@ pub trait RequestExt: sealed::Sealed + Sized { /// ``` /// use axum::{ /// async_trait, - /// extract::FromRequest, + /// extract::{Request, FromRequest}, /// headers::{authorization::Bearer, Authorization}, - /// http::Request, /// response::{IntoResponse, Response}, /// body::Body, /// Json, RequestExt, TypedHeader, @@ -163,7 +160,7 @@ pub trait RequestExt: sealed::Sealed + Sized { /// { /// type Rejection = Response; /// - /// async fn from_request(mut req: Request, _state: &S) -> Result { + /// async fn from_request(mut req: Request, _state: &S) -> Result { /// let TypedHeader(auth_header) = req /// .extract_parts::>>() /// .await @@ -194,8 +191,8 @@ pub trait RequestExt: sealed::Sealed + Sized { /// ``` /// use axum::{ /// async_trait, - /// extract::{FromRef, FromRequest, FromRequestParts}, - /// http::{request::Parts, Request}, + /// extract::{Request, FromRef, FromRequest, FromRequestParts}, + /// http::request::Parts, /// response::{IntoResponse, Response}, /// body::Body, /// Json, RequestExt, @@ -216,7 +213,7 @@ pub trait RequestExt: sealed::Sealed + Sized { /// { /// type Rejection = Response; /// - /// async fn from_request(mut req: Request, state: &S) -> Result { + /// async fn from_request(mut req: Request, state: &S) -> Result { /// let requires_state = req /// .extract_parts_with_state::(state) /// .await @@ -260,7 +257,7 @@ pub trait RequestExt: sealed::Sealed + Sized { /// Apply the [default body limit](crate::extract::DefaultBodyLimit). /// /// If it is disabled, return the request as-is in `Err`. - fn with_limited_body(self) -> Result>, Request>; + fn with_limited_body(self) -> Result>, Request>; /// Consumes the request, returning the body wrapped in [`Limited`] if a /// [default limit](crate::extract::DefaultBodyLimit) is in place, or not wrapped if the @@ -268,7 +265,7 @@ pub trait RequestExt: sealed::Sealed + Sized { fn into_limited_body(self) -> Result, Body>; } -impl RequestExt for Request { +impl RequestExt for Request { fn extract(self) -> BoxFuture<'static, Result> where E: FromRequest<(), M> + 'static, @@ -321,7 +318,7 @@ impl RequestExt for Request { }) } - fn with_limited_body(self) -> Result>, Request> { + fn with_limited_body(self) -> Result>, Request> { // update docs in `axum-core/src/extract/default_body_limit.rs` and // `axum/src/docs/extract.md` if this changes const DEFAULT_LIMIT: usize = 2_097_152; // 2 mb @@ -426,7 +423,7 @@ mod tests { { type Rejection = >::Rejection; - async fn from_request(mut req: Request, state: &S) -> Result { + async fn from_request(mut req: Request, state: &S) -> Result { let RequiresState(from_state) = req.extract_parts_with_state(state).await.unwrap(); let method = req.extract_parts().await.unwrap(); let body = req.extract().await?; diff --git a/axum-core/src/extract/default_body_limit.rs b/axum-core/src/extract/default_body_limit.rs index f27e23a3c7..5a2cd971b5 100644 --- a/axum-core/src/extract/default_body_limit.rs +++ b/axum-core/src/extract/default_body_limit.rs @@ -30,22 +30,21 @@ use tower_layer::Layer; /// Router, /// routing::post, /// body::Body, -/// extract::{DefaultBodyLimit, RawBody}, -/// http::Request, +/// extract::{Request, DefaultBodyLimit}, /// }; /// /// let app = Router::new() /// .route( /// "/", /// // even with `DefaultBodyLimit` the request body is still just `Body` -/// post(|request: Request| async {}), +/// post(|request: Request| async {}), /// ) /// .layer(DefaultBodyLimit::max(1024)); /// # let _: Router = app; /// ``` /// /// ``` -/// use axum::{Router, routing::post, body::Body, extract::RawBody, http::Request}; +/// use axum::{Router, routing::post, body::Body, extract::Request}; /// use tower_http::limit::RequestBodyLimitLayer; /// use http_body::Limited; /// @@ -54,7 +53,7 @@ use tower_layer::Layer; /// "/", /// // `RequestBodyLimitLayer` changes the request body type to `Limited` /// // extracting a different body type wont work -/// post(|request: Request| async {}), +/// post(|request: Request| async {}), /// ) /// .layer(RequestBodyLimitLayer::new(1024)); /// # let _: Router = app; diff --git a/axum-core/src/extract/mod.rs b/axum-core/src/extract/mod.rs index c8b6f39463..758e5a5203 100644 --- a/axum-core/src/extract/mod.rs +++ b/axum-core/src/extract/mod.rs @@ -6,7 +6,7 @@ use crate::{body::Body, response::IntoResponse}; use async_trait::async_trait; -use http::{request::Parts, Request}; +use http::request::Parts; use std::convert::Infallible; pub mod rejection; @@ -19,6 +19,10 @@ mod tuple; pub(crate) use self::default_body_limit::DefaultBodyLimitKind; pub use self::{default_body_limit::DefaultBodyLimit, from_ref::FromRef}; +/// Type alias for [`http::Request`] whose body type defaults to [`Body`], the most common body +/// type used with axum. +pub type Request = http::Request; + mod private { #[derive(Debug, Clone, Copy)] pub enum ViaParts {} @@ -78,7 +82,7 @@ pub trait FromRequest: Sized { type Rejection: IntoResponse; /// Perform the extraction. - async fn from_request(req: Request, state: &S) -> Result; + async fn from_request(req: Request, state: &S) -> Result; } #[async_trait] @@ -89,7 +93,7 @@ where { type Rejection = >::Rejection; - async fn from_request(req: Request, state: &S) -> Result { + async fn from_request(req: Request, state: &S) -> Result { let (mut parts, _) = req.into_parts(); Self::from_request_parts(&mut parts, state).await } @@ -119,7 +123,7 @@ where { type Rejection = Infallible; - async fn from_request(req: Request, state: &S) -> Result, Self::Rejection> { + async fn from_request(req: Request, state: &S) -> Result, Self::Rejection> { Ok(T::from_request(req, state).await.ok()) } } @@ -145,7 +149,7 @@ where { type Rejection = Infallible; - async fn from_request(req: Request, state: &S) -> Result { + async fn from_request(req: Request, state: &S) -> Result { Ok(T::from_request(req, state).await) } } diff --git a/axum-core/src/extract/request_parts.rs b/axum-core/src/extract/request_parts.rs index 80ad5e78a9..98943bea35 100644 --- a/axum-core/src/extract/request_parts.rs +++ b/axum-core/src/extract/request_parts.rs @@ -1,18 +1,18 @@ -use super::{rejection::*, FromRequest, FromRequestParts}; +use super::{rejection::*, FromRequest, FromRequestParts, Request}; use crate::{body::Body, RequestExt}; use async_trait::async_trait; use bytes::Bytes; -use http::{request::Parts, HeaderMap, Method, Request, Uri, Version}; +use http::{request::Parts, HeaderMap, Method, Uri, Version}; use std::convert::Infallible; #[async_trait] -impl FromRequest for Request +impl FromRequest for Request where S: Send + Sync, { type Rejection = Infallible; - async fn from_request(req: Request, _: &S) -> Result { + async fn from_request(req: Request, _: &S) -> Result { Ok(req) } } @@ -77,7 +77,7 @@ where { type Rejection = BytesRejection; - async fn from_request(req: Request, _: &S) -> Result { + async fn from_request(req: Request, _: &S) -> Result { let bytes = match req.into_limited_body() { Ok(limited_body) => crate::body::to_bytes(limited_body) .await @@ -98,7 +98,7 @@ where { type Rejection = StringRejection; - async fn from_request(req: Request, state: &S) -> Result { + async fn from_request(req: Request, state: &S) -> Result { let bytes = Bytes::from_request(req, state) .await .map_err(|err| match err { @@ -122,7 +122,19 @@ where { type Rejection = Infallible; - async fn from_request(req: Request, _: &S) -> Result { + async fn from_request(req: Request, _: &S) -> Result { Ok(req.into_parts().0) } } + +#[async_trait] +impl FromRequest for Body +where + S: Send + Sync, +{ + type Rejection = Infallible; + + async fn from_request(req: Request, _: &S) -> Result { + Ok(req.into_body()) + } +} diff --git a/axum-core/src/extract/tuple.rs b/axum-core/src/extract/tuple.rs index 14d1e1ddd3..021b9616df 100644 --- a/axum-core/src/extract/tuple.rs +++ b/axum-core/src/extract/tuple.rs @@ -1,8 +1,7 @@ -use super::{FromRequest, FromRequestParts}; -use crate::body::Body; +use super::{FromRequest, FromRequestParts, Request}; use crate::response::{IntoResponse, Response}; use async_trait::async_trait; -use http::request::{Parts, Request}; +use http::request::Parts; use std::convert::Infallible; #[async_trait] @@ -57,7 +56,7 @@ macro_rules! impl_from_request { { type Rejection = Response; - async fn from_request(req: Request, state: &S) -> Result { + async fn from_request(req: Request, state: &S) -> Result { let (mut parts, body) = req.into_parts(); $( diff --git a/axum-core/src/response/into_response.rs b/axum-core/src/response/into_response.rs index 82efaabed2..8a40641a04 100644 --- a/axum-core/src/response/into_response.rs +++ b/axum-core/src/response/into_response.rs @@ -1,5 +1,5 @@ use super::{IntoResponseParts, Response, ResponseParts}; -use crate::{body, BoxError}; +use crate::{body::Body, BoxError}; use bytes::{buf::Chain, Buf, Bytes, BytesMut}; use http::{ header::{self, HeaderMap, HeaderName, HeaderValue}, @@ -74,9 +74,9 @@ use std::{ /// body, /// routing::get, /// response::{IntoResponse, Response}, +/// body::Body, /// Router, /// }; -/// use http_body::Body; /// use http::HeaderMap; /// use bytes::Bytes; /// use std::{ @@ -89,7 +89,7 @@ use std::{ /// /// // First implement `Body` for `MyBody`. This could for example use /// // some custom streaming protocol. -/// impl Body for MyBody { +/// impl http_body::Body for MyBody { /// type Data = Bytes; /// type Error = Infallible; /// @@ -113,7 +113,7 @@ use std::{ /// // Now we can implement `IntoResponse` directly for `MyBody` /// impl IntoResponse for MyBody { /// fn into_response(self) -> Response { -/// Response::new(body::boxed(self)) +/// Response::new(Body::new(self)) /// } /// } /// @@ -165,25 +165,31 @@ where B::Error: Into, { fn into_response(self) -> Response { - self.map(body::boxed) + self.map(Body::new) } } impl IntoResponse for http::response::Parts { fn into_response(self) -> Response { - Response::from_parts(self, body::boxed(Empty::new())) + Response::from_parts(self, Body::empty()) } } impl IntoResponse for Full { fn into_response(self) -> Response { - Response::new(body::boxed(self)) + Response::new(Body::new(self)) } } impl IntoResponse for Empty { fn into_response(self) -> Response { - Response::new(body::boxed(self)) + Response::new(Body::new(self)) + } +} + +impl IntoResponse for Body { + fn into_response(self) -> Response { + Response::new(self) } } @@ -192,7 +198,7 @@ where E: Into + 'static, { fn into_response(self) -> Response { - Response::new(body::boxed(self)) + Response::new(Body::new(self)) } } @@ -201,7 +207,7 @@ where E: Into + 'static, { fn into_response(self) -> Response { - Response::new(body::boxed(self)) + Response::new(Body::new(self)) } } @@ -212,7 +218,7 @@ where B::Error: Into, { fn into_response(self) -> Response { - Response::new(body::boxed(self)) + Response::new(Body::new(self)) } } @@ -223,7 +229,7 @@ where E: Into, { fn into_response(self) -> Response { - Response::new(body::boxed(self)) + Response::new(Body::new(self)) } } @@ -274,7 +280,7 @@ where { fn into_response(self) -> Response { let (first, second) = self.into_inner(); - let mut res = Response::new(body::boxed(BytesChainBody { + let mut res = Response::new(Body::new(BytesChainBody { first: Some(first), second: Some(second), })); diff --git a/axum-core/src/response/mod.rs b/axum-core/src/response/mod.rs index d66dfec510..b06f072f19 100644 --- a/axum-core/src/response/mod.rs +++ b/axum-core/src/response/mod.rs @@ -4,7 +4,7 @@ //! //! [`axum::response`]: https://docs.rs/axum/latest/axum/response/index.html -use crate::body::BoxBody; +use crate::body::Body; mod append_headers; mod into_response; @@ -16,9 +16,9 @@ pub use self::{ into_response_parts::{IntoResponseParts, ResponseParts, TryIntoHeaderError}, }; -/// Type alias for [`http::Response`] whose body type defaults to [`BoxBody`], the most common body +/// Type alias for [`http::Response`] whose body type defaults to [`Body`], the most common body /// type used with axum. -pub type Response = http::Response; +pub type Response = http::Response; /// An [`IntoResponse`]-based result type that uses [`ErrorResponse`] as the error type. /// diff --git a/axum-extra/src/extract/cached.rs b/axum-extra/src/extract/cached.rs index 35fbe32474..6f7d6227b7 100644 --- a/axum-extra/src/extract/cached.rs +++ b/axum-extra/src/extract/cached.rs @@ -21,7 +21,6 @@ use http::request::Parts; /// use axum::{ /// async_trait, /// extract::FromRequestParts, -/// body::BoxBody, /// response::{IntoResponse, Response}, /// http::{StatusCode, request::Parts}, /// }; diff --git a/axum-extra/src/extract/form.rs b/axum-extra/src/extract/form.rs index aad32568bf..d7ee6f9842 100644 --- a/axum-extra/src/extract/form.rs +++ b/axum-extra/src/extract/form.rs @@ -1,11 +1,10 @@ use axum::{ async_trait, - body::Body, - extract::{rejection::RawFormRejection, FromRequest, RawForm}, + extract::{rejection::RawFormRejection, FromRequest, RawForm, Request}, response::{IntoResponse, Response}, Error, RequestExt, }; -use http::{Request, StatusCode}; +use http::StatusCode; use serde::de::DeserializeOwned; use std::fmt; @@ -53,7 +52,7 @@ where { type Rejection = FormRejection; - async fn from_request(req: Request, _state: &S) -> Result { + async fn from_request(req: Request, _state: &S) -> Result { let RawForm(bytes) = req .extract() .await diff --git a/axum-extra/src/extract/with_rejection.rs b/axum-extra/src/extract/with_rejection.rs index c5b9c326f7..1227a1ab13 100644 --- a/axum-extra/src/extract/with_rejection.rs +++ b/axum-extra/src/extract/with_rejection.rs @@ -1,9 +1,7 @@ use axum::async_trait; -use axum::body::Body; -use axum::extract::{FromRequest, FromRequestParts}; +use axum::extract::{FromRequest, FromRequestParts, Request}; use axum::response::IntoResponse; use http::request::Parts; -use http::Request; use std::fmt::Debug; use std::marker::PhantomData; use std::ops::{Deref, DerefMut}; @@ -118,7 +116,7 @@ where { type Rejection = R; - async fn from_request(req: Request, state: &S) -> Result { + async fn from_request(req: Request, state: &S) -> Result { let extractor = E::from_request(req, state).await?; Ok(WithRejection(extractor, PhantomData)) } @@ -141,6 +139,7 @@ where #[cfg(test)] mod tests { + use axum::body::Body; use axum::extract::FromRequestParts; use axum::http::Request; use axum::response::Response; diff --git a/axum-extra/src/handler/mod.rs b/axum-extra/src/handler/mod.rs index 275f8418cd..d017b41ef6 100644 --- a/axum-extra/src/handler/mod.rs +++ b/axum-extra/src/handler/mod.rs @@ -1,6 +1,7 @@ //! Additional handler utilities. use axum::body::Body; +use axum::extract::Request; use axum::{ extract::FromRequest, handler::Handler, @@ -174,7 +175,7 @@ where { type Future = BoxFuture<'static, Response>; - fn call(self, req: http::Request, state: S) -> Self::Future { + fn call(self, req: Request, state: S) -> Self::Future { let req = req.map(Body::new); Box::pin(async move { match T::from_request(req, &state).await { diff --git a/axum-extra/src/handler/or.rs b/axum-extra/src/handler/or.rs index b23ae5d4ee..7b78fe3d73 100644 --- a/axum-extra/src/handler/or.rs +++ b/axum-extra/src/handler/or.rs @@ -1,10 +1,8 @@ use super::HandlerCallWithExtractors; use crate::either::Either; use axum::{ - body::Body, - extract::{FromRequest, FromRequestParts}, + extract::{FromRequest, FromRequestParts, Request}, handler::Handler, - http::Request, response::{IntoResponse, Response}, }; use futures_util::future::{BoxFuture, Either as EitherFuture, FutureExt, Map}; @@ -67,7 +65,7 @@ where // this puts `futures_util` in our public API but thats fine in axum-extra type Future = BoxFuture<'static, Response>; - fn call(self, req: Request, state: S) -> Self::Future { + fn call(self, req: Request, state: S) -> Self::Future { Box::pin(async move { let (mut parts, body) = req.into_parts(); diff --git a/axum-extra/src/json_lines.rs b/axum-extra/src/json_lines.rs index 973e418e39..7cf47b04a3 100644 --- a/axum-extra/src/json_lines.rs +++ b/axum-extra/src/json_lines.rs @@ -3,13 +3,12 @@ use axum::{ async_trait, body::Body, - extract::FromRequest, + extract::{FromRequest, Request}, response::{IntoResponse, Response}, BoxError, }; use bytes::{BufMut, BytesMut}; use futures_util::stream::{BoxStream, Stream, TryStream, TryStreamExt}; -use http::Request; use pin_project_lite::pin_project; use serde::{de::DeserializeOwned, Serialize}; use std::{ @@ -108,7 +107,7 @@ where { type Rejection = Infallible; - async fn from_request(req: Request, _state: &S) -> Result { + async fn from_request(req: Request, _state: &S) -> Result { // `Stream::lines` isn't a thing so we have to convert it into an `AsyncRead` // so we can call `AsyncRead::lines` and then convert it back to a `Stream` let body = req.into_body(); diff --git a/axum-extra/src/protobuf.rs b/axum-extra/src/protobuf.rs index d5f19eb605..ce08bd7124 100644 --- a/axum-extra/src/protobuf.rs +++ b/axum-extra/src/protobuf.rs @@ -2,12 +2,11 @@ use axum::{ async_trait, - body::Body, - extract::{rejection::BytesRejection, FromRequest}, + extract::{rejection::BytesRejection, FromRequest, Request}, response::{IntoResponse, Response}, }; use bytes::{Bytes, BytesMut}; -use http::{Request, StatusCode}; +use http::StatusCode; use prost::Message; /// A Protocol Buffer message extractor and response. @@ -103,7 +102,7 @@ where { type Rejection = ProtobufRejection; - async fn from_request(req: Request, state: &S) -> Result { + async fn from_request(req: Request, state: &S) -> Result { let mut bytes = Bytes::from_request(req, state).await?; match T::decode(&mut bytes) { @@ -273,16 +272,16 @@ mod tests { result: String, } - let app = Router::new().route( - "/", - post(|input: Protobuf| async move { - let output = Output { - result: input.foo.to_owned(), - }; + #[axum::debug_handler] + async fn handler(input: Protobuf) -> Protobuf { + let output = Output { + result: input.foo.to_owned(), + }; - Protobuf(output) - }), - ); + Protobuf(output) + } + + let app = Router::new().route("/", post(handler)); let input = Input { foo: "bar".to_owned(), diff --git a/axum-extra/src/routing/mod.rs b/axum-extra/src/routing/mod.rs index 47d2e84a86..40cb336df8 100644 --- a/axum-extra/src/routing/mod.rs +++ b/axum-extra/src/routing/mod.rs @@ -1,8 +1,7 @@ //! Additional types for defining routes. use axum::{ - body::Body, - http::Request, + extract::Request, response::{IntoResponse, Redirect, Response}, routing::{any, MethodRouter}, Router, @@ -166,7 +165,7 @@ pub trait RouterExt: sealed::Sealed { /// This works like [`RouterExt::route_with_tsr`] but accepts any [`Service`]. fn route_service_with_tsr(self, path: &str, service: T) -> Self where - T: Service, Error = Infallible> + Clone + Send + 'static, + T: Service + Clone + Send + 'static, T::Response: IntoResponse, T::Future: Send + 'static, Self: Sized; @@ -269,7 +268,7 @@ where #[track_caller] fn route_service_with_tsr(mut self, path: &str, service: T) -> Self where - T: Service, Error = Infallible> + Clone + Send + 'static, + T: Service + Clone + Send + 'static, T::Response: IntoResponse, T::Future: Send + 'static, Self: Sized, diff --git a/axum-macros/CHANGELOG.md b/axum-macros/CHANGELOG.md index 1326807fab..5d5d5645a2 100644 --- a/axum-macros/CHANGELOG.md +++ b/axum-macros/CHANGELOG.md @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 request-consuming extractors ([#1826]) [#1826]: https://github.com/tokio-rs/axum/pull/1826 +[#1751]: https://github.com/tokio-rs/axum/pull/1751 # 0.3.5 (03. March, 2023) diff --git a/axum-macros/src/debug_handler.rs b/axum-macros/src/debug_handler.rs index d44371f95a..0f361a10cb 100644 --- a/axum-macros/src/debug_handler.rs +++ b/axum-macros/src/debug_handler.rs @@ -43,10 +43,9 @@ pub(crate) fn expand(attr: Attrs, item_fn: ItemFn) -> TokenStream { err.unwrap_or_else(|| { let state_ty = state_ty.unwrap_or_else(|| syn::parse_quote!(())); - let check_input_order = check_input_order(&item_fn); let check_future_send = check_future_send(&item_fn); - if let Some(check_input_order) = check_input_order { + if let Some(check_input_order) = check_input_order(&item_fn) { quote! { #check_input_order #check_future_send diff --git a/axum-macros/tests/debug_handler/fail/extract_self_mut.rs b/axum-macros/tests/debug_handler/fail/extract_self_mut.rs index 1d45b50494..21ae99d6b8 100644 --- a/axum-macros/tests/debug_handler/fail/extract_self_mut.rs +++ b/axum-macros/tests/debug_handler/fail/extract_self_mut.rs @@ -1,7 +1,6 @@ use axum::{ async_trait, - extract::FromRequest, - http::Request, + extract::{Request, FromRequest}, }; use axum_macros::debug_handler; @@ -14,7 +13,7 @@ where { type Rejection = (); - async fn from_request(_req: Request, _state: &S) -> Result { + async fn from_request(_req: Request, _state: &S) -> Result { unimplemented!() } } diff --git a/axum-macros/tests/debug_handler/fail/extract_self_mut.stderr b/axum-macros/tests/debug_handler/fail/extract_self_mut.stderr index 3d80dffbca..595786bf4e 100644 --- a/axum-macros/tests/debug_handler/fail/extract_self_mut.stderr +++ b/axum-macros/tests/debug_handler/fail/extract_self_mut.stderr @@ -1,5 +1,5 @@ error: Handlers must only take owned values - --> tests/debug_handler/fail/extract_self_mut.rs:24:22 + --> tests/debug_handler/fail/extract_self_mut.rs:23:22 | -24 | async fn handler(&mut self) {} +23 | async fn handler(&mut self) {} | ^^^^^^^^^ diff --git a/axum-macros/tests/debug_handler/fail/extract_self_ref.rs b/axum-macros/tests/debug_handler/fail/extract_self_ref.rs index e5c3bb032d..8e32811994 100644 --- a/axum-macros/tests/debug_handler/fail/extract_self_ref.rs +++ b/axum-macros/tests/debug_handler/fail/extract_self_ref.rs @@ -1,7 +1,6 @@ use axum::{ async_trait, - extract::FromRequest, - http::Request, + extract::{Request, FromRequest}, }; use axum_macros::debug_handler; @@ -14,7 +13,7 @@ where { type Rejection = (); - async fn from_request(_req: Request, _state: &S) -> Result { + async fn from_request(_req: Request, _state: &S) -> Result { unimplemented!() } } diff --git a/axum-macros/tests/debug_handler/fail/extract_self_ref.stderr b/axum-macros/tests/debug_handler/fail/extract_self_ref.stderr index 82d9a89ff5..4c0b4950c7 100644 --- a/axum-macros/tests/debug_handler/fail/extract_self_ref.stderr +++ b/axum-macros/tests/debug_handler/fail/extract_self_ref.stderr @@ -1,5 +1,5 @@ error: Handlers must only take owned values - --> tests/debug_handler/fail/extract_self_ref.rs:24:22 + --> tests/debug_handler/fail/extract_self_ref.rs:23:22 | -24 | async fn handler(&self) {} +23 | async fn handler(&self) {} | ^^^^^ diff --git a/axum-macros/tests/debug_handler/pass/request_last.rs b/axum-macros/tests/debug_handler/pass/request_last.rs index bbfb53d28e..da1531f458 100644 --- a/axum-macros/tests/debug_handler/pass/request_last.rs +++ b/axum-macros/tests/debug_handler/pass/request_last.rs @@ -1,7 +1,7 @@ -use axum::{body::Body, extract::Extension, http::Request}; +use axum::extract::{Extension, Request}; use axum_macros::debug_handler; #[debug_handler] -async fn handler(_: Extension, _: Request) {} +async fn handler(_: Extension, _: Request) {} fn main() {} diff --git a/axum-macros/tests/debug_handler/pass/self_receiver.rs b/axum-macros/tests/debug_handler/pass/self_receiver.rs index 2b4dfa4d6b..9b72284502 100644 --- a/axum-macros/tests/debug_handler/pass/self_receiver.rs +++ b/axum-macros/tests/debug_handler/pass/self_receiver.rs @@ -1,7 +1,6 @@ use axum::{ async_trait, - extract::FromRequest, - http::Request, + extract::{Request, FromRequest}, }; use axum_macros::debug_handler; @@ -14,7 +13,7 @@ where { type Rejection = (); - async fn from_request(_req: Request, _state: &S) -> Result { + async fn from_request(_req: Request, _state: &S) -> Result { unimplemented!() } } @@ -26,7 +25,7 @@ where { type Rejection = (); - async fn from_request(_req: Request, _state: &S) -> Result { + async fn from_request(_req: Request, _state: &S) -> Result { unimplemented!() } } diff --git a/axum-macros/tests/debug_handler/pass/set_state.rs b/axum-macros/tests/debug_handler/pass/set_state.rs index 7c06742e54..60a7a3304e 100644 --- a/axum-macros/tests/debug_handler/pass/set_state.rs +++ b/axum-macros/tests/debug_handler/pass/set_state.rs @@ -1,7 +1,6 @@ use axum_macros::debug_handler; -use axum::extract::{FromRef, FromRequest}; +use axum::extract::{Request, FromRef, FromRequest}; use axum::async_trait; -use axum::http::Request; #[debug_handler(state = AppState)] async fn handler(_: A) {} @@ -19,7 +18,7 @@ where { type Rejection = (); - async fn from_request(_req: Request, _state: &S) -> Result { + async fn from_request(_req: Request, _state: &S) -> Result { unimplemented!() } } diff --git a/axum-macros/tests/debug_handler/pass/state_and_body.rs b/axum-macros/tests/debug_handler/pass/state_and_body.rs index fea3700745..f348360b3a 100644 --- a/axum-macros/tests/debug_handler/pass/state_and_body.rs +++ b/axum-macros/tests/debug_handler/pass/state_and_body.rs @@ -1,8 +1,8 @@ use axum_macros::debug_handler; -use axum::{extract::State, http::Request}; +use axum::{extract::State, extract::Request}; #[debug_handler(state = AppState)] -async fn handler(_: State, _: Request) {} +async fn handler(_: State, _: Request) {} #[derive(Clone)] struct AppState; diff --git a/axum-macros/tests/from_request/pass/override_rejection.rs b/axum-macros/tests/from_request/pass/override_rejection.rs index db341b792e..779058b9fc 100644 --- a/axum-macros/tests/from_request/pass/override_rejection.rs +++ b/axum-macros/tests/from_request/pass/override_rejection.rs @@ -1,7 +1,7 @@ use axum::{ async_trait, - extract::{rejection::ExtensionRejection, FromRequest}, - http::{StatusCode, Request}, + extract::{Request, rejection::ExtensionRejection, FromRequest}, + http::StatusCode, response::{IntoResponse, Response}, routing::get, body::Body, @@ -35,7 +35,7 @@ where // this rejection doesn't implement `Display` and `Error` type Rejection = (StatusCode, String); - async fn from_request(_req: Request, _state: &S) -> Result { + async fn from_request(_req: Request, _state: &S) -> Result { todo!() } } diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index 09cdacc58d..c877677e57 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -8,18 +8,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased - **breaking:** The following types/traits are no longer generic over the request body - (i.e. the `B` type param has been removed) ([#1751]): - - `FromRequest` + (i.e. the `B` type param has been removed) ([#1751] and [#1789]): - `FromRequestParts` - - `Handler` + - `FromRequest` - `HandlerService` - `HandlerWithoutStateExt` - - `Layered` + - `Handler` - `LayeredFuture` + - `Layered` - `MethodRouter` + - `Next` - `RequestExt` - - `Route` - `RouteFuture` + - `Route` - `Router` - **breaking:** axum no longer re-exports `hyper::Body` as that type is removed in hyper 1.0. Instead axum has its own body type at `axum::body::Body` ([#1751]) @@ -28,6 +29,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **breaking:** Change `sse::Event::json_data` to use `axum_core::Error` as its error type ([#1762]) - **breaking:** Rename `DefaultOnFailedUpdgrade` to `DefaultOnFailedUpgrade` ([#1664]) - **breaking:** Rename `OnFailedUpdgrade` to `OnFailedUpgrade` ([#1664]) +- **breaking:** Removed re-exports of `Empty` and `Full`. Use + `axum::body::Body::empty` and `axum::body::Body::from` respectively ([#1789]) +- **breaking:** The response returned by `IntoResponse::into_response` must use + `axum::body::Body` as the body type. `axum::response::Response` does this + ([#1789]) +- **breaking:** Removed the `BoxBody` type alias and its `box_body` + constructor. Use `axum::body::Body::new` instead ([#1789]) +- **breaking:** Remove `RawBody` extractor. `axum::body::Body` implements `FromRequest` directly ([#1789]) +- **added:** Add `axum::extract::Request` type alias where the body is `axum::body::Body` ([#1789]) - **added:** Add `Router::as_service` and `Router::into_service` to workaround type inference issues when calling `ServiceExt` methods on a `Router` ([#1835]) @@ -35,6 +45,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#1751]: https://github.com/tokio-rs/axum/pull/1751 [#1762]: https://github.com/tokio-rs/axum/pull/1762 [#1835]: https://github.com/tokio-rs/axum/pull/1835 +[#1789]: https://github.com/tokio-rs/axum/pull/1789 # 0.6.16 (18. April, 2023) diff --git a/axum/src/body/mod.rs b/axum/src/body/mod.rs index a1243ffd87..0e3d478179 100644 --- a/axum/src/body/mod.rs +++ b/axum/src/body/mod.rs @@ -1,10 +1,10 @@ //! HTTP body utilities. #[doc(no_inline)] -pub use http_body::{Body as HttpBody, Empty, Full}; +pub use http_body::Body as HttpBody; #[doc(no_inline)] pub use bytes::Bytes; #[doc(inline)] -pub use axum_core::body::{boxed, Body, BoxBody}; +pub use axum_core::body::Body; diff --git a/axum/src/body/stream_body.rs b/axum/src/body/stream_body.rs index df782c6987..6331567a74 100644 --- a/axum/src/body/stream_body.rs +++ b/axum/src/body/stream_body.rs @@ -1,8 +1,9 @@ use crate::{ - body::{self, Bytes, HttpBody}, + body::{Bytes, HttpBody}, response::{IntoResponse, Response}, BoxError, Error, }; +use axum_core::body::Body; use futures_util::{ ready, stream::{self, TryStream}, @@ -93,7 +94,7 @@ where S::Error: Into, { fn into_response(self) -> Response { - Response::new(body::boxed(self)) + Response::new(Body::new(self)) } } diff --git a/axum/src/boxed.rs b/axum/src/boxed.rs index 84ddbf22d2..5473b2491b 100644 --- a/axum/src/boxed.rs +++ b/axum/src/boxed.rs @@ -1,7 +1,6 @@ use std::{convert::Infallible, fmt}; -use axum_core::body::Body; -use http::Request; +use crate::extract::Request; use tower::Service; use crate::{ @@ -64,7 +63,7 @@ pub(crate) trait ErasedIntoRoute: Send { fn into_route(self: Box, state: S) -> Route; - fn call_with_state(self: Box, request: Request, state: S) -> RouteFuture; + fn call_with_state(self: Box, request: Request, state: S) -> RouteFuture; } pub(crate) struct MakeErasedHandler { @@ -85,11 +84,7 @@ where (self.into_route)(self.handler, state) } - fn call_with_state( - self: Box, - request: Request, - state: S, - ) -> RouteFuture { + fn call_with_state(self: Box, request: Request, state: S) -> RouteFuture { self.into_route(state).call(request) } } @@ -123,11 +118,7 @@ where (self.into_route)(self.router, state) } - fn call_with_state( - mut self: Box, - request: Request, - state: S, - ) -> RouteFuture { + fn call_with_state(mut self: Box, request: Request, state: S) -> RouteFuture { self.router.call_with_state(request, state) } } @@ -166,7 +157,7 @@ where (self.layer)(self.inner.into_route(state)) } - fn call_with_state(self: Box, request: Request, state: S) -> RouteFuture { + fn call_with_state(self: Box, request: Request, state: S) -> RouteFuture { (self.layer)(self.inner.into_route(state)).call(request) } } diff --git a/axum/src/docs/extract.md b/axum/src/docs/extract.md index 4f90c2b8e1..e5e188a906 100644 --- a/axum/src/docs/extract.md +++ b/axum/src/docs/extract.md @@ -58,10 +58,10 @@ Some commonly used extractors are: ```rust,no_run use axum::{ - extract::{Json, TypedHeader, Path, Extension, Query}, + extract::{Request, Json, TypedHeader, Path, Extension, Query}, routing::post, headers::UserAgent, - http::{Request, header::HeaderMap}, + http::header::HeaderMap, body::{Bytes, Body}, Router, }; @@ -92,7 +92,7 @@ async fn bytes(body: Bytes) {} async fn json(Json(payload): Json) {} // `Request` gives you the whole request for maximum control -async fn request(request: Request) {} +async fn request(request: Request) {} // `Extension` extracts data from "request extensions" // This is commonly used to share state with handlers @@ -464,7 +464,7 @@ If your extractor needs to consume the request body you must implement [`FromReq ```rust,no_run use axum::{ async_trait, - extract::FromRequest, + extract::{Request, FromRequest}, response::{Response, IntoResponse}, body::{Bytes, Body}, routing::get, @@ -472,7 +472,6 @@ use axum::{ http::{ StatusCode, header::{HeaderValue, USER_AGENT}, - Request, }, }; @@ -486,7 +485,7 @@ where { type Rejection = Response; - async fn from_request(req: Request, state: &S) -> Result { + async fn from_request(req: Request, state: &S) -> Result { let body = Bytes::from_request(req, state) .await .map_err(IntoResponse::into_response)?; @@ -517,8 +516,8 @@ wrapping another extractor: use axum::{ Router, routing::get, - extract::{FromRequest, FromRequestParts}, - http::{Request, request::Parts}, + extract::{FromRequest, Request, FromRequestParts}, + http::request::Parts, body::Body, async_trait, }; @@ -535,7 +534,7 @@ where { type Rejection = Infallible; - async fn from_request(req: Request, state: &S) -> Result { + async fn from_request(req: Request, state: &S) -> Result { // ... # todo!() } @@ -645,20 +644,17 @@ Extractors can also be run from middleware: ```rust use axum::{ middleware::{self, Next}, - extract::{TypedHeader, FromRequestParts}, - http::{Request, StatusCode}, + extract::{TypedHeader, Request, FromRequestParts}, + http::StatusCode, response::Response, headers::authorization::{Authorization, Bearer}, RequestPartsExt, Router, }; -async fn auth_middleware( - request: Request, - next: Next, -) -> Result -where - B: Send, -{ +async fn auth_middleware( + request: Request, + next: Next, +) -> Result { // running extractors requires a `axum::http::request::Parts` let (mut parts, body) = request.into_parts(); @@ -697,8 +693,8 @@ use axum::{ Router, body::Body, routing::get, - extract::{FromRequest, FromRequestParts}, - http::{Request, HeaderMap, request::Parts}, + extract::{Request, FromRequest, FromRequestParts}, + http::{HeaderMap, request::Parts}, async_trait, }; use std::time::{Instant, Duration}; @@ -738,7 +734,7 @@ where { type Rejection = T::Rejection; - async fn from_request(req: Request, state: &S) -> Result { + async fn from_request(req: Request, state: &S) -> Result { let start = Instant::now(); let extractor = T::from_request(req, state).await?; let duration = start.elapsed(); diff --git a/axum/src/docs/middleware.md b/axum/src/docs/middleware.md index 05f17fa1a9..6aca7a3ead 100644 --- a/axum/src/docs/middleware.md +++ b/axum/src/docs/middleware.md @@ -223,7 +223,7 @@ A decent template for such a middleware could be: use axum::{ response::Response, body::Body, - http::Request, + extract::Request, }; use futures_util::future::BoxFuture; use tower::{Service, Layer}; @@ -245,9 +245,9 @@ struct MyMiddleware { inner: S, } -impl Service> for MyMiddleware +impl Service for MyMiddleware where - S: Service, Response = Response> + Send + 'static, + S: Service + Send + 'static, S::Future: Send + 'static, { type Response = S::Response; @@ -259,7 +259,7 @@ where self.inner.poll_ready(cx) } - fn call(&mut self, request: Request) -> Self::Future { + fn call(&mut self, request: Request) -> Self::Future { let future = self.inner.call(request); Box::pin(async move { let response: Response = future.await?; @@ -406,8 +406,7 @@ use axum::{ routing::get, middleware::{self, Next}, response::Response, - extract::State, - http::Request, + extract::{State, Request}, }; use tower::{Layer, Service}; use std::task::{Context, Poll}; @@ -477,17 +476,17 @@ State can be passed from middleware to handlers using [request extensions]: ```rust use axum::{ Router, - http::{Request, StatusCode}, + http::StatusCode, routing::get, response::{IntoResponse, Response}, middleware::{self, Next}, - extract::Extension, + extract::{Request, Extension}, }; #[derive(Clone)] struct CurrentUser { /* ... */ } -async fn auth(mut req: Request, next: Next) -> Result { +async fn auth(mut req: Request, next: Next) -> Result { let auth_header = req.headers() .get(http::header::AUTHORIZATION) .and_then(|header| header.to_str().ok()); @@ -546,7 +545,7 @@ use axum::{ ServiceExt, // for `into_make_service` response::Response, middleware::Next, - http::Request, + extract::Request, }; fn rewrite_request_uri(req: Request) -> Request { diff --git a/axum/src/docs/response.md b/axum/src/docs/response.md index 2afe476046..4e1d50b5e2 100644 --- a/axum/src/docs/response.md +++ b/axum/src/docs/response.md @@ -171,15 +171,15 @@ Use [`Response`](crate::response::Response) for more low level control: use axum::{ Json, response::{IntoResponse, Response}, - body::{Full, Bytes}, + body::Body, http::StatusCode, }; -async fn response() -> Response> { +async fn response() -> Response { Response::builder() .status(StatusCode::NOT_FOUND) .header("x-foo", "custom header") - .body(Full::from("not found")) + .body(Body::from("not found")) .unwrap() } ``` diff --git a/axum/src/docs/routing/route_service.md b/axum/src/docs/routing/route_service.md index 1be229e4c6..190e0183a3 100644 --- a/axum/src/docs/routing/route_service.md +++ b/axum/src/docs/routing/route_service.md @@ -7,7 +7,8 @@ use axum::{ Router, body::Body, routing::{any_service, get_service}, - http::{Request, StatusCode}, + extract::Request, + http::StatusCode, error_handling::HandleErrorLayer, }; use tower_http::services::ServeFile; @@ -22,7 +23,7 @@ let app = Router::new() // Services whose response body is not `axum::body::BoxBody` // can be wrapped in `axum::routing::any_service` (or one of the other routing filters) // to have the response body mapped - any_service(service_fn(|_: Request| async { + any_service(service_fn(|_: Request| async { let res = Response::new(Body::from("Hi from `GET /`")); Ok::<_, Infallible>(res) })) @@ -31,9 +32,8 @@ let app = Router::new() "/foo", // This service's response body is `axum::body::BoxBody` so // it can be routed to directly. - service_fn(|req: Request| async move { + service_fn(|req: Request| async move { let body = Body::from(format!("Hi from `{} /foo`", req.method())); - let body = axum::body::boxed(body); let res = Response::new(body); Ok::<_, Infallible>(res) }) diff --git a/axum/src/extract/matched_path.rs b/axum/src/extract/matched_path.rs index c4f9984e24..50224c32d3 100644 --- a/axum/src/extract/matched_path.rs +++ b/axum/src/extract/matched_path.rs @@ -35,8 +35,7 @@ use std::{collections::HashMap, sync::Arc}; /// ``` /// use axum::{ /// Router, -/// extract::MatchedPath, -/// http::Request, +/// extract::{Request, MatchedPath}, /// routing::get, /// }; /// use tower_http::trace::TraceLayer; @@ -65,13 +64,12 @@ use std::{collections::HashMap, sync::Arc}; /// Router, /// RequestExt, /// routing::get, -/// extract::{MatchedPath, rejection::MatchedPathRejection}, +/// extract::{Request, MatchedPath, rejection::MatchedPathRejection}, /// middleware::map_request, -/// http::Request, /// body::Body, /// }; /// -/// async fn access_matched_path(mut request: Request) -> Request { +/// async fn access_matched_path(mut request: Request) -> Request { /// // if `/foo/bar` is called this will be `Err(_)` since that matches /// // a nested route /// let matched_path: Result = diff --git a/axum/src/extract/mod.rs b/axum/src/extract/mod.rs index b93cc7f459..19d46cb5e6 100644 --- a/axum/src/extract/mod.rs +++ b/axum/src/extract/mod.rs @@ -17,7 +17,7 @@ mod request_parts; mod state; #[doc(inline)] -pub use axum_core::extract::{DefaultBodyLimit, FromRef, FromRequest, FromRequestParts}; +pub use axum_core::extract::{DefaultBodyLimit, FromRef, FromRequest, FromRequestParts, Request}; #[cfg(feature = "macros")] pub use axum_macros::{FromRef, FromRequest, FromRequestParts}; @@ -29,7 +29,6 @@ pub use self::{ path::{Path, RawPathParams}, raw_form::RawForm, raw_query::RawQuery, - request_parts::RawBody, state::State, }; diff --git a/axum/src/extract/multipart.rs b/axum/src/extract/multipart.rs index 2592c96d5b..ae8f9da1ff 100644 --- a/axum/src/extract/multipart.rs +++ b/axum/src/extract/multipart.rs @@ -2,7 +2,7 @@ //! //! See [`Multipart`] for more details. -use super::FromRequest; +use super::{FromRequest, Request}; use crate::body::Bytes; use async_trait::async_trait; use axum_core::__composite_rejection as composite_rejection; @@ -12,7 +12,7 @@ use axum_core::body::Body; use axum_core::RequestExt; use futures_util::stream::Stream; use http::header::{HeaderMap, CONTENT_TYPE}; -use http::{Request, StatusCode}; +use http::StatusCode; use std::error::Error; use std::{ fmt, @@ -65,7 +65,7 @@ where { type Rejection = MultipartRejection; - async fn from_request(req: Request, _state: &S) -> Result { + async fn from_request(req: Request, _state: &S) -> Result { let boundary = parse_boundary(req.headers()).ok_or(InvalidBoundary)?; let stream = match req.with_limited_body() { Ok(limited) => Body::new(limited), diff --git a/axum/src/extract/raw_form.rs b/axum/src/extract/raw_form.rs index ad74686817..b1a6f97336 100644 --- a/axum/src/extract/raw_form.rs +++ b/axum/src/extract/raw_form.rs @@ -1,7 +1,7 @@ use async_trait::async_trait; -use axum_core::{body::Body, extract::FromRequest}; +use axum_core::extract::{FromRequest, Request}; use bytes::{Bytes, BytesMut}; -use http::{Method, Request}; +use http::Method; use super::{ has_content_type, @@ -39,7 +39,7 @@ where { type Rejection = RawFormRejection; - async fn from_request(req: Request, state: &S) -> Result { + async fn from_request(req: Request, state: &S) -> Result { if req.method() == Method::GET { let mut bytes = BytesMut::new(); diff --git a/axum/src/extract/request_parts.rs b/axum/src/extract/request_parts.rs index 00e4a2b1a8..7dfb2f2609 100644 --- a/axum/src/extract/request_parts.rs +++ b/axum/src/extract/request_parts.rs @@ -1,7 +1,6 @@ -use super::{Extension, FromRequest, FromRequestParts}; -use crate::body::Body; +use super::{Extension, FromRequestParts}; use async_trait::async_trait; -use http::{request::Parts, Request, Uri}; +use http::{request::Parts, Uri}; use std::convert::Infallible; /// Extractor that gets the original request URI regardless of nesting. @@ -94,49 +93,6 @@ where #[cfg(feature = "original-uri")] axum_core::__impl_deref!(OriginalUri: Uri); -/// Extractor that extracts the raw request body. -/// -/// Since extracting the raw request body requires consuming it, the `RawBody` extractor must be -/// *last* if there are multiple extractors in a handler. See ["the order of extractors"][order-of-extractors] -/// -/// [order-of-extractors]: crate::extract#the-order-of-extractors -/// -/// # Example -/// -/// ```rust,no_run -/// use axum::{ -/// extract::RawBody, -/// routing::get, -/// Router, -/// }; -/// use futures_util::StreamExt; -/// -/// async fn handler(RawBody(body): RawBody) { -/// // ... -/// } -/// -/// let app = Router::new().route("/users", get(handler)); -/// # async { -/// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap(); -/// # }; -/// ``` -/// -/// [`body::Body`]: crate::body::Body -#[derive(Debug, Default)] -pub struct RawBody(pub Body); - -#[async_trait] -impl FromRequest for RawBody -where - S: Send + Sync, -{ - type Rejection = Infallible; - - async fn from_request(req: Request, _state: &S) -> Result { - Ok(Self(req.into_body())) - } -} - #[cfg(test)] mod tests { use crate::{extract::Extension, routing::get, test_helpers::*, Router}; diff --git a/axum/src/extract/ws.rs b/axum/src/extract/ws.rs index 9f4a70b460..d3a014cece 100644 --- a/axum/src/extract/ws.rs +++ b/axum/src/extract/ws.rs @@ -96,12 +96,9 @@ use self::rejection::*; use super::FromRequestParts; -use crate::{ - body::{self, Bytes}, - response::Response, - Error, -}; +use crate::{body::Bytes, response::Response, Error}; use async_trait::async_trait; +use axum_core::body::Body; use futures_util::{ sink::{Sink, SinkExt}, stream::{Stream, StreamExt}, @@ -332,7 +329,7 @@ impl WebSocketUpgrade { builder = builder.header(header::SEC_WEBSOCKET_PROTOCOL, protocol); } - builder.body(body::boxed(body::Empty::new())).unwrap() + builder.body(Body::empty()).unwrap() } } diff --git a/axum/src/form.rs b/axum/src/form.rs index 68ec656fbe..b197e6e27f 100644 --- a/axum/src/form.rs +++ b/axum/src/form.rs @@ -1,10 +1,10 @@ +use crate::extract::Request; use crate::extract::{rejection::*, FromRequest, RawForm}; use async_trait::async_trait; -use axum_core::body::Body; use axum_core::response::{IntoResponse, Response}; use axum_core::RequestExt; use http::header::CONTENT_TYPE; -use http::{Request, StatusCode}; +use http::StatusCode; use serde::de::DeserializeOwned; use serde::Serialize; @@ -70,7 +70,7 @@ where { type Rejection = FormRejection; - async fn from_request(req: Request, _state: &S) -> Result { + async fn from_request(req: Request, _state: &S) -> Result { let is_get_or_head = req.method() == http::Method::GET || req.method() == http::Method::HEAD; @@ -114,14 +114,15 @@ axum_core::__impl_deref!(Form); #[cfg(test)] mod tests { - use super::*; use crate::{ - body::Body, routing::{on, MethodFilter}, test_helpers::TestClient, Router, }; - use http::{header::CONTENT_TYPE, Method, Request}; + + use super::*; + use axum_core::body::Body; + use http::{Method, Request}; use mime::APPLICATION_WWW_FORM_URLENCODED; use serde::{Deserialize, Serialize}; use std::fmt::Debug; diff --git a/axum/src/handler/future.rs b/axum/src/handler/future.rs index 48f51a004a..751984d0c6 100644 --- a/axum/src/handler/future.rs +++ b/axum/src/handler/future.rs @@ -1,9 +1,8 @@ //! Handler future types. -use crate::body::Body; use crate::response::Response; +use axum_core::extract::Request; use futures_util::future::Map; -use http::Request; use pin_project_lite::pin_project; use std::{convert::Infallible, future::Future, pin::Pin, task::Context}; use tower::util::Oneshot; @@ -22,19 +21,19 @@ pin_project! { /// The response future for [`Layered`](super::Layered). pub struct LayeredFuture where - S: Service>, + S: Service, { #[pin] - inner: Map>, fn(Result) -> Response>, + inner: Map, fn(Result) -> Response>, } } impl LayeredFuture where - S: Service>, + S: Service, { pub(super) fn new( - inner: Map>, fn(Result) -> Response>, + inner: Map, fn(Result) -> Response>, ) -> Self { Self { inner } } @@ -42,7 +41,7 @@ where impl Future for LayeredFuture where - S: Service>, + S: Service, { type Output = Response; diff --git a/axum/src/handler/mod.rs b/axum/src/handler/mod.rs index eb539e5367..69e7879c1a 100644 --- a/axum/src/handler/mod.rs +++ b/axum/src/handler/mod.rs @@ -37,12 +37,10 @@ #[cfg(feature = "tokio")] use crate::extract::connect_info::IntoMakeServiceWithConnectInfo; use crate::{ - extract::{FromRequest, FromRequestParts}, + extract::{FromRequest, FromRequestParts, Request}, response::{IntoResponse, Response}, routing::IntoMakeService, }; -use axum_core::body::Body; -use http::Request; use std::{convert::Infallible, fmt, future::Future, marker::PhantomData, pin::Pin}; use tower::ServiceExt; use tower_layer::Layer; @@ -68,9 +66,8 @@ pub use self::service::HandlerService; /// ``` /// use tower::Service; /// use axum::{ -/// extract::State, +/// extract::{State, Request}, /// body::Body, -/// http::Request, /// handler::{HandlerWithoutStateExt, Handler}, /// }; /// @@ -89,7 +86,7 @@ pub use self::service::HandlerService; /// // helper to check that a value implements `Service` /// fn assert_service(service: S) /// where -/// S: Service>, +/// S: Service, /// {} /// ``` #[doc = include_str!("../docs/debugging_handler_type_errors.md")] @@ -104,7 +101,7 @@ pub trait Handler: Clone + Send + Sized + 'static { type Future: Future + Send + 'static; /// Call the handler with the given request. - fn call(self, req: Request, state: S) -> Self::Future; + fn call(self, req: Request, state: S) -> Self::Future; /// Apply a [`tower::Layer`] to the handler. /// @@ -145,7 +142,7 @@ pub trait Handler: Clone + Send + Sized + 'static { fn layer(self, layer: L) -> Layered where L: Layer> + Clone, - L::Service: Service>, + L::Service: Service, { Layered { layer, @@ -168,7 +165,7 @@ where { type Future = Pin + Send>>; - fn call(self, _req: Request, _state: S) -> Self::Future { + fn call(self, _req: Request, _state: S) -> Self::Future { Box::pin(async move { self().await.into_response() }) } } @@ -189,7 +186,7 @@ macro_rules! impl_handler { { type Future = Pin + Send>>; - fn call(self, req: Request, state: S) -> Self::Future { + fn call(self, req: Request, state: S) -> Self::Future { Box::pin(async move { let (mut parts, body) = req.into_parts(); let state = &state; @@ -257,15 +254,15 @@ impl Handler for Layered where L: Layer> + Clone + Send + 'static, H: Handler, - L::Service: Service, Error = Infallible> + Clone + Send + 'static, - >>::Response: IntoResponse, - >>::Future: Send, + L::Service: Service + Clone + Send + 'static, + >::Response: IntoResponse, + >::Future: Send, T: 'static, S: 'static, { type Future = future::LayeredFuture; - fn call(self, req: Request, state: S) -> Self::Future { + fn call(self, req: Request, state: S) -> Self::Future { use futures_util::future::{FutureExt, Map}; let svc = self.handler.with_state(state); @@ -275,8 +272,8 @@ where _, fn( Result< - >>::Response, - >>::Error, + >::Response, + >::Error, >, ) -> _, > = svc.oneshot(req).map(|result| match result { @@ -339,7 +336,8 @@ where #[cfg(test)] mod tests { use super::*; - use crate::{body, extract::State, test_helpers::*}; + use crate::{extract::State, test_helpers::*}; + use axum_core::body::Body; use http::StatusCode; use std::time::Duration; use tower_http::{ @@ -371,10 +369,10 @@ mod tests { .layer(( RequestBodyLimitLayer::new(1024), TimeoutLayer::new(Duration::from_secs(10)), - MapResponseBodyLayer::new(body::boxed), + MapResponseBodyLayer::new(Body::new), CompressionLayer::new(), )) - .layer(MapRequestBodyLayer::new(body::boxed)) + .layer(MapRequestBodyLayer::new(Body::new)) .with_state("foo"); let client = TestClient::new(svc); diff --git a/axum/src/json.rs b/axum/src/json.rs index 336f7d6fea..2cab54e91c 100644 --- a/axum/src/json.rs +++ b/axum/src/json.rs @@ -1,13 +1,11 @@ +use crate::extract::Request; use crate::extract::{rejection::*, FromRequest}; use async_trait::async_trait; -use axum_core::{ - body::Body, - response::{IntoResponse, Response}, -}; +use axum_core::response::{IntoResponse, Response}; use bytes::{BufMut, Bytes, BytesMut}; use http::{ header::{self, HeaderMap, HeaderValue}, - Request, StatusCode, + StatusCode, }; use serde::{de::DeserializeOwned, Serialize}; @@ -106,7 +104,7 @@ where { type Rejection = JsonRejection; - async fn from_request(req: Request, state: &S) -> Result { + async fn from_request(req: Request, state: &S) -> Result { if json_content_type(req.headers()) { let bytes = Bytes::from_request(req, state).await?; let deserializer = &mut serde_json::Deserializer::from_slice(&bytes); diff --git a/axum/src/middleware/from_fn.rs b/axum/src/middleware/from_fn.rs index ba595f338f..c2cf5fe659 100644 --- a/axum/src/middleware/from_fn.rs +++ b/axum/src/middleware/from_fn.rs @@ -1,9 +1,6 @@ -use crate::body::{Body, Bytes, HttpBody}; use crate::response::{IntoResponse, Response}; -use crate::BoxError; -use axum_core::extract::{FromRequest, FromRequestParts}; +use axum_core::extract::{FromRequest, FromRequestParts, Request}; use futures_util::future::BoxFuture; -use http::Request; use std::{ any::type_name, convert::Infallible, @@ -23,7 +20,7 @@ use tower_service::Service; /// /// 1. Be an `async fn`. /// 2. Take one or more [extractors] as the first arguments. -/// 3. Take [`Next`](Next) as the final argument. +/// 3. Take [`Next`](Next) as the final argument. /// 4. Return something that implements [`IntoResponse`]. /// /// Note that this function doesn't support extracting [`State`]. For that, use [`from_fn_with_state`]. @@ -33,15 +30,16 @@ use tower_service::Service; /// ```rust /// use axum::{ /// Router, -/// http::{self, Request}, +/// http, /// routing::get, /// response::Response, /// middleware::{self, Next}, +/// extract::Request, /// }; /// -/// async fn my_middleware( -/// request: Request, -/// next: Next, +/// async fn my_middleware( +/// request: Request, +/// next: Next, /// ) -> Response { /// // do something with `request`... /// @@ -63,23 +61,22 @@ use tower_service::Service; /// ```rust /// use axum::{ /// Router, -/// extract::TypedHeader, +/// extract::{Request, TypedHeader}, /// http::StatusCode, /// headers::authorization::{Authorization, Bearer}, -/// http::Request, /// middleware::{self, Next}, /// response::Response, /// routing::get, /// }; /// -/// async fn auth( +/// async fn auth( /// // run the `TypedHeader` extractor /// TypedHeader(auth): TypedHeader>, /// // you can also add more extractors here but the last /// // extractor must implement `FromRequest` which /// // `Request` does -/// request: Request, -/// next: Next, +/// request: Request, +/// next: Next, /// ) -> Result { /// if token_is_valid(auth.token()) { /// let response = next.run(request).await; @@ -115,23 +112,23 @@ pub fn from_fn(f: F) -> FromFnLayer { /// ```rust /// use axum::{ /// Router, -/// http::{Request, StatusCode}, +/// http::StatusCode, /// routing::get, /// response::{IntoResponse, Response}, /// middleware::{self, Next}, -/// extract::State, +/// extract::{Request, State}, /// }; /// /// #[derive(Clone)] /// struct AppState { /* ... */ } /// -/// async fn my_middleware( +/// async fn my_middleware( /// State(state): State, /// // you can add more extractors here but the last /// // extractor must implement `FromRequest` which /// // `Request` does -/// request: Request, -/// next: Next, +/// request: Request, +/// next: Next, /// ) -> Response { /// // do something with `request`... /// @@ -245,21 +242,19 @@ macro_rules! impl_service { [$($ty:ident),*], $last:ident ) => { #[allow(non_snake_case, unused_mut)] - impl Service> for FromFn + impl Service for FromFn where - F: FnMut($($ty,)* $last, Next) -> Fut + Clone + Send + 'static, + F: FnMut($($ty,)* $last, Next) -> Fut + Clone + Send + 'static, $( $ty: FromRequestParts + Send, )* $last: FromRequest + Send, Fut: Future + Send + 'static, Out: IntoResponse + 'static, - I: Service, Error = Infallible> + I: Service + Clone + Send + 'static, I::Response: IntoResponse, I::Future: Send + 'static, - B: HttpBody + Send + 'static, - B::Error: Into, S: Clone + Send + Sync + 'static, { type Response = Response; @@ -270,9 +265,7 @@ macro_rules! impl_service { self.inner.poll_ready(cx) } - fn call(&mut self, req: Request) -> Self::Future { - let req = req.map(Body::new); - + fn call(&mut self, req: Request) -> Self::Future { let not_ready_inner = self.inner.clone(); let ready_inner = std::mem::replace(&mut self.inner, not_ready_inner); @@ -330,13 +323,14 @@ where } /// The remainder of a middleware stack, including the handler. -pub struct Next { - inner: BoxCloneService, Response, Infallible>, +#[derive(Debug, Clone)] +pub struct Next { + inner: BoxCloneService, } -impl Next { +impl Next { /// Execute the remaining middleware stack. - pub async fn run(mut self, req: Request) -> Response { + pub async fn run(mut self, req: Request) -> Response { match self.inner.call(req).await { Ok(res) => res, Err(err) => match err {}, @@ -344,23 +338,7 @@ impl Next { } } -impl fmt::Debug for Next { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("FromFnLayer") - .field("inner", &self.inner) - .finish() - } -} - -impl Clone for Next { - fn clone(&self) -> Self { - Self { - inner: self.inner.clone(), - } - } -} - -impl Service> for Next { +impl Service for Next { type Response = Response; type Error = Infallible; type Future = Pin> + Send>>; @@ -369,7 +347,7 @@ impl Service> for Next { self.inner.poll_ready(cx) } - fn call(&mut self, req: Request) -> Self::Future { + fn call(&mut self, req: Request) -> Self::Future { self.inner.call(req) } } @@ -402,7 +380,7 @@ mod tests { #[crate::test] async fn basic() { - async fn insert_header(mut req: Request, next: Next) -> impl IntoResponse { + async fn insert_header(mut req: Request, next: Next) -> impl IntoResponse { req.headers_mut() .insert("x-axum-test", "ok".parse().unwrap()); diff --git a/axum/src/response/mod.rs b/axum/src/response/mod.rs index 95bcb762e2..919ca78b1c 100644 --- a/axum/src/response/mod.rs +++ b/axum/src/response/mod.rs @@ -1,6 +1,6 @@ #![doc = include_str!("../docs/response.md")] -use crate::body::{Bytes, Full}; +use axum_core::body::Body; use http::{header, HeaderValue}; mod redirect; @@ -44,7 +44,7 @@ pub struct Html(pub T); impl IntoResponse for Html where - T: Into>, + T: Into, { fn into_response(self) -> Response { ( diff --git a/axum/src/response/sse.rs b/axum/src/response/sse.rs index 1b205a93e0..3f849b9278 100644 --- a/axum/src/response/sse.rs +++ b/axum/src/response/sse.rs @@ -32,7 +32,7 @@ use crate::{ BoxError, }; use axum_core::{ - body, + body::Body, response::{IntoResponse, Response}, }; use bytes::{BufMut, BytesMut}; @@ -104,7 +104,7 @@ where (http::header::CONTENT_TYPE, mime::TEXT_EVENT_STREAM.as_ref()), (http::header::CACHE_CONTROL, "no-cache"), ], - body::boxed(Body { + Body::new(SseBody { event_stream: SyncWrapper::new(self.stream), keep_alive: self.keep_alive.map(KeepAliveStream::new), }), @@ -114,7 +114,7 @@ where } pin_project! { - struct Body { + struct SseBody { #[pin] event_stream: SyncWrapper, #[pin] @@ -122,7 +122,7 @@ pin_project! { } } -impl HttpBody for Body +impl HttpBody for SseBody where S: Stream>, { diff --git a/axum/src/routing/into_make_service.rs b/axum/src/routing/into_make_service.rs index fbc57c4acc..36da73a21e 100644 --- a/axum/src/routing/into_make_service.rs +++ b/axum/src/routing/into_make_service.rs @@ -46,12 +46,11 @@ opaque_future! { #[cfg(test)] mod tests { use super::*; - use crate::body::Body; #[test] fn traits() { use crate::test_helpers::*; - assert_send::>(); + assert_send::>(); } } diff --git a/axum/src/routing/method_routing.rs b/axum/src/routing/method_routing.rs index 7d982f4232..9040ee30a5 100644 --- a/axum/src/routing/method_routing.rs +++ b/axum/src/routing/method_routing.rs @@ -8,11 +8,11 @@ use crate::{ boxed::BoxedIntoRoute, error_handling::{HandleError, HandleErrorLayer}, handler::Handler, - http::{Method, Request, StatusCode}, + http::{Method, StatusCode}, response::Response, routing::{future::RouteFuture, Fallback, MethodFilter, Route}, }; -use axum_core::{response::IntoResponse, BoxError}; +use axum_core::{extract::Request, response::IntoResponse, BoxError}; use bytes::BytesMut; use std::{ convert::Infallible, @@ -34,7 +34,7 @@ macro_rules! top_level_service_fn { /// /// ```rust /// use axum::{ - /// http::Request, + /// extract::Request, /// Router, /// routing::get_service, /// body::Body, @@ -42,7 +42,7 @@ macro_rules! top_level_service_fn { /// use http::Response; /// use std::convert::Infallible; /// - /// let service = tower::service_fn(|request: Request| async { + /// let service = tower::service_fn(|request: Request| async { /// Ok::<_, Infallible>(Response::new(Body::empty())) /// }); /// @@ -80,7 +80,7 @@ macro_rules! top_level_service_fn { $(#[$m])+ pub fn $name(svc: T) -> MethodRouter where - T: Service> + Clone + Send + 'static, + T: Service + Clone + Send + 'static, T::Response: IntoResponse + 'static, T::Future: Send + 'static, S: Clone, @@ -161,7 +161,7 @@ macro_rules! chained_service_fn { /// /// ```rust /// use axum::{ - /// http::Request, + /// extract::Request, /// Router, /// routing::post_service, /// body::Body, @@ -169,11 +169,11 @@ macro_rules! chained_service_fn { /// use http::Response; /// use std::convert::Infallible; /// - /// let service = tower::service_fn(|request: Request| async { + /// let service = tower::service_fn(|request: Request| async { /// Ok::<_, Infallible>(Response::new(Body::empty())) /// }); /// - /// let other_service = tower::service_fn(|request: Request| async { + /// let other_service = tower::service_fn(|request: Request| async { /// Ok::<_, Infallible>(Response::new(Body::empty())) /// }); /// @@ -213,7 +213,7 @@ macro_rules! chained_service_fn { #[track_caller] pub fn $name(self, svc: T) -> Self where - T: Service, Error = E> + T: Service + Clone + Send + 'static, @@ -301,7 +301,7 @@ top_level_service_fn!(trace_service, TRACE); /// /// ```rust /// use axum::{ -/// http::Request, +/// extract::Request, /// routing::on, /// Router, /// body::Body, @@ -310,7 +310,7 @@ top_level_service_fn!(trace_service, TRACE); /// use http::Response; /// use std::convert::Infallible; /// -/// let service = tower::service_fn(|request: Request| async { +/// let service = tower::service_fn(|request: Request| async { /// Ok::<_, Infallible>(Response::new(Body::empty())) /// }); /// @@ -322,7 +322,7 @@ top_level_service_fn!(trace_service, TRACE); /// ``` pub fn on_service(filter: MethodFilter, svc: T) -> MethodRouter where - T: Service> + Clone + Send + 'static, + T: Service + Clone + Send + 'static, T::Response: IntoResponse + 'static, T::Future: Send + 'static, S: Clone, @@ -336,7 +336,7 @@ where /// /// ```rust /// use axum::{ -/// http::Request, +/// extract::Request, /// Router, /// routing::any_service, /// body::Body, @@ -344,7 +344,7 @@ where /// use http::Response; /// use std::convert::Infallible; /// -/// let service = tower::service_fn(|request: Request| async { +/// let service = tower::service_fn(|request: Request| async { /// Ok::<_, Infallible>(Response::new(Body::empty())) /// }); /// @@ -359,7 +359,7 @@ where /// /// ```rust /// use axum::{ -/// http::Request, +/// extract::Request, /// Router, /// routing::any_service, /// body::Body, @@ -367,12 +367,12 @@ where /// use http::Response; /// use std::convert::Infallible; /// -/// let service = tower::service_fn(|request: Request| async { +/// let service = tower::service_fn(|request: Request| async { /// # Ok::<_, Infallible>(Response::new(Body::empty())) /// // ... /// }); /// -/// let other_service = tower::service_fn(|request: Request| async { +/// let other_service = tower::service_fn(|request: Request| async { /// # Ok::<_, Infallible>(Response::new(Body::empty())) /// // ... /// }); @@ -385,7 +385,7 @@ where /// ``` pub fn any_service(svc: T) -> MethodRouter where - T: Service> + Clone + Send + 'static, + T: Service + Clone + Send + 'static, T::Response: IntoResponse + 'static, T::Future: Send + 'static, S: Clone, @@ -487,7 +487,7 @@ where /// /// ``` /// use tower::Service; -/// use axum::{routing::get, extract::State, body::Body, http::Request}; +/// use axum::{routing::get, extract::{State, Request}, body::Body}; /// /// // this `MethodRouter` doesn't require any state, i.e. the state is `()`, /// let method_router = get(|| async {}); @@ -504,7 +504,7 @@ where /// // helper to check that a value implements `Service` /// fn assert_service(service: S) /// where -/// S: Service>, +/// S: Service, /// {} /// ``` #[must_use] @@ -703,7 +703,7 @@ where /// Create a default `MethodRouter` that will respond with `405 Method Not Allowed` to all /// requests. pub fn new() -> Self { - let fallback = Route::new(service_fn(|_: Request| async { + let fallback = Route::new(service_fn(|_: Request| async { Ok(StatusCode::METHOD_NOT_ALLOWED.into_response()) })); @@ -744,7 +744,7 @@ where /// /// ```rust /// use axum::{ - /// http::Request, + /// extract::Request, /// Router, /// routing::{MethodFilter, on_service}, /// body::Body, @@ -752,7 +752,7 @@ where /// use http::Response; /// use std::convert::Infallible; /// - /// let service = tower::service_fn(|request: Request| async { + /// let service = tower::service_fn(|request: Request| async { /// Ok::<_, Infallible>(Response::new(Body::empty())) /// }); /// @@ -765,7 +765,7 @@ where #[track_caller] pub fn on_service(self, filter: MethodFilter, svc: T) -> Self where - T: Service, Error = E> + Clone + Send + 'static, + T: Service + Clone + Send + 'static, T::Response: IntoResponse + 'static, T::Future: Send + 'static, { @@ -897,7 +897,7 @@ where #[doc = include_str!("../docs/method_routing/fallback.md")] pub fn fallback_service(mut self, svc: T) -> Self where - T: Service, Error = E> + Clone + Send + 'static, + T: Service + Clone + Send + 'static, T::Response: IntoResponse + 'static, T::Future: Send + 'static, { @@ -909,10 +909,10 @@ where pub fn layer(self, layer: L) -> MethodRouter where L: Layer> + Clone + Send + 'static, - L::Service: Service> + Clone + Send + 'static, - >>::Response: IntoResponse + 'static, - >>::Error: Into + 'static, - >>::Future: Send + 'static, + L::Service: Service + Clone + Send + 'static, + >::Response: IntoResponse + 'static, + >::Error: Into + 'static, + >::Future: Send + 'static, E: 'static, S: 'static, NewError: 'static, @@ -938,9 +938,9 @@ where pub fn route_layer(mut self, layer: L) -> MethodRouter where L: Layer> + Clone + Send + 'static, - L::Service: Service, Error = E> + Clone + Send + 'static, - >>::Response: IntoResponse + 'static, - >>::Future: Send + 'static, + L::Service: Service + Clone + Send + 'static, + >::Response: IntoResponse + 'static, + >::Future: Send + 'static, E: 'static, S: 'static, { @@ -1036,9 +1036,9 @@ where pub fn handle_error(self, f: F) -> MethodRouter where F: Clone + Send + Sync + 'static, - HandleError, F, T>: Service, Error = Infallible>, - , F, T> as Service>>::Future: Send, - , F, T> as Service>>::Response: IntoResponse + Send, + HandleError, F, T>: Service, + , F, T> as Service>::Future: Send, + , F, T> as Service>::Response: IntoResponse + Send, T: 'static, E: 'static, S: 'static, @@ -1051,7 +1051,7 @@ where self } - pub(crate) fn call_with_state(&mut self, req: Request, state: S) -> RouteFuture { + pub(crate) fn call_with_state(&mut self, req: Request, state: S) -> RouteFuture { macro_rules! call { ( $req:expr, @@ -1256,7 +1256,7 @@ where { type Future = InfallibleRouteFuture; - fn call(mut self, req: Request, state: S) -> Self::Future { + fn call(mut self, req: Request, state: S) -> Self::Future { InfallibleRouteFuture::new(self.call_with_state(req, state)) } } @@ -1284,7 +1284,7 @@ mod tests { #[crate::test] async fn get_service_fn() { - async fn handle(_req: Request) -> Result, Infallible> { + async fn handle(_req: Request) -> Result, Infallible> { Ok(Response::new(Body::from("ok"))) } @@ -1545,7 +1545,7 @@ mod tests { async fn call(method: Method, svc: &mut S) -> (StatusCode, HeaderMap, String) where - S: Service, Error = Infallible>, + S: Service, S::Response: IntoResponse, { let request = Request::builder() diff --git a/axum/src/routing/mod.rs b/axum/src/routing/mod.rs index d6a95c5054..74660fe696 100644 --- a/axum/src/routing/mod.rs +++ b/axum/src/routing/mod.rs @@ -9,8 +9,10 @@ use crate::{ handler::Handler, util::try_downcast, }; -use axum_core::response::{IntoResponse, Response}; -use http::Request; +use axum_core::{ + extract::Request, + response::{IntoResponse, Response}, +}; use std::{ convert::Infallible, fmt, @@ -124,7 +126,7 @@ where #[doc = include_str!("../docs/routing/route_service.md")] pub fn route_service(mut self, path: &str, service: T) -> Self where - T: Service, Error = Infallible> + Clone + Send + 'static, + T: Service + Clone + Send + 'static, T::Response: IntoResponse, T::Future: Send + 'static, { @@ -164,7 +166,7 @@ where #[track_caller] pub fn nest_service(mut self, path: &str, service: T) -> Self where - T: Service, Error = Infallible> + Clone + Send + 'static, + T: Service + Clone + Send + 'static, T::Response: IntoResponse, T::Future: Send + 'static, { @@ -213,10 +215,10 @@ where pub fn layer(self, layer: L) -> Router where L: Layer + Clone + Send + 'static, - L::Service: Service> + Clone + Send + 'static, - >>::Response: IntoResponse + 'static, - >>::Error: Into + 'static, - >>::Future: Send + 'static, + L::Service: Service + Clone + Send + 'static, + >::Response: IntoResponse + 'static, + >::Error: Into + 'static, + >::Future: Send + 'static, { Router { path_router: self.path_router.layer(layer.clone()), @@ -230,10 +232,10 @@ where pub fn route_layer(self, layer: L) -> Self where L: Layer + Clone + Send + 'static, - L::Service: Service> + Clone + Send + 'static, - >>::Response: IntoResponse + 'static, - >>::Error: Into + 'static, - >>::Future: Send + 'static, + L::Service: Service + Clone + Send + 'static, + >::Response: IntoResponse + 'static, + >::Error: Into + 'static, + >::Future: Send + 'static, { Router { path_router: self.path_router.route_layer(layer), @@ -258,7 +260,7 @@ where /// See [`Router::fallback`] for more details. pub fn fallback_service(self, service: T) -> Self where - T: Service, Error = Infallible> + Clone + Send + 'static, + T: Service + Clone + Send + 'static, T::Response: IntoResponse, T::Future: Send + 'static, { @@ -284,7 +286,7 @@ where pub(crate) fn call_with_state( &mut self, - mut req: Request, + mut req: Request, state: S, ) -> RouteFuture { // required for opaque routers to still inherit the fallback @@ -607,10 +609,10 @@ where fn layer(self, layer: L) -> Endpoint where L: Layer + Clone + Send + 'static, - L::Service: Service> + Clone + Send + 'static, - >>::Response: IntoResponse + 'static, - >>::Error: Into + 'static, - >>::Future: Send + 'static, + L::Service: Service + Clone + Send + 'static, + >::Response: IntoResponse + 'static, + >::Error: Into + 'static, + >::Future: Send + 'static, { match self { Endpoint::MethodRouter(method_router) => { diff --git a/axum/src/routing/route.rs b/axum/src/routing/route.rs index 335c87df9e..0a2713a8d2 100644 --- a/axum/src/routing/route.rs +++ b/axum/src/routing/route.rs @@ -1,12 +1,12 @@ use crate::{ - body::{boxed, Body, Empty, HttpBody}, + body::{Body, HttpBody}, response::Response, }; -use axum_core::response::IntoResponse; +use axum_core::{extract::Request, response::IntoResponse}; use bytes::Bytes; use http::{ header::{self, CONTENT_LENGTH}, - HeaderMap, HeaderValue, Request, + HeaderMap, HeaderValue, }; use pin_project_lite::pin_project; use std::{ @@ -27,12 +27,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(BoxCloneService, Response, E>); +pub struct Route(BoxCloneService); impl Route { pub(crate) fn new(svc: T) -> Self where - T: Service, Error = E> + Clone + Send + 'static, + T: Service + Clone + Send + 'static, T::Response: IntoResponse + 'static, T::Future: Send + 'static, { @@ -43,18 +43,18 @@ impl Route { pub(crate) fn oneshot_inner( &mut self, - req: Request, - ) -> Oneshot, Response, E>, Request> { + req: Request, + ) -> Oneshot, Request> { self.0.clone().oneshot(req) } pub(crate) fn layer(self, layer: L) -> Route where L: Layer> + Clone + Send + 'static, - L::Service: Service> + Clone + Send + 'static, - >>::Response: IntoResponse + 'static, - >>::Error: Into + 'static, - >>::Future: Send + 'static, + L::Service: Service + Clone + Send + 'static, + >::Response: IntoResponse + 'static, + >::Error: Into + 'static, + >::Future: Send + 'static, NewError: 'static, { let layer = ServiceBuilder::new() @@ -117,8 +117,8 @@ pin_project! { Future { #[pin] future: Oneshot< - BoxCloneService, Response, E>, - Request, + BoxCloneService, + Request, >, }, Response { @@ -129,7 +129,7 @@ pin_project! { impl RouteFuture { pub(crate) fn from_future( - future: Oneshot, Response, E>, Request>, + future: Oneshot, Request>, ) -> Self { Self { kind: RouteFutureKind::Future { future }, @@ -173,7 +173,7 @@ impl Future for RouteFuture { set_content_length(res.size_hint(), res.headers_mut()); let res = if *this.strip_body { - res.map(|_| boxed(Empty::new())) + res.map(|_| Body::empty()) } else { res }; diff --git a/axum/src/routing/tests/get_to_head.rs b/axum/src/routing/tests/get_to_head.rs index b46114c60e..cedf84918e 100644 --- a/axum/src/routing/tests/get_to_head.rs +++ b/axum/src/routing/tests/get_to_head.rs @@ -45,7 +45,7 @@ mod for_services { async fn get_handles_head() { let app = Router::new().route( "/", - get_service(service_fn(|_req: Request| async move { + get_service(service_fn(|_req: Request| async move { Ok::<_, Infallible>( ([("x-some-header", "foobar")], "you shouldn't see this").into_response(), ) diff --git a/axum/src/routing/tests/merge.rs b/axum/src/routing/tests/merge.rs index 0344a87939..31b42d8336 100644 --- a/axum/src/routing/tests/merge.rs +++ b/axum/src/routing/tests/merge.rs @@ -195,13 +195,13 @@ async fn services() { let app = Router::new() .route( "/foo", - get_service(service_fn(|_: Request| async { + get_service(service_fn(|_: Request| async { Ok::<_, Infallible>(Response::new(Body::empty())) })), ) .merge(Router::new().route( "/bar", - get_service(service_fn(|_: Request| async { + get_service(service_fn(|_: Request| async { Ok::<_, Infallible>(Response::new(Body::empty())) })), )); @@ -218,7 +218,7 @@ async fn services() { async fn all_the_uris( uri: Uri, OriginalUri(original_uri): OriginalUri, - req: Request, + req: Request, ) -> impl IntoResponse { Json(json!({ "uri": uri.to_string(), diff --git a/axum/src/routing/tests/mod.rs b/axum/src/routing/tests/mod.rs index 8c2d6ccb9b..c3f7875dd4 100644 --- a/axum/src/routing/tests/mod.rs +++ b/axum/src/routing/tests/mod.rs @@ -1,22 +1,16 @@ use crate::{ - body::{Body, Bytes, Empty}, + body::{Body, Bytes}, error_handling::HandleErrorLayer, extract::{self, DefaultBodyLimit, FromRef, Path, State}, handler::{Handler, HandlerWithoutStateExt}, - response::IntoResponse, - routing::{ - delete, get, get_service, on, on_service, patch, patch_service, - path_router::path_for_nested_route, post, MethodFilter, - }, - test_helpers::{ - tracing_helpers::{capture_tracing, TracingEvent}, - *, - }, - BoxError, Extension, Json, Router, + response::{IntoResponse, Response}, + routing::{delete, get, get_service, on, on_service, patch, patch_service, post, MethodFilter, path_router::path_for_nested_route}, + test_helpers::{*, tracing_helpers::{capture_tracing, TracingEvent}}, + BoxError, Json, Router, Extension, }; +use axum_core::extract::Request; use futures_util::stream::StreamExt; -use http::{header::ALLOW, header::CONTENT_LENGTH, HeaderMap, Request, Response, StatusCode, Uri}; -use serde::Deserialize; +use http::{header::ALLOW, header::CONTENT_LENGTH, HeaderMap, StatusCode, Uri}; use serde_json::json; use std::{ convert::Infallible, @@ -28,6 +22,7 @@ use std::{ use tower::{service_fn, timeout::TimeoutLayer, util::MapResponseLayer, ServiceBuilder}; use tower_http::{limit::RequestBodyLimitLayer, validate_request::ValidateRequestHeaderLayer}; use tower_service::Service; +use serde::Deserialize; mod fallback; mod get_to_head; @@ -37,15 +32,15 @@ mod nest; #[crate::test] async fn hello_world() { - async fn root(_: Request) -> &'static str { + async fn root(_: Request) -> &'static str { "Hello, World!" } - async fn foo(_: Request) -> &'static str { + async fn foo(_: Request) -> &'static str { "foo" } - async fn users_create(_: Request) -> &'static str { + async fn users_create(_: Request) -> &'static str { "users#create" } @@ -73,13 +68,12 @@ async fn routing() { let app = Router::new() .route( "/users", - get(|_: Request| async { "users#index" }) - .post(|_: Request| async { "users#create" }), + get(|_: Request| async { "users#index" }).post(|_: Request| async { "users#create" }), ) - .route("/users/:id", get(|_: Request| async { "users#show" })) + .route("/users/:id", get(|_: Request| async { "users#show" })) .route( "/users/:id/action", - get(|_: Request| async { "users#action" }), + get(|_: Request| async { "users#action" }), ); let client = TestClient::new(app); @@ -109,12 +103,8 @@ async fn router_type_doesnt_change() { let app: Router = Router::new() .route( "/", - on(MethodFilter::GET, |_: Request| async { - "hi from GET" - }) - .on(MethodFilter::POST, |_: Request| async { - "hi from POST" - }), + on(MethodFilter::GET, |_: Request| async { "hi from GET" }) + .on(MethodFilter::POST, |_: Request| async { "hi from POST" }), ) .layer(tower_http::compression::CompressionLayer::new()); @@ -134,22 +124,22 @@ async fn routing_between_services() { use std::convert::Infallible; use tower::service_fn; - async fn handle(_: Request) -> &'static str { + async fn handle(_: Request) -> &'static str { "handler" } let app = Router::new() .route( "/one", - get_service(service_fn(|_: Request| async { + get_service(service_fn(|_: Request| async { Ok::<_, Infallible>(Response::new(Body::from("one get"))) })) - .post_service(service_fn(|_: Request| async { + .post_service(service_fn(|_: Request| async { Ok::<_, Infallible>(Response::new(Body::from("one post"))) })) .on_service( MethodFilter::PUT, - service_fn(|_: Request| async { + service_fn(|_: Request| async { Ok::<_, Infallible>(Response::new(Body::from("one put"))) }), ), @@ -180,7 +170,7 @@ async fn middleware_on_single_route() { use tower::ServiceBuilder; use tower_http::{compression::CompressionLayer, trace::TraceLayer}; - async fn handle(_: Request) -> &'static str { + async fn handle(_: Request) -> &'static str { "Hello, World!" } @@ -204,7 +194,7 @@ async fn middleware_on_single_route() { #[crate::test] async fn service_in_bottom() { - async fn handler(_req: Request) -> Result, Infallible> { + async fn handler(_req: Request) -> Result, Infallible> { Ok(Response::new(Body::empty())) } @@ -242,7 +232,7 @@ async fn wrong_method_service() { struct Svc; impl Service for Svc { - type Response = Response>; + type Response = Response; type Error = Infallible; type Future = Ready>; @@ -251,7 +241,7 @@ async fn wrong_method_service() { } fn call(&mut self, _req: R) -> Self::Future { - ready(Ok(Response::new(Empty::new()))) + ready(Ok(().into_response())) } } @@ -278,7 +268,7 @@ async fn wrong_method_service() { #[crate::test] async fn multiple_methods_for_one_handler() { - async fn root(_: Request) -> &'static str { + async fn root(_: Request) -> &'static str { "Hello, World!" } diff --git a/axum/src/routing/tests/nest.rs b/axum/src/routing/tests/nest.rs index 2e699a8b28..a9119eb3cf 100644 --- a/axum/src/routing/tests/nest.rs +++ b/axum/src/routing/tests/nest.rs @@ -1,5 +1,5 @@ use super::*; -use crate::{body::boxed, extract::Extension}; +use crate::extract::Extension; use std::collections::HashMap; use tower_http::services::ServeDir; @@ -141,7 +141,7 @@ async fn nested_url_extractor() { .route("/baz", get(|uri: Uri| async move { uri.to_string() })) .route( "/qux", - get(|req: Request| async move { req.uri().to_string() }), + get(|req: Request| async move { req.uri().to_string() }), ), ), ); @@ -185,8 +185,8 @@ async fn nested_service_sees_stripped_uri() { "/bar", Router::new().route_service( "/baz", - service_fn(|req: Request| async move { - let body = boxed(Body::from(req.uri().to_string())); + service_fn(|req: Request| async move { + let body = Body::from(req.uri().to_string()); Ok::<_, Infallible>(Response::new(body)) }), ), diff --git a/axum/src/test_helpers/test_client.rs b/axum/src/test_helpers/test_client.rs index d1d73f6c1d..fa052a004d 100644 --- a/axum/src/test_helpers/test_client.rs +++ b/axum/src/test_helpers/test_client.rs @@ -4,7 +4,7 @@ use http::{ header::{HeaderName, HeaderValue}, Request, StatusCode, }; -use hyper::{Body, Server}; +use hyper::Server; use std::net::{SocketAddr, TcpListener}; use tower::make::Shared; use tower_service::Service; @@ -17,7 +17,10 @@ pub(crate) struct TestClient { impl TestClient { pub(crate) fn new(svc: S) -> Self where - S: Service, Response = http::Response> + Clone + Send + 'static, + S: Service, Response = http::Response> + + Clone + + Send + + 'static, ResBody: HttpBody + Send + 'static, ResBody::Data: Send, ResBody::Error: Into, diff --git a/examples/consume-body-in-extractor-or-middleware/src/main.rs b/examples/consume-body-in-extractor-or-middleware/src/main.rs index 25819a2278..f1b2cbf754 100644 --- a/examples/consume-body-in-extractor-or-middleware/src/main.rs +++ b/examples/consume-body-in-extractor-or-middleware/src/main.rs @@ -7,8 +7,8 @@ use axum::{ async_trait, body::{Body, Bytes}, - extract::FromRequest, - http::{Request, StatusCode}, + extract::{FromRequest, Request}, + http::StatusCode, middleware::{self, Next}, response::{IntoResponse, Response}, routing::post, @@ -41,10 +41,7 @@ async fn main() { } // middleware that shows how to consume the request body upfront -async fn print_request_body( - request: Request, - next: Next, -) -> Result { +async fn print_request_body(request: Request, next: Next) -> Result { let request = buffer_request_body(request).await?; Ok(next.run(request).await) @@ -52,7 +49,7 @@ async fn print_request_body( // the trick is to take the request apart, buffer the body, do what you need to do, then put // the request back together -async fn buffer_request_body(request: Request) -> Result, Response> { +async fn buffer_request_body(request: Request) -> Result { let (parts, body) = request.into_parts(); // this wont work if the body is an long running stream @@ -84,7 +81,7 @@ where { type Rejection = Response; - async fn from_request(req: Request, state: &S) -> Result { + async fn from_request(req: Request, state: &S) -> Result { let body = Bytes::from_request(req, state) .await .map_err(|err| err.into_response())?; diff --git a/examples/customize-extractor-error/src/custom_extractor.rs b/examples/customize-extractor-error/src/custom_extractor.rs index 6a39ffbd22..3611fba796 100644 --- a/examples/customize-extractor-error/src/custom_extractor.rs +++ b/examples/customize-extractor-error/src/custom_extractor.rs @@ -6,9 +6,7 @@ //! - Complexity: Manually implementing `FromRequest` results on more complex code use axum::{ async_trait, - body::Body, - extract::{rejection::JsonRejection, FromRequest, MatchedPath}, - http::Request, + extract::{rejection::JsonRejection, FromRequest, MatchedPath, Request}, http::StatusCode, response::IntoResponse, RequestPartsExt, @@ -30,7 +28,7 @@ where { type Rejection = (StatusCode, axum::Json); - async fn from_request(req: Request, state: &S) -> Result { + async fn from_request(req: Request, state: &S) -> Result { let (mut parts, body) = req.into_parts(); // We can use other extractors to provide better rejection messages. diff --git a/examples/http-proxy/src/main.rs b/examples/http-proxy/src/main.rs index 73abf78f18..08845ca9e0 100644 --- a/examples/http-proxy/src/main.rs +++ b/examples/http-proxy/src/main.rs @@ -13,8 +13,9 @@ //! Example is based on use axum::{ - body::{self, Body}, - http::{Method, Request, StatusCode}, + body::Body, + extract::Request, + http::{Method, StatusCode}, response::{IntoResponse, Response}, routing::get, Router, @@ -59,7 +60,7 @@ async fn main() { .unwrap(); } -async fn proxy(req: Request) -> Result { +async fn proxy(req: Request) -> Result { tracing::trace!(?req); if let Some(host_addr) = req.uri().authority().map(|auth| auth.to_string()) { @@ -74,7 +75,7 @@ async fn proxy(req: Request) -> Result { } }); - Ok(Response::new(body::boxed(body::Empty::new()))) + Ok(Response::new(Body::empty())) } else { tracing::warn!("CONNECT host is not socket addr: {:?}", req.uri()); Ok(( diff --git a/examples/low-level-rustls/src/main.rs b/examples/low-level-rustls/src/main.rs index afe6a037e9..1e9a951f49 100644 --- a/examples/low-level-rustls/src/main.rs +++ b/examples/low-level-rustls/src/main.rs @@ -4,7 +4,7 @@ //! cargo run -p example-low-level-rustls //! ``` -use axum::{body::Body, extract::ConnectInfo, http::Request, routing::get, Router}; +use axum::{extract::ConnectInfo, extract::Request, routing::get, Router}; use futures_util::future::poll_fn; use hyper::server::{ accept::Accept, @@ -67,7 +67,7 @@ async fn main() { let protocol = protocol.clone(); - let svc = MakeService::<_, Request>::make_service(&mut app, &stream); + let svc = MakeService::<_, Request>::make_service(&mut app, &stream); tokio::spawn(async move { if let Ok(stream) = acceptor.accept(stream).await { diff --git a/examples/parse-body-based-on-content-type/src/main.rs b/examples/parse-body-based-on-content-type/src/main.rs index d66791bcdd..9ee26c6a10 100644 --- a/examples/parse-body-based-on-content-type/src/main.rs +++ b/examples/parse-body-based-on-content-type/src/main.rs @@ -8,9 +8,8 @@ use axum::{ async_trait, - body::Body, - extract::FromRequest, - http::{header::CONTENT_TYPE, Request, StatusCode}, + extract::{FromRequest, Request}, + http::{header::CONTENT_TYPE, StatusCode}, response::{IntoResponse, Response}, routing::post, Form, Json, RequestExt, Router, @@ -61,7 +60,7 @@ where { type Rejection = Response; - async fn from_request(req: Request, _state: &S) -> Result { + async fn from_request(req: Request, _state: &S) -> Result { let content_type_header = req.headers().get(CONTENT_TYPE); let content_type = content_type_header.and_then(|value| value.to_str().ok()); diff --git a/examples/print-request-response/src/main.rs b/examples/print-request-response/src/main.rs index c071ff5495..4703c05845 100644 --- a/examples/print-request-response/src/main.rs +++ b/examples/print-request-response/src/main.rs @@ -6,7 +6,8 @@ use axum::{ body::{Body, Bytes}, - http::{Request, StatusCode}, + extract::Request, + http::StatusCode, middleware::{self, Next}, response::{IntoResponse, Response}, routing::post, @@ -38,8 +39,8 @@ async fn main() { } async fn print_request_response( - req: Request, - next: Next, + req: Request, + next: Next, ) -> Result { let (parts, body) = req.into_parts(); let bytes = buffer_and_print("request", body).await?; diff --git a/examples/prometheus-metrics/src/main.rs b/examples/prometheus-metrics/src/main.rs index cf7db5ac29..675310bede 100644 --- a/examples/prometheus-metrics/src/main.rs +++ b/examples/prometheus-metrics/src/main.rs @@ -8,8 +8,7 @@ //! ``` use axum::{ - extract::MatchedPath, - http::Request, + extract::{MatchedPath, Request}, middleware::{self, Next}, response::IntoResponse, routing::get, @@ -94,7 +93,7 @@ fn setup_metrics_recorder() -> PrometheusHandle { .unwrap() } -async fn track_metrics(req: Request, next: Next) -> impl IntoResponse { +async fn track_metrics(req: Request, next: Next) -> impl IntoResponse { let start = Instant::now(); let path = if let Some(matched_path) = req.extensions().get::() { matched_path.as_str().to_owned() diff --git a/examples/rest-grpc-multiplex/src/multiplex_service.rs b/examples/rest-grpc-multiplex/src/multiplex_service.rs index 0f08dbf63f..777c14cd19 100644 --- a/examples/rest-grpc-multiplex/src/multiplex_service.rs +++ b/examples/rest-grpc-multiplex/src/multiplex_service.rs @@ -1,6 +1,9 @@ -use axum::{body::BoxBody, http::header::CONTENT_TYPE, response::IntoResponse}; +use axum::{ + extract::Request, + http::header::CONTENT_TYPE, + response::{IntoResponse, Response}, +}; use futures::{future::BoxFuture, ready}; -use hyper::{Body, Request, Response}; use std::{ convert::Infallible, task::{Context, Poll}, @@ -41,16 +44,16 @@ where } } -impl Service> for MultiplexService +impl Service> for MultiplexService where - A: Service, Error = Infallible>, + A: Service, Error = Infallible>, A::Response: IntoResponse, A::Future: Send + 'static, - B: Service>, + B: Service>, B::Response: IntoResponse, B::Future: Send + 'static, { - type Response = Response; + type Response = Response; type Error = B::Error; type Future = BoxFuture<'static, Result>; @@ -73,7 +76,7 @@ where } } - fn call(&mut self, req: Request) -> Self::Future { + fn call(&mut self, req: Request) -> Self::Future { // require users to call `poll_ready` first, if they don't we're allowed to panic // as per the `tower::Service` contract assert!( diff --git a/examples/reverse-proxy/src/main.rs b/examples/reverse-proxy/src/main.rs index 634a6a0440..a01947c652 100644 --- a/examples/reverse-proxy/src/main.rs +++ b/examples/reverse-proxy/src/main.rs @@ -9,8 +9,8 @@ use axum::{ body::Body, - extract::State, - http::{uri::Uri, Request}, + extract::{Request, State}, + http::uri::Uri, response::{IntoResponse, Response}, routing::get, Router, @@ -36,7 +36,7 @@ async fn main() { .unwrap(); } -async fn handler(State(client): State, mut req: Request) -> Response { +async fn handler(State(client): State, mut req: Request) -> Response { let path = req.uri().path(); let path_query = req .uri() diff --git a/examples/static-file-server/src/main.rs b/examples/static-file-server/src/main.rs index 50857d9611..3a3a24149f 100644 --- a/examples/static-file-server/src/main.rs +++ b/examples/static-file-server/src/main.rs @@ -5,11 +5,7 @@ //! ``` use axum::{ - body::Body, - handler::HandlerWithoutStateExt, - http::{Request, StatusCode}, - routing::get, - Router, + extract::Request, handler::HandlerWithoutStateExt, http::StatusCode, routing::get, Router, }; use std::net::SocketAddr; use tower::ServiceExt; @@ -97,7 +93,7 @@ fn calling_serve_dir_from_a_handler() -> Router { // call `ServeDir` yourself from a handler Router::new().nest_service( "/foo", - get(|request: Request| async { + get(|request: Request| async { let service = ServeDir::new("assets"); let result = service.oneshot(request).await; result diff --git a/examples/stream-to-file/src/main.rs b/examples/stream-to-file/src/main.rs index e58fd1497f..2d51416239 100644 --- a/examples/stream-to-file/src/main.rs +++ b/examples/stream-to-file/src/main.rs @@ -5,9 +5,9 @@ //! ``` use axum::{ - body::{Body, Bytes}, - extract::{Multipart, Path}, - http::{Request, StatusCode}, + body::Bytes, + extract::{Multipart, Path, Request}, + http::StatusCode, response::{Html, Redirect}, routing::{get, post}, BoxError, Router, @@ -52,7 +52,7 @@ async fn main() { // POST'ing to `/file/foo.txt` will create a file called `foo.txt`. async fn save_request_body( Path(file_name): Path, - request: Request, + request: Request, ) -> Result<(), (StatusCode, String)> { stream_to_file(&file_name, request.into_body()).await } diff --git a/examples/testing/src/main.rs b/examples/testing/src/main.rs index 02079eb856..5f7dbc07f1 100644 --- a/examples/testing/src/main.rs +++ b/examples/testing/src/main.rs @@ -162,7 +162,7 @@ mod tests { // in multiple request #[tokio::test] async fn multiple_request() { - let mut app = app(); + let mut app = app().into_service(); let request = Request::builder().uri("/").body(Body::empty()).unwrap(); let response = ServiceExt::>::ready(&mut app) @@ -190,20 +190,15 @@ mod tests { // tests. #[tokio::test] async fn with_into_make_service_with_connect_info() { - let mut app = app().layer(MockConnectInfo(SocketAddr::from(([0, 0, 0, 0], 3000)))); + let mut app = app() + .layer(MockConnectInfo(SocketAddr::from(([0, 0, 0, 0], 3000)))) + .into_service(); let request = Request::builder() .uri("/requires-connect-into") .body(Body::empty()) .unwrap(); - let response = app - .as_service() - .ready() - .await - .unwrap() - .call(request) - .await - .unwrap(); + let response = app.ready().await.unwrap().call(request).await.unwrap(); assert_eq!(response.status(), StatusCode::OK); } } diff --git a/examples/validator/src/main.rs b/examples/validator/src/main.rs index 8545a3e9ce..4f4f6239bf 100644 --- a/examples/validator/src/main.rs +++ b/examples/validator/src/main.rs @@ -12,9 +12,8 @@ use async_trait::async_trait; use axum::{ - body::Body, - extract::{rejection::FormRejection, Form, FromRequest}, - http::{Request, StatusCode}, + extract::{rejection::FormRejection, Form, FromRequest, Request}, + http::StatusCode, response::{Html, IntoResponse, Response}, routing::get, Router, @@ -70,7 +69,7 @@ where { type Rejection = ServerError; - async fn from_request(req: Request, state: &S) -> Result { + async fn from_request(req: Request, state: &S) -> Result { let Form(value) = Form::::from_request(req, state).await?; value.validate()?; Ok(ValidatedForm(value))