From 05767b986e2f7067bb035cd972dc9dfe74db99d0 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 5 Nov 2022 10:34:16 +0100 Subject: [PATCH] Remove unneeded bounds on S in FromRequest(Parts) implementations This doesn't actually allow using non-Send or non-Sync state types, but it makes the docs simpler. --- axum-core/src/extract/mod.rs | 3 +- axum-core/src/extract/request_parts.rs | 36 ++++----------- axum-core/src/extract/tuple.rs | 5 +-- axum-extra/src/extract/cached.rs | 7 +-- axum-extra/src/extract/cookie/mod.rs | 7 +-- axum-extra/src/extract/form.rs | 7 ++- axum-extra/src/extract/query.rs | 3 +- axum-extra/src/extract/with_rejection.rs | 7 +-- axum-extra/src/json_lines.rs | 3 +- axum-extra/src/protobuf.rs | 7 ++- axum-macros/src/typed_path.rs | 25 ++++------- .../debug_handler/fail/extract_self_mut.rs | 9 +--- .../debug_handler/fail/extract_self_ref.rs | 9 +--- .../pass/result_impl_into_response.rs | 7 +-- .../tests/debug_handler/pass/self_receiver.rs | 12 ++--- .../tests/debug_handler/pass/set_state.rs | 7 ++- .../from_request/pass/override_rejection.rs | 5 +-- .../pass/override_rejection_parts.rs | 7 +-- axum/src/docs/extract.md | 44 +++++++------------ axum/src/extension.rs | 3 +- axum/src/extract/connect_info.rs | 6 +-- axum/src/extract/host.rs | 7 +-- axum/src/extract/matched_path.rs | 7 +-- axum/src/extract/multipart.rs | 7 ++- axum/src/extract/path/mod.rs | 3 +- axum/src/extract/query.rs | 3 +- axum/src/extract/raw_form.rs | 7 ++- axum/src/extract/raw_query.rs | 7 +-- axum/src/extract/request_parts.rs | 17 +++---- axum/src/extract/ws.rs | 7 +-- axum/src/form.rs | 3 +- axum/src/json.rs | 10 +++-- axum/src/middleware/from_extractor.rs | 14 ++---- axum/src/typed_header.rs | 3 +- .../src/main.rs | 11 ++--- examples/jwt/src/main.rs | 7 +-- .../src/main.rs | 3 +- examples/validator/src/main.rs | 2 +- examples/versioning/src/main.rs | 7 +-- 39 files changed, 112 insertions(+), 232 deletions(-) diff --git a/axum-core/src/extract/mod.rs b/axum-core/src/extract/mod.rs index 83b7e6fe..df0e0d78 100644 --- a/axum-core/src/extract/mod.rs +++ b/axum-core/src/extract/mod.rs @@ -84,8 +84,9 @@ pub trait FromRequestParts: Sized { /// #[async_trait] /// impl FromRequest for MyExtractor /// where -/// // these bounds are required by `async_trait` +/// // this bound is required by `async_trait` /// B: Send + 'static, +/// // this bound is also required if the state parameter is not discarded with `_: &S` /// S: Send + Sync, /// { /// type Rejection = http::StatusCode; diff --git a/axum-core/src/extract/request_parts.rs b/axum-core/src/extract/request_parts.rs index 05d7d727..481fbab5 100644 --- a/axum-core/src/extract/request_parts.rs +++ b/axum-core/src/extract/request_parts.rs @@ -9,7 +9,6 @@ use std::convert::Infallible; impl FromRequest for Request where B: Send, - S: Send + Sync, { type Rejection = Infallible; @@ -19,10 +18,7 @@ where } #[async_trait] -impl FromRequestParts for Method -where - S: Send + Sync, -{ +impl FromRequestParts for Method { type Rejection = Infallible; async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { @@ -31,10 +27,7 @@ where } #[async_trait] -impl FromRequestParts for Uri -where - S: Send + Sync, -{ +impl FromRequestParts for Uri { type Rejection = Infallible; async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { @@ -43,10 +36,7 @@ where } #[async_trait] -impl FromRequestParts for Version -where - S: Send + Sync, -{ +impl FromRequestParts for Version { type Rejection = Infallible; async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { @@ -60,10 +50,7 @@ where /// /// [`TypedHeader`]: https://docs.rs/axum/latest/axum/extract/struct.TypedHeader.html #[async_trait] -impl FromRequestParts for HeaderMap -where - S: Send + Sync, -{ +impl FromRequestParts for HeaderMap { type Rejection = Infallible; async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { @@ -77,7 +64,6 @@ where B: http_body::Body + Send + 'static, B::Data: Send, B::Error: Into, - S: Send + Sync, { type Rejection = BytesRejection; @@ -101,18 +87,13 @@ where B: http_body::Body + Send + 'static, B::Data: Send, B::Error: Into, - S: Send + Sync, { type Rejection = StringRejection; - async fn from_request(req: Request, state: &S) -> Result { - let bytes = Bytes::from_request(req, state) - .await - .map_err(|err| match err { - BytesRejection::FailedToBufferBody(inner) => { - StringRejection::FailedToBufferBody(inner) - } - })?; + async fn from_request(req: Request, _: &S) -> Result { + let bytes: Bytes = req.extract().await.map_err(|err| match err { + BytesRejection::FailedToBufferBody(inner) => StringRejection::FailedToBufferBody(inner), + })?; let string = std::str::from_utf8(&bytes) .map_err(InvalidUtf8::from_err)? @@ -126,7 +107,6 @@ where impl FromRequest for Parts where B: Send + 'static, - S: Send + Sync, { type Rejection = Infallible; diff --git a/axum-core/src/extract/tuple.rs b/axum-core/src/extract/tuple.rs index 728135b2..c15588da 100644 --- a/axum-core/src/extract/tuple.rs +++ b/axum-core/src/extract/tuple.rs @@ -5,10 +5,7 @@ use http::request::{Parts, Request}; use std::convert::Infallible; #[async_trait] -impl FromRequestParts for () -where - S: Send + Sync, -{ +impl FromRequestParts for () { type Rejection = Infallible; async fn from_request_parts(_: &mut Parts, _: &S) -> Result<(), Self::Rejection> { diff --git a/axum-extra/src/extract/cached.rs b/axum-extra/src/extract/cached.rs index 03565c77..b20a2a6c 100644 --- a/axum-extra/src/extract/cached.rs +++ b/axum-extra/src/extract/cached.rs @@ -141,15 +141,12 @@ mod tests { struct Extractor(Instant); #[async_trait] - impl FromRequestParts for Extractor - where - S: Send + Sync, - { + impl FromRequestParts for Extractor { type Rejection = Infallible; async fn from_request_parts( _parts: &mut Parts, - _state: &S, + _: &S, ) -> Result { COUNTER.fetch_add(1, Ordering::SeqCst); Ok(Self(Instant::now())) diff --git a/axum-extra/src/extract/cookie/mod.rs b/axum-extra/src/extract/cookie/mod.rs index cbbbdd1b..4baa8f56 100644 --- a/axum-extra/src/extract/cookie/mod.rs +++ b/axum-extra/src/extract/cookie/mod.rs @@ -89,13 +89,10 @@ pub struct CookieJar { } #[async_trait] -impl FromRequestParts for CookieJar -where - S: Send + Sync, -{ +impl FromRequestParts for CookieJar { type Rejection = Infallible; - async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result { + async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { Ok(Self::from_headers(&parts.headers)) } } diff --git a/axum-extra/src/extract/form.rs b/axum-extra/src/extract/form.rs index 254c7cce..e56ef923 100644 --- a/axum-extra/src/extract/form.rs +++ b/axum-extra/src/extract/form.rs @@ -5,7 +5,7 @@ use axum::{ rejection::{FailedToDeserializeQueryString, FormRejection, InvalidFormContentType}, FromRequest, }, - BoxError, + BoxError, RequestExt, }; use bytes::Bytes; use http::{header, HeaderMap, Method, Request}; @@ -61,11 +61,10 @@ where B: HttpBody + Send + 'static, B::Data: Send, B::Error: Into, - S: Send + Sync, { type Rejection = FormRejection; - async fn from_request(req: Request, state: &S) -> Result { + async fn from_request(req: Request, _: &S) -> Result { if req.method() == Method::GET { let query = req.uri().query().unwrap_or_default(); let value = serde_html_form::from_str(query) @@ -76,7 +75,7 @@ where return Err(InvalidFormContentType::default().into()); } - let bytes = Bytes::from_request(req, state).await?; + let bytes: Bytes = req.extract().await?; let value = serde_html_form::from_bytes(&bytes) .map_err(FailedToDeserializeQueryString::__private_new)?; diff --git a/axum-extra/src/extract/query.rs b/axum-extra/src/extract/query.rs index 4a8d6f86..47d5de19 100644 --- a/axum-extra/src/extract/query.rs +++ b/axum-extra/src/extract/query.rs @@ -62,11 +62,10 @@ pub struct Query(pub T); impl FromRequestParts for Query where T: DeserializeOwned, - S: Send + Sync, { type Rejection = QueryRejection; - async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result { + async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { let query = parts.uri.query().unwrap_or_default(); let value = serde_html_form::from_str(query) .map_err(FailedToDeserializeQueryString::__private_new)?; diff --git a/axum-extra/src/extract/with_rejection.rs b/axum-extra/src/extract/with_rejection.rs index f3a0f04e..bad7b8d3 100644 --- a/axum-extra/src/extract/with_rejection.rs +++ b/axum-extra/src/extract/with_rejection.rs @@ -154,15 +154,12 @@ mod tests { struct TestRejection; #[async_trait] - impl FromRequestParts for TestExtractor - where - S: Send + Sync, - { + impl FromRequestParts for TestExtractor { type Rejection = (); async fn from_request_parts( _parts: &mut Parts, - _state: &S, + _: &S, ) -> Result { Err(()) } diff --git a/axum-extra/src/json_lines.rs b/axum-extra/src/json_lines.rs index 83336311..ffe43285 100644 --- a/axum-extra/src/json_lines.rs +++ b/axum-extra/src/json_lines.rs @@ -106,11 +106,10 @@ where B::Data: Into, B::Error: Into, T: DeserializeOwned, - S: Send + Sync, { type Rejection = Infallible; - async fn from_request(req: Request, _state: &S) -> Result { + async fn from_request(req: Request, _: &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 = BodyStream { diff --git a/axum-extra/src/protobuf.rs b/axum-extra/src/protobuf.rs index ddb70122..db07b1bb 100644 --- a/axum-extra/src/protobuf.rs +++ b/axum-extra/src/protobuf.rs @@ -5,7 +5,7 @@ use axum::{ body::{Bytes, HttpBody}, extract::{rejection::BytesRejection, FromRequest}, response::{IntoResponse, Response}, - BoxError, + BoxError, RequestExt, }; use bytes::BytesMut; use http::{Request, StatusCode}; @@ -103,12 +103,11 @@ where B: HttpBody + Send + 'static, B::Data: Send, B::Error: Into, - S: Send + Sync, { type Rejection = ProtoBufRejection; - async fn from_request(req: Request, state: &S) -> Result { - let mut bytes = Bytes::from_request(req, state).await?; + async fn from_request(req: Request, _: &S) -> Result { + let mut bytes: Bytes = req.extract().await?; match T::decode(&mut bytes) { Ok(value) => Ok(ProtoBuf(value)), diff --git a/axum-macros/src/typed_path.rs b/axum-macros/src/typed_path.rs index 6a50886f..aa27b680 100644 --- a/axum-macros/src/typed_path.rs +++ b/axum-macros/src/typed_path.rs @@ -135,17 +135,14 @@ fn expand_named_fields( let from_request_impl = quote! { #[::axum::async_trait] #[automatically_derived] - impl ::axum::extract::FromRequestParts for #ident - where - S: Send + Sync, - { + impl ::axum::extract::FromRequestParts for #ident { type Rejection = #rejection_assoc_type; async fn from_request_parts( parts: &mut ::axum::http::request::Parts, - state: &S, + _: &S, ) -> ::std::result::Result { - ::axum::extract::Path::from_request_parts(parts, state) + ::axum::extract::Path::from_request_parts(parts, &()) .await .map(|path| path.0) #map_err_rejection @@ -240,17 +237,14 @@ fn expand_unnamed_fields( let from_request_impl = quote! { #[::axum::async_trait] #[automatically_derived] - impl ::axum::extract::FromRequestParts for #ident - where - S: Send + Sync, - { + impl ::axum::extract::FromRequestParts for #ident { type Rejection = #rejection_assoc_type; async fn from_request_parts( parts: &mut ::axum::http::request::Parts, - state: &S, + _: &S, ) -> ::std::result::Result { - ::axum::extract::Path::from_request_parts(parts, state) + ::axum::extract::Path::from_request_parts(parts, &()) .await .map(|path| path.0) #map_err_rejection @@ -324,15 +318,12 @@ fn expand_unit_fields( let from_request_impl = quote! { #[::axum::async_trait] #[automatically_derived] - impl ::axum::extract::FromRequestParts for #ident - where - S: Send + Sync, - { + impl ::axum::extract::FromRequestParts for #ident { type Rejection = #rejection_assoc_type; async fn from_request_parts( parts: &mut ::axum::http::request::Parts, - _state: &S, + _: &S, ) -> ::std::result::Result { if parts.uri.path() == ::PATH { Ok(Self) 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 d20426e2..87ca1450 100644 --- a/axum-macros/tests/debug_handler/fail/extract_self_mut.rs +++ b/axum-macros/tests/debug_handler/fail/extract_self_mut.rs @@ -1,8 +1,4 @@ -use axum::{ - async_trait, - extract::FromRequest, - http::Request, -}; +use axum::{async_trait, extract::FromRequest, http::Request}; use axum_macros::debug_handler; struct A; @@ -11,11 +7,10 @@ struct 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, _: &S) -> Result { unimplemented!() } } 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 77940e29..9523f7b2 100644 --- a/axum-macros/tests/debug_handler/fail/extract_self_ref.rs +++ b/axum-macros/tests/debug_handler/fail/extract_self_ref.rs @@ -1,8 +1,4 @@ -use axum::{ - async_trait, - extract::FromRequest, - http::Request, -}; +use axum::{async_trait, extract::FromRequest, http::Request}; use axum_macros::debug_handler; struct A; @@ -11,11 +7,10 @@ struct 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, _: &S) -> Result { unimplemented!() } } diff --git a/axum-macros/tests/debug_handler/pass/result_impl_into_response.rs b/axum-macros/tests/debug_handler/pass/result_impl_into_response.rs index 782fc930..756b5caa 100644 --- a/axum-macros/tests/debug_handler/pass/result_impl_into_response.rs +++ b/axum-macros/tests/debug_handler/pass/result_impl_into_response.rs @@ -116,13 +116,10 @@ impl A { } #[async_trait] -impl FromRequestParts for A -where - S: Send + Sync, -{ +impl FromRequestParts for A { type Rejection = (); - async fn from_request_parts(_parts: &mut Parts, _state: &S) -> Result { + async fn from_request_parts(_parts: &mut Parts, _: &S) -> Result { unimplemented!() } } diff --git a/axum-macros/tests/debug_handler/pass/self_receiver.rs b/axum-macros/tests/debug_handler/pass/self_receiver.rs index e7bf81ce..92c29fc0 100644 --- a/axum-macros/tests/debug_handler/pass/self_receiver.rs +++ b/axum-macros/tests/debug_handler/pass/self_receiver.rs @@ -1,8 +1,4 @@ -use axum::{ - async_trait, - extract::FromRequest, - http::Request, -}; +use axum::{async_trait, extract::FromRequest, http::Request}; use axum_macros::debug_handler; struct A; @@ -11,11 +7,10 @@ struct 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, _: &S) -> Result { unimplemented!() } } @@ -24,11 +19,10 @@ where 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, _: &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 5c84dbd2..b1284a26 100644 --- a/axum-macros/tests/debug_handler/pass/set_state.rs +++ b/axum-macros/tests/debug_handler/pass/set_state.rs @@ -1,7 +1,7 @@ -use axum_macros::debug_handler; -use axum::extract::{FromRef, FromRequest}; use axum::async_trait; +use axum::extract::{FromRef, FromRequest}; use axum::http::Request; +use axum_macros::debug_handler; #[debug_handler(state = AppState)] async fn handler(_: A) {} @@ -15,12 +15,11 @@ struct 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, _: &S) -> Result { unimplemented!() } } diff --git a/axum-macros/tests/from_request/pass/override_rejection.rs b/axum-macros/tests/from_request/pass/override_rejection.rs index 7167ffe0..a1141039 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}, + http::{Request, StatusCode}, response::{IntoResponse, Response}, routing::get, Extension, Router, @@ -31,12 +31,11 @@ struct 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, _: &S) -> Result { todo!() } } diff --git a/axum-macros/tests/from_request/pass/override_rejection_parts.rs b/axum-macros/tests/from_request/pass/override_rejection_parts.rs index 8e2271c3..7c160cfd 100644 --- a/axum-macros/tests/from_request/pass/override_rejection_parts.rs +++ b/axum-macros/tests/from_request/pass/override_rejection_parts.rs @@ -28,14 +28,11 @@ struct MyExtractor { struct OtherExtractor; #[async_trait] -impl FromRequestParts for OtherExtractor -where - S: Send + Sync, -{ +impl FromRequestParts for OtherExtractor { // this rejection doesn't implement `Display` and `Error` type Rejection = (StatusCode, String); - async fn from_request_parts(_parts: &mut Parts, _state: &S) -> Result { + async fn from_request_parts(_parts: &mut Parts, _: &S) -> Result { todo!() } } diff --git a/axum/src/docs/extract.md b/axum/src/docs/extract.md index e002a8b7..92b4f2b5 100644 --- a/axum/src/docs/extract.md +++ b/axum/src/docs/extract.md @@ -424,13 +424,10 @@ use axum::{ struct ExtractUserAgent(HeaderValue); #[async_trait] -impl FromRequestParts for ExtractUserAgent -where - S: Send + Sync, -{ +impl FromRequestParts for ExtractUserAgent { type Rejection = (StatusCode, &'static str); - async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { + async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { if let Some(user_agent) = parts.headers.get(USER_AGENT) { Ok(ExtractUserAgent(user_agent.clone())) } else { @@ -456,16 +453,15 @@ If your extractor needs to consume the request body you must implement [`FromReq ```rust,no_run use axum::{ async_trait, - extract::FromRequest, - response::{Response, IntoResponse}, body::Bytes, - routing::get, - Router, + extract::FromRequest, http::{ - StatusCode, header::{HeaderValue, USER_AGENT}, - Request, + Request, StatusCode, }, + response::{IntoResponse, Response}, + routing::get, + RequestExt, Router, }; struct ValidatedBody(Bytes); @@ -475,14 +471,11 @@ impl FromRequest for ValidatedBody where Bytes: FromRequest, B: Send + 'static, - S: Send + Sync, { type Rejection = Response; - async fn from_request(req: Request, state: &S) -> Result { - let body = Bytes::from_request(req, state) - .await - .map_err(IntoResponse::into_response)?; + async fn from_request(req: Request, _: &S) -> Result { + let body: Bytes = req.extract().await.map_err(IntoResponse::into_response)?; // do validation... @@ -523,12 +516,11 @@ struct MyExtractor; #[async_trait] impl FromRequest for MyExtractor where - S: Send + Sync, B: Send + 'static, { type Rejection = Infallible; - async fn from_request(req: Request, state: &S) -> Result { + async fn from_request(req: Request, _: &S) -> Result { // ... # todo!() } @@ -536,13 +528,10 @@ where // and `FromRequestParts` #[async_trait] -impl FromRequestParts for MyExtractor -where - S: Send + Sync, -{ +impl FromRequestParts for MyExtractor { type Rejection = Infallible; - async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { + async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { // ... # todo!() } @@ -588,16 +577,13 @@ struct AuthenticatedUser { } #[async_trait] -impl FromRequestParts for AuthenticatedUser -where - S: Send + Sync, -{ +impl FromRequestParts for AuthenticatedUser { type Rejection = Response; - async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { + async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { // You can either call them directly... let TypedHeader(Authorization(token)) = - TypedHeader::>::from_request_parts(parts, state) + TypedHeader::>::from_request_parts(parts, &()) .await .map_err(|err| err.into_response())?; diff --git a/axum/src/extension.rs b/axum/src/extension.rs index 575d62ca..275e210d 100644 --- a/axum/src/extension.rs +++ b/axum/src/extension.rs @@ -76,11 +76,10 @@ pub struct Extension(pub T); impl FromRequestParts for Extension where T: Clone + Send + Sync + 'static, - S: Send + Sync, { type Rejection = ExtensionRejection; - async fn from_request_parts(req: &mut Parts, _state: &S) -> Result { + async fn from_request_parts(req: &mut Parts, _: &S) -> Result { let value = req .extensions .get::() diff --git a/axum/src/extract/connect_info.rs b/axum/src/extract/connect_info.rs index fff2b46f..29f911fa 100644 --- a/axum/src/extract/connect_info.rs +++ b/axum/src/extract/connect_info.rs @@ -7,6 +7,7 @@ use super::{Extension, FromRequestParts}; use crate::middleware::AddExtension; use async_trait::async_trait; +use axum_core::RequestPartsExt; use http::request::Parts; use hyper::server::conn::AddrStream; use std::{ @@ -131,13 +132,12 @@ pub struct ConnectInfo(pub T); #[async_trait] impl FromRequestParts for ConnectInfo where - S: Send + Sync, T: Clone + Send + Sync + 'static, { type Rejection = as FromRequestParts>::Rejection; - async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { - let Extension(connect_info) = Extension::::from_request_parts(parts, state).await?; + async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { + let Extension(connect_info): Extension = parts.extract().await?; Ok(connect_info) } } diff --git a/axum/src/extract/host.rs b/axum/src/extract/host.rs index f92a6227..f16a4b3d 100644 --- a/axum/src/extract/host.rs +++ b/axum/src/extract/host.rs @@ -24,13 +24,10 @@ const X_FORWARDED_HOST_HEADER_KEY: &str = "X-Forwarded-Host"; pub struct Host(pub String); #[async_trait] -impl FromRequestParts for Host -where - S: Send + Sync, -{ +impl FromRequestParts for Host { type Rejection = HostRejection; - async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result { + async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { if let Some(host) = parse_forwarded(&parts.headers) { return Ok(Host(host.to_owned())); } diff --git a/axum/src/extract/matched_path.rs b/axum/src/extract/matched_path.rs index fa4d50b3..adaccaf6 100644 --- a/axum/src/extract/matched_path.rs +++ b/axum/src/extract/matched_path.rs @@ -65,13 +65,10 @@ impl MatchedPath { } #[async_trait] -impl FromRequestParts for MatchedPath -where - S: Send + Sync, -{ +impl FromRequestParts for MatchedPath { type Rejection = MatchedPathRejection; - async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result { + async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { let matched_path = parts .extensions .get::() diff --git a/axum/src/extract/multipart.rs b/axum/src/extract/multipart.rs index 76dd52de..9e78b50f 100644 --- a/axum/src/extract/multipart.rs +++ b/axum/src/extract/multipart.rs @@ -60,15 +60,14 @@ where B: HttpBody + Send + 'static, B::Data: Into, B::Error: Into, - S: Send + Sync, { type Rejection = MultipartRejection; - async fn from_request(req: Request, state: &S) -> Result { + async fn from_request(req: Request, _: &S) -> Result { let boundary = parse_boundary(req.headers()).ok_or(InvalidBoundary)?; let stream_result = match req.with_limited_body() { - Ok(limited) => BodyStream::from_request(limited, state).await, - Err(unlimited) => BodyStream::from_request(unlimited, state).await, + Ok(limited) => limited.extract::().await, + Err(unlimited) => unlimited.extract::().await, }; let stream = stream_result.unwrap_or_else(|err| match err {}); let multipart = multer::Multipart::new(stream, boundary); diff --git a/axum/src/extract/path/mod.rs b/axum/src/extract/path/mod.rs index ad4e5eb4..937f83f0 100644 --- a/axum/src/extract/path/mod.rs +++ b/axum/src/extract/path/mod.rs @@ -170,11 +170,10 @@ impl DerefMut for Path { impl FromRequestParts for Path where T: DeserializeOwned + Send, - S: Send + Sync, { type Rejection = PathRejection; - async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result { + async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { let params = match parts.extensions.get::() { Some(UrlParams::Params(params)) => params, Some(UrlParams::InvalidUtf8InPathParam { key }) => { diff --git a/axum/src/extract/query.rs b/axum/src/extract/query.rs index a520853a..4d47984d 100644 --- a/axum/src/extract/query.rs +++ b/axum/src/extract/query.rs @@ -53,11 +53,10 @@ pub struct Query(pub T); impl FromRequestParts for Query where T: DeserializeOwned, - S: Send + Sync, { type Rejection = QueryRejection; - async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result { + async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { let query = parts.uri.query().unwrap_or_default(); let value = serde_urlencoded::from_str(query) .map_err(FailedToDeserializeQueryString::__private_new)?; diff --git a/axum/src/extract/raw_form.rs b/axum/src/extract/raw_form.rs index 3f9f67f6..dc89dc59 100644 --- a/axum/src/extract/raw_form.rs +++ b/axum/src/extract/raw_form.rs @@ -1,5 +1,5 @@ use async_trait::async_trait; -use axum_core::extract::FromRequest; +use axum_core::{extract::FromRequest, RequestExt}; use bytes::{Bytes, BytesMut}; use http::{Method, Request}; @@ -40,11 +40,10 @@ where B: HttpBody + Send + 'static, B::Data: Send, B::Error: Into, - S: Send + Sync, { type Rejection = RawFormRejection; - async fn from_request(req: Request, state: &S) -> Result { + async fn from_request(req: Request, _: &S) -> Result { if req.method() == Method::GET { let mut bytes = BytesMut::new(); @@ -58,7 +57,7 @@ where return Err(InvalidFormContentType.into()); } - Ok(Self(Bytes::from_request(req, state).await?)) + Ok(Self(req.extract::().await?)) } } } diff --git a/axum/src/extract/raw_query.rs b/axum/src/extract/raw_query.rs index 0e507cfc..1ca8989c 100644 --- a/axum/src/extract/raw_query.rs +++ b/axum/src/extract/raw_query.rs @@ -28,13 +28,10 @@ use std::convert::Infallible; pub struct RawQuery(pub Option); #[async_trait] -impl FromRequestParts for RawQuery -where - S: Send + Sync, -{ +impl FromRequestParts for RawQuery { type Rejection = Infallible; - async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result { + async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { let query = parts.uri.query().map(|query| query.to_owned()); Ok(Self(query)) } diff --git a/axum/src/extract/request_parts.rs b/axum/src/extract/request_parts.rs index 933e7a71..5e323c3c 100644 --- a/axum/src/extract/request_parts.rs +++ b/axum/src/extract/request_parts.rs @@ -4,6 +4,7 @@ use crate::{ BoxError, Error, }; use async_trait::async_trait; +use axum_core::RequestPartsExt; use futures_util::stream::Stream; use http::{request::Parts, Request, Uri}; use std::{ @@ -86,14 +87,12 @@ pub struct OriginalUri(pub Uri); #[cfg(feature = "original-uri")] #[async_trait] -impl FromRequestParts for OriginalUri -where - S: Send + Sync, -{ +impl FromRequestParts for OriginalUri { type Rejection = Infallible; - async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { - let uri = Extension::::from_request_parts(parts, state) + async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { + let uri = parts + .extract::>() .await .unwrap_or_else(|_| Extension(OriginalUri(parts.uri.clone()))) .0; @@ -151,11 +150,10 @@ where B: HttpBody + Send + 'static, B::Data: Into, B::Error: Into, - S: Send + Sync, { type Rejection = Infallible; - async fn from_request(req: Request, _state: &S) -> Result { + async fn from_request(req: Request, _: &S) -> Result { let body = req .into_body() .map_data(Into::into) @@ -212,11 +210,10 @@ pub struct RawBody(pub B); impl FromRequest for RawBody where B: Send, - S: Send + Sync, { type Rejection = Infallible; - async fn from_request(req: Request, _state: &S) -> Result { + async fn from_request(req: Request, _: &S) -> Result { Ok(Self(req.into_body())) } } diff --git a/axum/src/extract/ws.rs b/axum/src/extract/ws.rs index dbc38905..31befe92 100644 --- a/axum/src/extract/ws.rs +++ b/axum/src/extract/ws.rs @@ -276,13 +276,10 @@ impl WebSocketUpgrade { } #[async_trait] -impl FromRequestParts for WebSocketUpgrade -where - S: Send + Sync, -{ +impl FromRequestParts for WebSocketUpgrade { type Rejection = WebSocketUpgradeRejection; - async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result { + async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { if parts.method != Method::GET { return Err(MethodNotGet.into()); } diff --git a/axum/src/form.rs b/axum/src/form.rs index 3ead68a4..922a905b 100644 --- a/axum/src/form.rs +++ b/axum/src/form.rs @@ -68,11 +68,10 @@ where B: HttpBody + Send + 'static, B::Data: Send, B::Error: Into, - S: Send + Sync, { type Rejection = FormRejection; - async fn from_request(req: Request, _state: &S) -> Result { + async fn from_request(req: Request, _: &S) -> Result { match req.extract().await { Ok(RawForm(bytes)) => { let value = serde_urlencoded::from_bytes(&bytes) diff --git a/axum/src/json.rs b/axum/src/json.rs index ca52e069..f820ce74 100644 --- a/axum/src/json.rs +++ b/axum/src/json.rs @@ -4,7 +4,10 @@ use crate::{ BoxError, }; use async_trait::async_trait; -use axum_core::response::{IntoResponse, Response}; +use axum_core::{ + response::{IntoResponse, Response}, + RequestExt, +}; use bytes::{BufMut, BytesMut}; use http::{ header::{self, HeaderMap, HeaderValue}, @@ -106,13 +109,12 @@ where B: HttpBody + Send + 'static, B::Data: Send, B::Error: Into, - S: Send + Sync, { type Rejection = JsonRejection; - async fn from_request(req: Request, state: &S) -> Result { + async fn from_request(req: Request, _: &S) -> Result { if json_content_type(req.headers()) { - let bytes = Bytes::from_request(req, state).await?; + let bytes: Bytes = req.extract().await?; let deserializer = &mut serde_json::Deserializer::from_slice(&bytes); let value = match serde_path_to_error::deserialize(deserializer) { diff --git a/axum/src/middleware/from_extractor.rs b/axum/src/middleware/from_extractor.rs index a6822af8..03750929 100644 --- a/axum/src/middleware/from_extractor.rs +++ b/axum/src/middleware/from_extractor.rs @@ -45,13 +45,10 @@ use tower_service::Service; /// struct RequireAuth; /// /// #[async_trait] -/// impl FromRequestParts for RequireAuth -/// where -/// S: Send + Sync, -/// { +/// impl FromRequestParts for RequireAuth { /// type Rejection = StatusCode; /// -/// async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { +/// async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { /// let auth_header = parts /// .headers /// .get(header::AUTHORIZATION) @@ -370,15 +367,12 @@ mod tests { struct MyExtractor; #[async_trait] - impl FromRequestParts for MyExtractor - where - S: Send + Sync, - { + impl FromRequestParts for MyExtractor { type Rejection = std::convert::Infallible; async fn from_request_parts( _parts: &mut Parts, - _state: &S, + _: &S, ) -> Result { unimplemented!() } diff --git a/axum/src/typed_header.rs b/axum/src/typed_header.rs index 87805a82..e5cf8cd1 100644 --- a/axum/src/typed_header.rs +++ b/axum/src/typed_header.rs @@ -56,11 +56,10 @@ pub struct TypedHeader(pub T); impl FromRequestParts for TypedHeader where T: headers::Header, - S: Send + Sync, { type Rejection = TypedHeaderRejection; - async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result { + async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { match parts.headers.typed_try_get::() { Ok(Some(value)) => Ok(Self(value)), Ok(None) => Err(TypedHeaderRejection { 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 b3363d99..d098de33 100644 --- a/examples/consume-body-in-extractor-or-middleware/src/main.rs +++ b/examples/consume-body-in-extractor-or-middleware/src/main.rs @@ -81,16 +81,11 @@ struct BufferRequestBody(Bytes); // we must implement `FromRequest` (and not `FromRequestParts`) to consume the body #[async_trait] -impl FromRequest for BufferRequestBody -where - S: Send + Sync, -{ +impl FromRequest for BufferRequestBody { type Rejection = Response; - async fn from_request(req: Request, state: &S) -> Result { - let body = Bytes::from_request(req, state) - .await - .map_err(|err| err.into_response())?; + async fn from_request(req: Request, _: &S) -> Result { + let body: Bytes = req.extract().await.map_err(|err| err.into_response())?; do_thing_with_request_body(body.clone()); diff --git a/examples/jwt/src/main.rs b/examples/jwt/src/main.rs index 82633d96..1e45f724 100644 --- a/examples/jwt/src/main.rs +++ b/examples/jwt/src/main.rs @@ -122,13 +122,10 @@ impl AuthBody { } #[async_trait] -impl FromRequestParts for Claims -where - S: Send + Sync, -{ +impl FromRequestParts for Claims { type Rejection = AuthError; - async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result { + async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { // Extract the token from the authorization header let TypedHeader(Authorization(bearer)) = parts .extract::>>() 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 9167d1e0..582147da 100644 --- a/examples/parse-body-based-on-content-type/src/main.rs +++ b/examples/parse-body-based-on-content-type/src/main.rs @@ -60,7 +60,6 @@ enum JsonOrForm { impl FromRequest for JsonOrForm where B: Send + 'static, - S: Send + Sync, Json: FromRequest<(), B>, Form: FromRequest<(), B>, T: 'static, @@ -68,7 +67,7 @@ where { type Rejection = Response; - async fn from_request(req: Request, _state: &S) -> Result { + async fn from_request(req: Request, _: &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/validator/src/main.rs b/examples/validator/src/main.rs index 359be614..14c0a31b 100644 --- a/examples/validator/src/main.rs +++ b/examples/validator/src/main.rs @@ -70,7 +70,7 @@ where type Rejection = ServerError; async fn from_request(req: Request, state: &S) -> Result { - let Form(value) = Form::::from_request(req, state).await?; + let Form(value): Form = req.extract().await?; value.validate()?; Ok(ValidatedForm(value)) } diff --git a/examples/versioning/src/main.rs b/examples/versioning/src/main.rs index 6948eb1d..ed1e2f1b 100644 --- a/examples/versioning/src/main.rs +++ b/examples/versioning/src/main.rs @@ -48,13 +48,10 @@ enum Version { } #[async_trait] -impl FromRequestParts for Version -where - S: Send + Sync, -{ +impl FromRequestParts for Version { type Rejection = Response; - async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result { + async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { let params: Path> = parts.extract().await.map_err(IntoResponse::into_response)?;