diff --git a/axum-extra/src/routing/spa.rs b/axum-extra/src/routing/spa.rs index de360735fa..25265b02b3 100644 --- a/axum-extra/src/routing/spa.rs +++ b/axum-extra/src/routing/spa.rs @@ -263,7 +263,7 @@ mod tests { let spa = SpaRouter::new("/assets", "test_files").handle_error(handle_error); - Router::new().merge(spa); + Router::<()>::new().merge(spa); } #[allow(dead_code)] diff --git a/axum-macros/CHANGELOG.md b/axum-macros/CHANGELOG.md index 6bd23d1a1a..63c712b66f 100644 --- a/axum-macros/CHANGELOG.md +++ b/axum-macros/CHANGELOG.md @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased -- None. +- **breaking:** `#[debug_handler]` no longer accepts a `body = _` argument. The + body type is always `axum::body::Body` # 0.3.4 (12. February, 2022) diff --git a/axum-macros/src/debug_handler.rs b/axum-macros/src/debug_handler.rs index 67fd8747b1..bfe5879b5d 100644 --- a/axum-macros/src/debug_handler.rs +++ b/axum-macros/src/debug_handler.rs @@ -6,14 +6,10 @@ use crate::{ }; use proc_macro2::{Span, TokenStream}; use quote::{format_ident, quote, quote_spanned}; -use syn::{parse::Parse, parse_quote, spanned::Spanned, FnArg, ItemFn, Token, Type}; +use syn::{parse::Parse, spanned::Spanned, FnArg, ItemFn, Token, Type}; pub(crate) fn expand(attr: Attrs, item_fn: ItemFn) -> TokenStream { - let Attrs { body_ty, state_ty } = attr; - - let body_ty = body_ty - .map(second) - .unwrap_or_else(|| parse_quote!(axum::body::Body)); + let Attrs { state_ty } = attr; let mut state_ty = state_ty.map(second); @@ -48,7 +44,7 @@ pub(crate) fn expand(attr: Attrs, item_fn: ItemFn) -> TokenStream { let state_ty = state_ty.unwrap_or_else(|| syn::parse_quote!(())); let check_inputs_impls_from_request = - check_inputs_impls_from_request(&item_fn, &body_ty, state_ty); + check_inputs_impls_from_request(&item_fn, state_ty); let check_future_send = check_future_send(&item_fn); quote! { @@ -79,20 +75,16 @@ mod kw { } pub(crate) struct Attrs { - body_ty: Option<(kw::body, Type)>, state_ty: Option<(kw::state, Type)>, } impl Parse for Attrs { fn parse(input: syn::parse::ParseStream) -> syn::Result { - let mut body_ty = None; let mut state_ty = None; while !input.is_empty() { let lh = input.lookahead1(); - if lh.peek(kw::body) { - parse_assignment_attribute(input, &mut body_ty)?; - } else if lh.peek(kw::state) { + if lh.peek(kw::state) { parse_assignment_attribute(input, &mut state_ty)?; } else { return Err(lh.error()); @@ -101,7 +93,7 @@ impl Parse for Attrs { let _ = input.parse::(); } - Ok(Self { body_ty, state_ty }) + Ok(Self { state_ty }) } } @@ -174,11 +166,7 @@ fn is_self_pat_type(typed: &syn::PatType) -> bool { ident == "self" } -fn check_inputs_impls_from_request( - item_fn: &ItemFn, - body_ty: &Type, - state_ty: Type, -) -> TokenStream { +fn check_inputs_impls_from_request(item_fn: &ItemFn, state_ty: Type) -> TokenStream { let takes_self = item_fn.sig.inputs.first().map_or(false, |arg| match arg { FnArg::Receiver(_) => true, FnArg::Typed(typed) => is_self_pat_type(typed), @@ -255,7 +243,7 @@ fn check_inputs_impls_from_request( } } else { quote_spanned! {span=> - #ty: ::axum::extract::FromRequest<#state_ty, #body_ty, M> + Send + #ty: ::axum::extract::FromRequest<#state_ty, M> + Send } }; diff --git a/axum-macros/src/from_request.rs b/axum-macros/src/from_request.rs index 7f6e76c9f7..e9608e7f62 100644 --- a/axum-macros/src/from_request.rs +++ b/axum-macros/src/from_request.rs @@ -19,13 +19,6 @@ pub(crate) enum Trait { } impl Trait { - fn body_type(&self) -> impl Iterator { - match self { - Trait::FromRequest => Some(parse_quote!(B)).into_iter(), - Trait::FromRequestParts => None.into_iter(), - } - } - fn via_marker_type(&self) -> Option { match self { Trait::FromRequest => Some(parse_quote!(M)), @@ -370,14 +363,12 @@ fn impl_struct_by_extracting_each_field( quote!(::axum::response::Response) }; - let impl_generics = tr - .body_type() - .chain(state.impl_generics()) + let impl_generics = state + .impl_generics() .collect::>(); let trait_generics = state .trait_generics() - .chain(tr.body_type()) .collect::>(); let state_bounds = state.bounds(); @@ -388,15 +379,12 @@ fn impl_struct_by_extracting_each_field( #[automatically_derived] impl<#impl_generics> ::axum::extract::FromRequest<#trait_generics> for #ident where - B: ::axum::body::HttpBody + ::std::marker::Send + 'static, - B::Data: ::std::marker::Send, - B::Error: ::std::convert::Into<::axum::BoxError>, #state_bounds { type Rejection = #rejection_ident; async fn from_request( - mut req: ::axum::http::Request, + mut req: ::axum::http::Request<::axum::body::Body>, state: &#state, ) -> ::std::result::Result { #trait_fn_body @@ -749,7 +737,7 @@ fn impl_struct_by_extracting_all_at_once( // struct AppState {} // ``` // - // we need to implement `impl FromRequest` but only for + // we need to implement `impl FromRequest` but only for // - `#[derive(FromRequest)]`, not `#[derive(FromRequestParts)]` // - `State`, not other extractors // @@ -760,16 +748,15 @@ fn impl_struct_by_extracting_all_at_once( None }; - let impl_generics = tr - .body_type() - .chain(via_marker_type.clone()) + let impl_generics = via_marker_type + .iter() + .cloned() .chain(state.impl_generics()) .chain(generic_ident.is_some().then(|| parse_quote!(T))) .collect::>(); let trait_generics = state .trait_generics() - .chain(tr.body_type()) .chain(via_marker_type) .collect::>(); @@ -828,13 +815,12 @@ fn impl_struct_by_extracting_all_at_once( where #via_path<#via_type_generics>: ::axum::extract::FromRequest<#trait_generics>, #rejection_bound - B: ::std::marker::Send + 'static, #state_bounds { type Rejection = #associated_rejection_type; async fn from_request( - req: ::axum::http::Request, + req: ::axum::http::Request<::axum::body::Body>, state: &#state, ) -> ::std::result::Result { ::axum::extract::FromRequest::from_request(req, state) @@ -923,14 +909,12 @@ fn impl_enum_by_extracting_all_at_once( let path_span = path.span(); - let impl_generics = tr - .body_type() - .chain(state.impl_generics()) + let impl_generics = state + .impl_generics() .collect::>(); let trait_generics = state .trait_generics() - .chain(tr.body_type()) .collect::>(); let state_bounds = state.bounds(); @@ -942,15 +926,12 @@ fn impl_enum_by_extracting_all_at_once( #[automatically_derived] impl<#impl_generics> ::axum::extract::FromRequest<#trait_generics> for #ident where - B: ::axum::body::HttpBody + ::std::marker::Send + 'static, - B::Data: ::std::marker::Send, - B::Error: ::std::convert::Into<::axum::BoxError>, #state_bounds { type Rejection = #associated_rejection_type; async fn from_request( - req: ::axum::http::Request, + req: ::axum::http::Request<::axum::body::Body>, state: &#state, ) -> ::std::result::Result { ::axum::extract::FromRequest::from_request(req, state) diff --git a/axum-macros/src/lib.rs b/axum-macros/src/lib.rs index 78575390a6..ba462a50d2 100644 --- a/axum-macros/src/lib.rs +++ b/axum-macros/src/lib.rs @@ -147,7 +147,7 @@ use from_request::Trait::{FromRequest, FromRequestParts}; /// ``` /// pub struct ViaExtractor(pub T); /// -/// // impl FromRequest for ViaExtractor { ... } +/// // impl FromRequest for ViaExtractor { ... } /// ``` /// /// More complex via extractors are not supported and require writing a manual implementation. @@ -480,21 +480,6 @@ pub fn derive_from_request_parts(item: TokenStream) -> TokenStream { /// } /// ``` /// -/// # Changing request body type -/// -/// By default `#[debug_handler]` assumes your request body type is `axum::body::Body`. This will -/// work for most extractors but, for example, it wont work for `Request`, -/// which only implements `FromRequest` and _not_ `FromRequest`. -/// -/// To work around that the request body type can be customized like so: -/// -/// ``` -/// use axum::{body::BoxBody, http::Request, debug_handler}; -/// -/// #[debug_handler(body = BoxBody)] -/// async fn handler(request: Request) {} -/// ``` -/// /// # Changing state type /// /// By default `#[debug_handler]` assumes your state type is `()` unless your handler has a diff --git a/axum-macros/tests/debug_handler/fail/argument_not_extractor.stderr b/axum-macros/tests/debug_handler/fail/argument_not_extractor.stderr index 360163cade..8be92b7913 100644 --- a/axum-macros/tests/debug_handler/fail/argument_not_extractor.stderr +++ b/axum-macros/tests/debug_handler/fail/argument_not_extractor.stderr @@ -16,7 +16,7 @@ error[E0277]: the trait bound `bool: FromRequestParts<()>` is not satisfied <(T1, T2, T3, T4, T5, T6, T7) as FromRequestParts> <(T1, T2, T3, T4, T5, T6, T7, T8) as FromRequestParts> and 26 others - = note: required for `bool` to implement `FromRequest<(), Body, axum_core::extract::private::ViaParts>` + = note: required for `bool` to implement `FromRequest<(), axum_core::extract::private::ViaParts>` note: required by a bound in `__axum_macros_check_handler_0_from_request_check` --> tests/debug_handler/fail/argument_not_extractor.rs:4:23 | diff --git a/axum-macros/tests/debug_handler/fail/duplicate_args.rs b/axum-macros/tests/debug_handler/fail/duplicate_args.rs index dca335bd3c..4fc7c90fd6 100644 --- a/axum-macros/tests/debug_handler/fail/duplicate_args.rs +++ b/axum-macros/tests/debug_handler/fail/duplicate_args.rs @@ -1,9 +1,6 @@ use axum_macros::debug_handler; -#[debug_handler(body = BoxBody, body = BoxBody)] -async fn handler() {} - #[debug_handler(state = (), state = ())] -async fn handler_2() {} +async fn handler() {} fn main() {} diff --git a/axum-macros/tests/debug_handler/fail/duplicate_args.stderr b/axum-macros/tests/debug_handler/fail/duplicate_args.stderr index 694b6cb3ee..bed70f774a 100644 --- a/axum-macros/tests/debug_handler/fail/duplicate_args.stderr +++ b/axum-macros/tests/debug_handler/fail/duplicate_args.stderr @@ -1,11 +1,5 @@ -error: `body` specified more than once - --> tests/debug_handler/fail/duplicate_args.rs:3:33 - | -3 | #[debug_handler(body = BoxBody, body = BoxBody)] - | ^^^^ - error: `state` specified more than once - --> tests/debug_handler/fail/duplicate_args.rs:6:29 + --> tests/debug_handler/fail/duplicate_args.rs:3:29 | -6 | #[debug_handler(state = (), state = ())] +3 | #[debug_handler(state = (), state = ())] | ^^^^^ 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 d20426e22f..1d45b50494 100644 --- a/axum-macros/tests/debug_handler/fail/extract_self_mut.rs +++ b/axum-macros/tests/debug_handler/fail/extract_self_mut.rs @@ -8,14 +8,13 @@ use axum_macros::debug_handler; struct A; #[async_trait] -impl FromRequest for A +impl FromRequest for A where - B: Send + 'static, S: Send + Sync, { 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 1e1a9ec384..3d80dffbca 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:25:22 + --> tests/debug_handler/fail/extract_self_mut.rs:24:22 | -25 | async fn handler(&mut self) {} +24 | 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 77940e2996..e5c3bb032d 100644 --- a/axum-macros/tests/debug_handler/fail/extract_self_ref.rs +++ b/axum-macros/tests/debug_handler/fail/extract_self_ref.rs @@ -8,14 +8,13 @@ use axum_macros::debug_handler; struct A; #[async_trait] -impl FromRequest for A +impl FromRequest for A where - B: Send + 'static, S: Send + Sync, { 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 79f9d190f5..82d9a89ff5 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:25:22 + --> tests/debug_handler/fail/extract_self_ref.rs:24:22 | -25 | async fn handler(&self) {} +24 | async fn handler(&self) {} | ^^^^^ diff --git a/axum-macros/tests/debug_handler/fail/invalid_attrs.stderr b/axum-macros/tests/debug_handler/fail/invalid_attrs.stderr index 93514ebfe5..d2db79b336 100644 --- a/axum-macros/tests/debug_handler/fail/invalid_attrs.stderr +++ b/axum-macros/tests/debug_handler/fail/invalid_attrs.stderr @@ -1,4 +1,4 @@ -error: expected `body` or `state` +error: expected `state` --> tests/debug_handler/fail/invalid_attrs.rs:3:17 | 3 | #[debug_handler(foo)] diff --git a/axum-macros/tests/debug_handler/pass/different_request_body_type.rs b/axum-macros/tests/debug_handler/pass/different_request_body_type.rs deleted file mode 100644 index 715e5aec19..0000000000 --- a/axum-macros/tests/debug_handler/pass/different_request_body_type.rs +++ /dev/null @@ -1,10 +0,0 @@ -use axum::{body::BoxBody, http::Request}; -use axum_macros::debug_handler; - -#[debug_handler(body = BoxBody)] -async fn handler(_: Request) {} - -#[debug_handler(body = axum::body::BoxBody,)] -async fn handler_with_trailing_comma_and_type_path(_: 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 e7bf81ce6c..2b4dfa4d6b 100644 --- a/axum-macros/tests/debug_handler/pass/self_receiver.rs +++ b/axum-macros/tests/debug_handler/pass/self_receiver.rs @@ -8,27 +8,25 @@ use axum_macros::debug_handler; struct A; #[async_trait] -impl FromRequest for A +impl FromRequest for A where - B: Send + 'static, S: Send + Sync, { type Rejection = (); - async fn from_request(_req: Request, _state: &S) -> Result { + async fn from_request(_req: Request, _state: &S) -> Result { unimplemented!() } } #[async_trait] -impl FromRequest for Box +impl FromRequest for Box where - B: Send + 'static, S: Send + Sync, { 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 5c84dbd25b..7c06742e54 100644 --- a/axum-macros/tests/debug_handler/pass/set_state.rs +++ b/axum-macros/tests/debug_handler/pass/set_state.rs @@ -12,15 +12,14 @@ struct AppState; struct A; #[async_trait] -impl FromRequest for A +impl FromRequest for A where - B: Send + 'static, S: Send + Sync, AppState: FromRef, { 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 7e1525f524..fea3700745 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::{body::BoxBody, extract::State, http::Request}; +use axum::{extract::State, http::Request}; -#[debug_handler(state = AppState, body = BoxBody)] -async fn handler(_: State, _: Request) {} +#[debug_handler(state = AppState)] +async fn handler(_: State, _: Request) {} #[derive(Clone)] struct AppState; diff --git a/axum-macros/tests/from_request/fail/generic_without_via.rs b/axum-macros/tests/from_request/fail/generic_without_via.rs index 38eaa437a3..e2b690938f 100644 --- a/axum-macros/tests/from_request/fail/generic_without_via.rs +++ b/axum-macros/tests/from_request/fail/generic_without_via.rs @@ -1,4 +1,4 @@ -use axum::{body::Body, routing::get, Router}; +use axum::{routing::get, Router}; use axum_macros::FromRequest; #[derive(FromRequest, Clone)] @@ -7,5 +7,5 @@ struct Extractor(T); async fn foo(_: Extractor<()>) {} fn main() { - Router::<(), Body>::new().route("/", get(foo)); + Router::<()>::new().route("/", get(foo)); } diff --git a/axum-macros/tests/from_request/fail/generic_without_via.stderr b/axum-macros/tests/from_request/fail/generic_without_via.stderr index 7630c9bf75..64c6a49281 100644 --- a/axum-macros/tests/from_request/fail/generic_without_via.stderr +++ b/axum-macros/tests/from_request/fail/generic_without_via.stderr @@ -4,18 +4,18 @@ error: #[derive(FromRequest)] only supports generics when used with #[from_reque 5 | struct Extractor(T); | ^ -error[E0277]: the trait bound `fn(Extractor<()>) -> impl Future {foo}: Handler<_, _, _>` is not satisfied - --> tests/from_request/fail/generic_without_via.rs:10:46 +error[E0277]: the trait bound `fn(Extractor<()>) -> impl Future {foo}: Handler<_, _>` is not satisfied + --> tests/from_request/fail/generic_without_via.rs:10:40 | -10 | Router::<(), Body>::new().route("/", get(foo)); - | --- ^^^ the trait `Handler<_, _, _>` is not implemented for fn item `fn(Extractor<()>) -> impl Future {foo}` - | | - | required by a bound introduced by this call +10 | Router::<()>::new().route("/", get(foo)); + | --- ^^^ the trait `Handler<_, _>` is not implemented for fn item `fn(Extractor<()>) -> impl Future {foo}` + | | + | required by a bound introduced by this call | = note: Consider using `#[axum::debug_handler]` to improve the error message - = help: the following other types implement trait `Handler`: - as Handler> - as Handler<(), S, B>> + = help: the following other types implement trait `Handler`: + as Handler> + as Handler<(), S>> note: required by a bound in `axum::routing::get` --> $WORKSPACE/axum/src/routing/method_routing.rs | diff --git a/axum-macros/tests/from_request/fail/generic_without_via_rejection.rs b/axum-macros/tests/from_request/fail/generic_without_via_rejection.rs index 38d6b0910b..598767df78 100644 --- a/axum-macros/tests/from_request/fail/generic_without_via_rejection.rs +++ b/axum-macros/tests/from_request/fail/generic_without_via_rejection.rs @@ -1,4 +1,4 @@ -use axum::{body::Body, routing::get, Router}; +use axum::{routing::get, Router}; use axum_macros::FromRequest; #[derive(FromRequest, Clone)] @@ -8,5 +8,5 @@ struct Extractor(T); async fn foo(_: Extractor<()>) {} fn main() { - Router::<(), Body>::new().route("/", get(foo)); + Router::<()>::new().route("/", get(foo)); } diff --git a/axum-macros/tests/from_request/fail/generic_without_via_rejection.stderr b/axum-macros/tests/from_request/fail/generic_without_via_rejection.stderr index 65f0e64d74..50a38bb24a 100644 --- a/axum-macros/tests/from_request/fail/generic_without_via_rejection.stderr +++ b/axum-macros/tests/from_request/fail/generic_without_via_rejection.stderr @@ -4,18 +4,18 @@ error: #[derive(FromRequest)] only supports generics when used with #[from_reque 6 | struct Extractor(T); | ^ -error[E0277]: the trait bound `fn(Extractor<()>) -> impl Future {foo}: Handler<_, _, _>` is not satisfied - --> tests/from_request/fail/generic_without_via_rejection.rs:11:46 +error[E0277]: the trait bound `fn(Extractor<()>) -> impl Future {foo}: Handler<_, _>` is not satisfied + --> tests/from_request/fail/generic_without_via_rejection.rs:11:40 | -11 | Router::<(), Body>::new().route("/", get(foo)); - | --- ^^^ the trait `Handler<_, _, _>` is not implemented for fn item `fn(Extractor<()>) -> impl Future {foo}` - | | - | required by a bound introduced by this call +11 | Router::<()>::new().route("/", get(foo)); + | --- ^^^ the trait `Handler<_, _>` is not implemented for fn item `fn(Extractor<()>) -> impl Future {foo}` + | | + | required by a bound introduced by this call | = note: Consider using `#[axum::debug_handler]` to improve the error message - = help: the following other types implement trait `Handler`: - as Handler> - as Handler<(), S, B>> + = help: the following other types implement trait `Handler`: + as Handler> + as Handler<(), S>> note: required by a bound in `axum::routing::get` --> $WORKSPACE/axum/src/routing/method_routing.rs | diff --git a/axum-macros/tests/from_request/fail/override_rejection_on_enum_without_via.stderr b/axum-macros/tests/from_request/fail/override_rejection_on_enum_without_via.stderr index d5a11d81d0..7ed7dea626 100644 --- a/axum-macros/tests/from_request/fail/override_rejection_on_enum_without_via.stderr +++ b/axum-macros/tests/from_request/fail/override_rejection_on_enum_without_via.stderr @@ -4,18 +4,18 @@ error: cannot use `rejection` without `via` 18 | #[from_request(rejection(MyRejection))] | ^^^^^^^^^ -error[E0277]: the trait bound `fn(MyExtractor) -> impl Future {handler}: Handler<_, _, _>` is not satisfied +error[E0277]: the trait bound `fn(MyExtractor) -> impl Future {handler}: Handler<_, _>` is not satisfied --> tests/from_request/fail/override_rejection_on_enum_without_via.rs:10:50 | 10 | let _: Router = Router::new().route("/", get(handler).post(handler_result)); - | --- ^^^^^^^ the trait `Handler<_, _, _>` is not implemented for fn item `fn(MyExtractor) -> impl Future {handler}` + | --- ^^^^^^^ the trait `Handler<_, _>` is not implemented for fn item `fn(MyExtractor) -> impl Future {handler}` | | | required by a bound introduced by this call | = note: Consider using `#[axum::debug_handler]` to improve the error message - = help: the following other types implement trait `Handler`: - as Handler> - as Handler<(), S, B>> + = help: the following other types implement trait `Handler`: + as Handler> + as Handler<(), S>> note: required by a bound in `axum::routing::get` --> $WORKSPACE/axum/src/routing/method_routing.rs | @@ -23,21 +23,21 @@ note: required by a bound in `axum::routing::get` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `axum::routing::get` = note: this error originates in the macro `top_level_handler_fn` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0277]: the trait bound `fn(Result) -> impl Future {handler_result}: Handler<_, _, _>` is not satisfied +error[E0277]: the trait bound `fn(Result) -> impl Future {handler_result}: Handler<_, _>` is not satisfied --> tests/from_request/fail/override_rejection_on_enum_without_via.rs:10:64 | 10 | let _: Router = Router::new().route("/", get(handler).post(handler_result)); - | ---- ^^^^^^^^^^^^^^ the trait `Handler<_, _, _>` is not implemented for fn item `fn(Result) -> impl Future {handler_result}` + | ---- ^^^^^^^^^^^^^^ the trait `Handler<_, _>` is not implemented for fn item `fn(Result) -> impl Future {handler_result}` | | | required by a bound introduced by this call | = note: Consider using `#[axum::debug_handler]` to improve the error message - = help: the following other types implement trait `Handler`: - as Handler> - as Handler<(), S, B>> -note: required by a bound in `MethodRouter::::post` + = help: the following other types implement trait `Handler`: + as Handler> + as Handler<(), S>> +note: required by a bound in `MethodRouter::::post` --> $WORKSPACE/axum/src/routing/method_routing.rs | | chained_handler_fn!(post, POST); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `MethodRouter::::post` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `MethodRouter::::post` = note: this error originates in the macro `chained_handler_fn` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/axum-macros/tests/from_request/fail/state_infer_multiple_different_types.rs b/axum-macros/tests/from_request/fail/state_infer_multiple_different_types.rs index 57400377ef..6533d3276a 100644 --- a/axum-macros/tests/from_request/fail/state_infer_multiple_different_types.rs +++ b/axum-macros/tests/from_request/fail/state_infer_multiple_different_types.rs @@ -15,7 +15,7 @@ struct OtherState {} fn assert_from_request() where - Extractor: axum::extract::FromRequest, + Extractor: axum::extract::FromRequest, { } diff --git a/axum-macros/tests/from_request/pass/container.rs b/axum-macros/tests/from_request/pass/container.rs index 6e62c569a4..9d4e0666e9 100644 --- a/axum-macros/tests/from_request/pass/container.rs +++ b/axum-macros/tests/from_request/pass/container.rs @@ -1,5 +1,4 @@ use axum::{ - body::Body, extract::{FromRequest, Json}, response::Response, }; @@ -15,7 +14,7 @@ struct Extractor { fn assert_from_request() where - Extractor: FromRequest<(), Body, Rejection = Response>, + Extractor: FromRequest<(), Rejection = Response>, { } diff --git a/axum-macros/tests/from_request/pass/empty_named.rs b/axum-macros/tests/from_request/pass/empty_named.rs index eec021d0f5..63af28f5d0 100644 --- a/axum-macros/tests/from_request/pass/empty_named.rs +++ b/axum-macros/tests/from_request/pass/empty_named.rs @@ -5,7 +5,7 @@ struct Extractor {} fn assert_from_request() where - Extractor: axum::extract::FromRequest<(), axum::body::Body, Rejection = std::convert::Infallible>, + Extractor: axum::extract::FromRequest<(), Rejection = std::convert::Infallible>, { } diff --git a/axum-macros/tests/from_request/pass/empty_tuple.rs b/axum-macros/tests/from_request/pass/empty_tuple.rs index 3d8bcd25c0..b740cb8374 100644 --- a/axum-macros/tests/from_request/pass/empty_tuple.rs +++ b/axum-macros/tests/from_request/pass/empty_tuple.rs @@ -5,7 +5,7 @@ struct Extractor(); fn assert_from_request() where - Extractor: axum::extract::FromRequest<(), axum::body::Body, Rejection = std::convert::Infallible>, + Extractor: axum::extract::FromRequest<(), Rejection = std::convert::Infallible>, { } diff --git a/axum-macros/tests/from_request/pass/enum_via.rs b/axum-macros/tests/from_request/pass/enum_via.rs index c68b9796a7..44e3f306c1 100644 --- a/axum-macros/tests/from_request/pass/enum_via.rs +++ b/axum-macros/tests/from_request/pass/enum_via.rs @@ -1,4 +1,4 @@ -use axum::{body::Body, routing::get, Extension, Router}; +use axum::{routing::get, Extension, Router}; use axum_macros::FromRequest; #[derive(FromRequest, Clone)] @@ -8,5 +8,5 @@ enum Extractor {} async fn foo(_: Extractor) {} fn main() { - Router::<(), Body>::new().route("/", get(foo)); + Router::<()>::new().route("/", get(foo)); } diff --git a/axum-macros/tests/from_request/pass/enum_via_parts.rs b/axum-macros/tests/from_request/pass/enum_via_parts.rs index 5e18d9228d..784d396214 100644 --- a/axum-macros/tests/from_request/pass/enum_via_parts.rs +++ b/axum-macros/tests/from_request/pass/enum_via_parts.rs @@ -1,4 +1,4 @@ -use axum::{body::Body, routing::get, Extension, Router}; +use axum::{routing::get, Extension, Router}; use axum_macros::FromRequestParts; #[derive(FromRequestParts, Clone)] @@ -8,5 +8,5 @@ enum Extractor {} async fn foo(_: Extractor) {} fn main() { - Router::<(), Body>::new().route("/", get(foo)); + Router::<()>::new().route("/", get(foo)); } diff --git a/axum-macros/tests/from_request/pass/named.rs b/axum-macros/tests/from_request/pass/named.rs index e042477b90..e775d41d84 100644 --- a/axum-macros/tests/from_request/pass/named.rs +++ b/axum-macros/tests/from_request/pass/named.rs @@ -1,5 +1,4 @@ use axum::{ - body::Body, extract::{FromRequest, TypedHeader, rejection::TypedHeaderRejection}, response::Response, headers::{self, UserAgent}, @@ -17,7 +16,7 @@ struct Extractor { fn assert_from_request() where - Extractor: FromRequest<(), Body, Rejection = Response>, + Extractor: FromRequest<(), Rejection = Response>, { } diff --git a/axum-macros/tests/from_request/pass/named_via.rs b/axum-macros/tests/from_request/pass/named_via.rs index 41cc361556..be2e8c67a6 100644 --- a/axum-macros/tests/from_request/pass/named_via.rs +++ b/axum-macros/tests/from_request/pass/named_via.rs @@ -1,5 +1,4 @@ use axum::{ - body::Body, response::Response, extract::{ rejection::TypedHeaderRejection, @@ -24,7 +23,7 @@ struct Extractor { fn assert_from_request() where - Extractor: FromRequest<(), Body, Rejection = Response>, + Extractor: FromRequest<(), Rejection = Response>, { } diff --git a/axum-macros/tests/from_request/pass/override_rejection.rs b/axum-macros/tests/from_request/pass/override_rejection.rs index 0147c9a8b3..db341b792e 100644 --- a/axum-macros/tests/from_request/pass/override_rejection.rs +++ b/axum-macros/tests/from_request/pass/override_rejection.rs @@ -4,6 +4,7 @@ use axum::{ http::{StatusCode, Request}, response::{IntoResponse, Response}, routing::get, + body::Body, Extension, Router, }; @@ -27,15 +28,14 @@ struct MyExtractor { struct OtherExtractor; #[async_trait] -impl FromRequest for OtherExtractor +impl FromRequest for OtherExtractor where - B: Send + 'static, S: Send + Sync, { // 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-macros/tests/from_request/pass/state_cookie.rs b/axum-macros/tests/from_request/pass/state_cookie.rs index a4f46c6acd..6e2aa1f4ed 100644 --- a/axum-macros/tests/from_request/pass/state_cookie.rs +++ b/axum-macros/tests/from_request/pass/state_cookie.rs @@ -20,7 +20,7 @@ impl FromRef for Key { fn assert_from_request() where - Extractor: axum::extract::FromRequest, + Extractor: axum::extract::FromRequest, { } diff --git a/axum-macros/tests/from_request/pass/state_infer.rs b/axum-macros/tests/from_request/pass/state_infer.rs index 5290614991..07545ab074 100644 --- a/axum-macros/tests/from_request/pass/state_infer.rs +++ b/axum-macros/tests/from_request/pass/state_infer.rs @@ -11,7 +11,7 @@ struct AppState {} fn assert_from_request() where - Extractor: axum::extract::FromRequest, + Extractor: axum::extract::FromRequest, { } diff --git a/axum-macros/tests/from_request/pass/state_infer_multiple.rs b/axum-macros/tests/from_request/pass/state_infer_multiple.rs index 6729e61572..cb8de1d59c 100644 --- a/axum-macros/tests/from_request/pass/state_infer_multiple.rs +++ b/axum-macros/tests/from_request/pass/state_infer_multiple.rs @@ -12,7 +12,7 @@ struct AppState {} fn assert_from_request() where - Extractor: axum::extract::FromRequest, + Extractor: axum::extract::FromRequest, { } diff --git a/axum-macros/tests/from_request/pass/tuple.rs b/axum-macros/tests/from_request/pass/tuple.rs index 2af407d0f9..85a409817e 100644 --- a/axum-macros/tests/from_request/pass/tuple.rs +++ b/axum-macros/tests/from_request/pass/tuple.rs @@ -5,7 +5,7 @@ struct Extractor(axum::http::HeaderMap, String); fn assert_from_request() where - Extractor: axum::extract::FromRequest<(), axum::body::Body>, + Extractor: axum::extract::FromRequest<()>, { } diff --git a/axum-macros/tests/from_request/pass/tuple_same_type_twice.rs b/axum-macros/tests/from_request/pass/tuple_same_type_twice.rs index 227e4a3c8f..343563ddb6 100644 --- a/axum-macros/tests/from_request/pass/tuple_same_type_twice.rs +++ b/axum-macros/tests/from_request/pass/tuple_same_type_twice.rs @@ -13,7 +13,7 @@ struct Payload {} fn assert_from_request() where - Extractor: axum::extract::FromRequest<(), axum::body::Body>, + Extractor: axum::extract::FromRequest<()>, { } diff --git a/axum-macros/tests/from_request/pass/tuple_same_type_twice_via.rs b/axum-macros/tests/from_request/pass/tuple_same_type_twice_via.rs index 82342c56c5..ab0ee467f6 100644 --- a/axum-macros/tests/from_request/pass/tuple_same_type_twice_via.rs +++ b/axum-macros/tests/from_request/pass/tuple_same_type_twice_via.rs @@ -14,7 +14,7 @@ struct Payload {} fn assert_from_request() where - Extractor: axum::extract::FromRequest<(), axum::body::Body, Rejection = Response>, + Extractor: axum::extract::FromRequest<(), Rejection = Response>, { } diff --git a/axum-macros/tests/from_request/pass/tuple_via.rs b/axum-macros/tests/from_request/pass/tuple_via.rs index 03a9e3610c..3b62287ed8 100644 --- a/axum-macros/tests/from_request/pass/tuple_via.rs +++ b/axum-macros/tests/from_request/pass/tuple_via.rs @@ -9,7 +9,7 @@ struct State; fn assert_from_request() where - Extractor: axum::extract::FromRequest<(), axum::body::Body>, + Extractor: axum::extract::FromRequest<()>, { } diff --git a/axum-macros/tests/from_request/pass/unit.rs b/axum-macros/tests/from_request/pass/unit.rs index 3e5d986917..9a4dc1dd44 100644 --- a/axum-macros/tests/from_request/pass/unit.rs +++ b/axum-macros/tests/from_request/pass/unit.rs @@ -5,7 +5,7 @@ struct Extractor; fn assert_from_request() where - Extractor: axum::extract::FromRequest<(), axum::body::Body, Rejection = std::convert::Infallible>, + Extractor: axum::extract::FromRequest<(), Rejection = std::convert::Infallible>, { } diff --git a/axum-macros/tests/typed_path/pass/customize_rejection.rs b/axum-macros/tests/typed_path/pass/customize_rejection.rs index 40f3ec0ada..f490898c98 100644 --- a/axum-macros/tests/typed_path/pass/customize_rejection.rs +++ b/axum-macros/tests/typed_path/pass/customize_rejection.rs @@ -40,7 +40,7 @@ impl Default for MyRejection { } fn main() { - axum::Router::<(), axum::body::Body>::new() + axum::Router::<()>::new() .typed_get(|_: Result| async {}) .typed_post(|_: Result| async {}) .typed_put(|_: Result| async {}); diff --git a/axum-macros/tests/typed_path/pass/named_fields_struct.rs b/axum-macros/tests/typed_path/pass/named_fields_struct.rs index 6119304080..2a05d7775f 100644 --- a/axum-macros/tests/typed_path/pass/named_fields_struct.rs +++ b/axum-macros/tests/typed_path/pass/named_fields_struct.rs @@ -9,7 +9,7 @@ struct MyPath { } fn main() { - axum::Router::<(), axum::body::Body>::new().route("/", axum::routing::get(|_: MyPath| async {})); + axum::Router::<()>::new().route("/", axum::routing::get(|_: MyPath| async {})); assert_eq!(MyPath::PATH, "/users/:user_id/teams/:team_id"); assert_eq!( diff --git a/axum-macros/tests/typed_path/pass/option_result.rs b/axum-macros/tests/typed_path/pass/option_result.rs index bd4c6dc282..7ccd5f7f39 100644 --- a/axum-macros/tests/typed_path/pass/option_result.rs +++ b/axum-macros/tests/typed_path/pass/option_result.rs @@ -19,7 +19,7 @@ struct UsersIndex; async fn result_handler_unit_struct(_: Result) {} fn main() { - axum::Router::<(), axum::body::Body>::new() + axum::Router::<()>::new() .typed_get(option_handler) .typed_post(result_handler) .typed_post(result_handler_unit_struct); diff --git a/axum-macros/tests/typed_path/pass/tuple_struct.rs b/axum-macros/tests/typed_path/pass/tuple_struct.rs index 4f8fa17eeb..27e9aa8bfd 100644 --- a/axum-macros/tests/typed_path/pass/tuple_struct.rs +++ b/axum-macros/tests/typed_path/pass/tuple_struct.rs @@ -8,7 +8,7 @@ pub type Result = std::result::Result; struct MyPath(u32, u32); fn main() { - axum::Router::<(), axum::body::Body>::new().route("/", axum::routing::get(|_: MyPath| async {})); + axum::Router::<()>::new().route("/", axum::routing::get(|_: MyPath| async {})); assert_eq!(MyPath::PATH, "/users/:user_id/teams/:team_id"); assert_eq!(format!("{}", MyPath(1, 2)), "/users/1/teams/2"); diff --git a/axum-macros/tests/typed_path/pass/unit_struct.rs b/axum-macros/tests/typed_path/pass/unit_struct.rs index 0ba27f81ac..233cb982f8 100644 --- a/axum-macros/tests/typed_path/pass/unit_struct.rs +++ b/axum-macros/tests/typed_path/pass/unit_struct.rs @@ -5,7 +5,7 @@ use axum_extra::routing::TypedPath; struct MyPath; fn main() { - axum::Router::<(), axum::body::Body>::new() + axum::Router::<()>::new() .route("/", axum::routing::get(|_: MyPath| async {})); assert_eq!(MyPath::PATH, "/users"); diff --git a/axum-macros/tests/typed_path/pass/wildcards.rs b/axum-macros/tests/typed_path/pass/wildcards.rs index e7794fc895..b310beb0de 100644 --- a/axum-macros/tests/typed_path/pass/wildcards.rs +++ b/axum-macros/tests/typed_path/pass/wildcards.rs @@ -8,5 +8,5 @@ struct MyPath { } fn main() { - axum::Router::<(), axum::body::Body>::new().typed_get(|_: MyPath| async {}); + axum::Router::<()>::new().typed_get(|_: MyPath| async {}); } diff --git a/axum/src/response/mod.rs b/axum/src/response/mod.rs index c3f4d49327..8ffdd11522 100644 --- a/axum/src/response/mod.rs +++ b/axum/src/response/mod.rs @@ -98,7 +98,7 @@ mod tests { } } - Router::new() + Router::<()>::new() .route("/", get(impl_trait_ok)) .route("/", get(impl_trait_err)) .route("/", get(impl_trait_both)) @@ -208,7 +208,7 @@ mod tests { ) } - Router::new() + Router::<()>::new() .route("/", get(status)) .route("/", get(status_headermap)) .route("/", get(status_header_array)) diff --git a/axum/src/routing/tests/nest.rs b/axum/src/routing/tests/nest.rs index 9a15af6b98..216b168623 100644 --- a/axum/src/routing/tests/nest.rs +++ b/axum/src/routing/tests/nest.rs @@ -265,7 +265,7 @@ async fn multiple_top_level_nests() { #[crate::test] #[should_panic(expected = "Invalid route: nested routes cannot contain wildcards (*)")] async fn nest_cannot_contain_wildcards() { - Router::new().nest("/one/*rest", Router::new()); + Router::<()>::new().nest("/one/*rest", Router::new()); } #[crate::test] 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 f8ae4e491a..6a5227cd42 100644 --- a/examples/consume-body-in-extractor-or-middleware/src/main.rs +++ b/examples/consume-body-in-extractor-or-middleware/src/main.rs @@ -6,7 +6,7 @@ use axum::{ async_trait, - body::{self, BoxBody, Bytes, Full}, + body::{Body, Bytes}, extract::FromRequest, http::{Request, StatusCode}, middleware::{self, Next}, @@ -16,7 +16,6 @@ use axum::{ }; use std::net::SocketAddr; use tower::ServiceBuilder; -use tower_http::ServiceBuilderExt; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; #[tokio::main] @@ -29,11 +28,9 @@ async fn main() { .with(tracing_subscriber::fmt::layer()) .init(); - let app = Router::new().route("/", post(handler)).layer( - ServiceBuilder::new() - .map_request_body(body::boxed) - .layer(middleware::from_fn(print_request_body)), - ); + let app = Router::new() + .route("/", post(handler)) + .layer(ServiceBuilder::new().layer(middleware::from_fn(print_request_body))); let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); tracing::debug!("listening on {}", addr); @@ -45,8 +42,8 @@ async fn main() { // middleware that shows how to consume the request body upfront async fn print_request_body( - request: Request, - next: Next, + request: Request, + next: Next, ) -> Result { let request = buffer_request_body(request).await?; @@ -55,7 +52,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, Response> { let (parts, body) = request.into_parts(); // this wont work if the body is an long running stream @@ -65,7 +62,7 @@ async fn buffer_request_body(request: Request) -> Result FromRequest for BufferRequestBody +impl FromRequest for BufferRequestBody where S: Send + Sync, { 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 d9093bc493..4cedcc9f79 100644 --- a/examples/customize-extractor-error/src/custom_extractor.rs +++ b/examples/customize-extractor-error/src/custom_extractor.rs @@ -6,6 +6,7 @@ //! - Complexity: Manually implementing `FromRequest` results on more complex code use axum::{ async_trait, + body::Body, extract::{rejection::JsonRejection, FromRequest, MatchedPath}, http::Request, http::StatusCode, @@ -22,15 +23,14 @@ pub async fn handler(Json(value): Json) -> impl IntoResponse { pub struct Json(pub T); #[async_trait] -impl FromRequest for Json +impl FromRequest for Json where - axum::Json: FromRequest, + axum::Json: FromRequest, S: Send + Sync, - B: Send + 'static, { 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 27d3b85046..73abf78f18 100644 --- a/examples/http-proxy/src/main.rs +++ b/examples/http-proxy/src/main.rs @@ -37,8 +37,9 @@ async fn main() { let router_svc = Router::new().route("/", get(|| async { "Hello, World!" })); - let service = tower::service_fn(move |req: Request| { + let service = tower::service_fn(move |req: Request<_>| { let router_svc = router_svc.clone(); + let req = req.map(Body::new); async move { if req.method() == Method::CONNECT { proxy(req).await diff --git a/examples/low-level-rustls/src/main.rs b/examples/low-level-rustls/src/main.rs index 16de3ac1ed..68c228ae67 100644 --- a/examples/low-level-rustls/src/main.rs +++ b/examples/low-level-rustls/src/main.rs @@ -4,7 +4,7 @@ //! cd examples && cargo run -p example-low-level-rustls //! ``` -use axum::{extract::ConnectInfo, routing::get, Router}; +use axum::{body::Body, extract::ConnectInfo, http::Request, routing::get, Router}; use futures_util::future::poll_fn; use hyper::server::{ accept::Accept, @@ -24,7 +24,7 @@ use tokio_rustls::{ rustls::{Certificate, PrivateKey, ServerConfig}, TlsAcceptor, }; -use tower::MakeService; +use tower::make::MakeService; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; #[tokio::main] @@ -53,7 +53,7 @@ async fn main() { let protocol = Arc::new(Http::new()); - let mut app = Router::new() + let mut app = Router::<()>::new() .route("/", get(handler)) .into_make_service_with_connect_info::(); @@ -67,7 +67,7 @@ async fn main() { let protocol = protocol.clone(); - let svc = app.make_service(&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 2e1222a767..c8d31f64d0 100644 --- a/examples/parse-body-based-on-content-type/src/main.rs +++ b/examples/parse-body-based-on-content-type/src/main.rs @@ -8,6 +8,7 @@ use axum::{ async_trait, + body::Body, extract::FromRequest, http::{header::CONTENT_TYPE, Request, StatusCode}, response::{IntoResponse, Response}, @@ -51,17 +52,16 @@ async fn handler(JsonOrForm(payload): JsonOrForm) { struct JsonOrForm(T); #[async_trait] -impl FromRequest for JsonOrForm +impl FromRequest for JsonOrForm where - B: Send + 'static, S: Send + Sync, - Json: FromRequest<(), B>, - Form: FromRequest<(), B>, + Json: FromRequest<()>, + Form: FromRequest<()>, T: 'static, { 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/reverse-proxy/src/main.rs b/examples/reverse-proxy/src/main.rs index d8cabb79aa..124174e5bb 100644 --- a/examples/reverse-proxy/src/main.rs +++ b/examples/reverse-proxy/src/main.rs @@ -8,12 +8,14 @@ //! ``` use axum::{ + body::Body, extract::State, - http::{uri::Uri, Request, Response}, + http::{uri::Uri, Request}, + response::{IntoResponse, Response}, routing::get, Router, }; -use hyper::{client::HttpConnector, Body}; +use hyper::client::HttpConnector; use std::net::SocketAddr; type Client = hyper::client::Client; @@ -22,7 +24,7 @@ type Client = hyper::client::Client; async fn main() { tokio::spawn(server()); - let client = Client::new(); + let client: Client = hyper::Client::builder().build(HttpConnector::new()); let app = Router::new().route("/", get(handler)).with_state(client); @@ -34,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() @@ -46,7 +48,7 @@ async fn handler(State(client): State, mut req: Request) -> Respon *req.uri_mut() = Uri::try_from(uri).unwrap(); - client.request(req).await.unwrap() + client.request(req).await.unwrap().into_response() } async fn server() { diff --git a/examples/static-file-server/src/main.rs b/examples/static-file-server/src/main.rs index 1ff4cf90ad..21403f8ced 100644 --- a/examples/static-file-server/src/main.rs +++ b/examples/static-file-server/src/main.rs @@ -94,9 +94,10 @@ fn using_serve_dir_with_handler_as_service() -> Router { } // you can convert handler function to service - let service = handle_404 - .into_service() - .map_err(|err| -> std::io::Error { match err {} }); + let service = tower::ServiceExt::>::map_err( + handle_404.into_service(), + |err| -> std::io::Error { match err {} }, + ); let serve_dir = ServeDir::new("assets").not_found_service(service); let serve_dir = get_service(serve_dir).handle_error(handle_error); diff --git a/examples/stream-to-file/src/main.rs b/examples/stream-to-file/src/main.rs index 38e88eff05..22653ffcea 100644 --- a/examples/stream-to-file/src/main.rs +++ b/examples/stream-to-file/src/main.rs @@ -5,9 +5,9 @@ //! ``` use axum::{ - body::Bytes, - extract::{BodyStream, Multipart, Path}, - http::StatusCode, + body::{Body, Bytes}, + extract::{Multipart, Path}, + http::{Request, StatusCode}, response::{Html, Redirect}, routing::{get, post}, BoxError, Router, @@ -52,9 +52,9 @@ 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, - body: BodyStream, + request: Request, ) -> Result<(), (StatusCode, String)> { - stream_to_file(&file_name, body).await + stream_to_file(&file_name, request.into_body()).await } // Handler that returns HTML for a multipart form. diff --git a/examples/testing/src/main.rs b/examples/testing/src/main.rs index b2041bef9e..fd429fe068 100644 --- a/examples/testing/src/main.rs +++ b/examples/testing/src/main.rs @@ -140,7 +140,7 @@ mod tests { .request( Request::builder() .uri(format!("http://{}", addr)) - .body(Body::empty()) + .body(hyper::Body::empty()) .unwrap(), ) .await @@ -157,11 +157,21 @@ mod tests { let mut app = app(); let request = Request::builder().uri("/").body(Body::empty()).unwrap(); - let response = app.ready().await.unwrap().call(request).await.unwrap(); + let response = ServiceExt::>::ready(&mut app) + .await + .unwrap() + .call(request) + .await + .unwrap(); assert_eq!(response.status(), StatusCode::OK); let request = Request::builder().uri("/").body(Body::empty()).unwrap(); - let response = app.ready().await.unwrap().call(request).await.unwrap(); + let response = ServiceExt::>::ready(&mut app) + .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 692ee801e0..d30fdca4c0 100644 --- a/examples/validator/src/main.rs +++ b/examples/validator/src/main.rs @@ -12,6 +12,7 @@ use async_trait::async_trait; use axum::{ + body::Body, extract::{rejection::FormRejection, Form, FromRequest}, http::{Request, StatusCode}, response::{Html, IntoResponse, Response}, @@ -61,16 +62,15 @@ async fn handler(ValidatedForm(input): ValidatedForm) -> Html pub struct ValidatedForm(pub T); #[async_trait] -impl FromRequest for ValidatedForm +impl FromRequest for ValidatedForm where T: DeserializeOwned + Validate, S: Send + Sync, - Form: FromRequest, - B: Send + 'static, + Form: FromRequest, { 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))