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.
This commit is contained in:
Jonas Platte
2022-11-05 11:04:56 +01:00
parent da058db509
commit 05767b986e
39 changed files with 112 additions and 232 deletions
+2 -1
View File
@@ -84,8 +84,9 @@ pub trait FromRequestParts<S>: Sized {
/// #[async_trait] /// #[async_trait]
/// impl<S, B> FromRequest<S, B> for MyExtractor /// impl<S, B> FromRequest<S, B> for MyExtractor
/// where /// where
/// // these bounds are required by `async_trait` /// // this bound is required by `async_trait`
/// B: Send + 'static, /// B: Send + 'static,
/// // this bound is also required if the state parameter is not discarded with `_: &S`
/// S: Send + Sync, /// S: Send + Sync,
/// { /// {
/// type Rejection = http::StatusCode; /// type Rejection = http::StatusCode;
+8 -28
View File
@@ -9,7 +9,6 @@ use std::convert::Infallible;
impl<S, B> FromRequest<S, B> for Request<B> impl<S, B> FromRequest<S, B> for Request<B>
where where
B: Send, B: Send,
S: Send + Sync,
{ {
type Rejection = Infallible; type Rejection = Infallible;
@@ -19,10 +18,7 @@ where
} }
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for Method impl<S> FromRequestParts<S> for Method {
where
S: Send + Sync,
{
type Rejection = Infallible; type Rejection = Infallible;
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
@@ -31,10 +27,7 @@ where
} }
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for Uri impl<S> FromRequestParts<S> for Uri {
where
S: Send + Sync,
{
type Rejection = Infallible; type Rejection = Infallible;
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
@@ -43,10 +36,7 @@ where
} }
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for Version impl<S> FromRequestParts<S> for Version {
where
S: Send + Sync,
{
type Rejection = Infallible; type Rejection = Infallible;
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
@@ -60,10 +50,7 @@ where
/// ///
/// [`TypedHeader`]: https://docs.rs/axum/latest/axum/extract/struct.TypedHeader.html /// [`TypedHeader`]: https://docs.rs/axum/latest/axum/extract/struct.TypedHeader.html
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for HeaderMap impl<S> FromRequestParts<S> for HeaderMap {
where
S: Send + Sync,
{
type Rejection = Infallible; type Rejection = Infallible;
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
@@ -77,7 +64,6 @@ where
B: http_body::Body + Send + 'static, B: http_body::Body + Send + 'static,
B::Data: Send, B::Data: Send,
B::Error: Into<BoxError>, B::Error: Into<BoxError>,
S: Send + Sync,
{ {
type Rejection = BytesRejection; type Rejection = BytesRejection;
@@ -101,18 +87,13 @@ where
B: http_body::Body + Send + 'static, B: http_body::Body + Send + 'static,
B::Data: Send, B::Data: Send,
B::Error: Into<BoxError>, B::Error: Into<BoxError>,
S: Send + Sync,
{ {
type Rejection = StringRejection; type Rejection = StringRejection;
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> { async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
let bytes = Bytes::from_request(req, state) let bytes: Bytes = req.extract().await.map_err(|err| match err {
.await BytesRejection::FailedToBufferBody(inner) => StringRejection::FailedToBufferBody(inner),
.map_err(|err| match err { })?;
BytesRejection::FailedToBufferBody(inner) => {
StringRejection::FailedToBufferBody(inner)
}
})?;
let string = std::str::from_utf8(&bytes) let string = std::str::from_utf8(&bytes)
.map_err(InvalidUtf8::from_err)? .map_err(InvalidUtf8::from_err)?
@@ -126,7 +107,6 @@ where
impl<S, B> FromRequest<S, B> for Parts impl<S, B> FromRequest<S, B> for Parts
where where
B: Send + 'static, B: Send + 'static,
S: Send + Sync,
{ {
type Rejection = Infallible; type Rejection = Infallible;
+1 -4
View File
@@ -5,10 +5,7 @@ use http::request::{Parts, Request};
use std::convert::Infallible; use std::convert::Infallible;
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for () impl<S> FromRequestParts<S> for () {
where
S: Send + Sync,
{
type Rejection = Infallible; type Rejection = Infallible;
async fn from_request_parts(_: &mut Parts, _: &S) -> Result<(), Self::Rejection> { async fn from_request_parts(_: &mut Parts, _: &S) -> Result<(), Self::Rejection> {
+2 -5
View File
@@ -141,15 +141,12 @@ mod tests {
struct Extractor(Instant); struct Extractor(Instant);
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for Extractor impl<S> FromRequestParts<S> for Extractor {
where
S: Send + Sync,
{
type Rejection = Infallible; type Rejection = Infallible;
async fn from_request_parts( async fn from_request_parts(
_parts: &mut Parts, _parts: &mut Parts,
_state: &S, _: &S,
) -> Result<Self, Self::Rejection> { ) -> Result<Self, Self::Rejection> {
COUNTER.fetch_add(1, Ordering::SeqCst); COUNTER.fetch_add(1, Ordering::SeqCst);
Ok(Self(Instant::now())) Ok(Self(Instant::now()))
+2 -5
View File
@@ -89,13 +89,10 @@ pub struct CookieJar {
} }
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for CookieJar impl<S> FromRequestParts<S> for CookieJar {
where
S: Send + Sync,
{
type Rejection = Infallible; type Rejection = Infallible;
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
Ok(Self::from_headers(&parts.headers)) Ok(Self::from_headers(&parts.headers))
} }
} }
+3 -4
View File
@@ -5,7 +5,7 @@ use axum::{
rejection::{FailedToDeserializeQueryString, FormRejection, InvalidFormContentType}, rejection::{FailedToDeserializeQueryString, FormRejection, InvalidFormContentType},
FromRequest, FromRequest,
}, },
BoxError, BoxError, RequestExt,
}; };
use bytes::Bytes; use bytes::Bytes;
use http::{header, HeaderMap, Method, Request}; use http::{header, HeaderMap, Method, Request};
@@ -61,11 +61,10 @@ where
B: HttpBody + Send + 'static, B: HttpBody + Send + 'static,
B::Data: Send, B::Data: Send,
B::Error: Into<BoxError>, B::Error: Into<BoxError>,
S: Send + Sync,
{ {
type Rejection = FormRejection; type Rejection = FormRejection;
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> { async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
if req.method() == Method::GET { if req.method() == Method::GET {
let query = req.uri().query().unwrap_or_default(); let query = req.uri().query().unwrap_or_default();
let value = serde_html_form::from_str(query) let value = serde_html_form::from_str(query)
@@ -76,7 +75,7 @@ where
return Err(InvalidFormContentType::default().into()); 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) let value = serde_html_form::from_bytes(&bytes)
.map_err(FailedToDeserializeQueryString::__private_new)?; .map_err(FailedToDeserializeQueryString::__private_new)?;
+1 -2
View File
@@ -62,11 +62,10 @@ pub struct Query<T>(pub T);
impl<T, S> FromRequestParts<S> for Query<T> impl<T, S> FromRequestParts<S> for Query<T>
where where
T: DeserializeOwned, T: DeserializeOwned,
S: Send + Sync,
{ {
type Rejection = QueryRejection; type Rejection = QueryRejection;
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
let query = parts.uri.query().unwrap_or_default(); let query = parts.uri.query().unwrap_or_default();
let value = serde_html_form::from_str(query) let value = serde_html_form::from_str(query)
.map_err(FailedToDeserializeQueryString::__private_new)?; .map_err(FailedToDeserializeQueryString::__private_new)?;
+2 -5
View File
@@ -154,15 +154,12 @@ mod tests {
struct TestRejection; struct TestRejection;
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for TestExtractor impl<S> FromRequestParts<S> for TestExtractor {
where
S: Send + Sync,
{
type Rejection = (); type Rejection = ();
async fn from_request_parts( async fn from_request_parts(
_parts: &mut Parts, _parts: &mut Parts,
_state: &S, _: &S,
) -> Result<Self, Self::Rejection> { ) -> Result<Self, Self::Rejection> {
Err(()) Err(())
} }
+1 -2
View File
@@ -106,11 +106,10 @@ where
B::Data: Into<Bytes>, B::Data: Into<Bytes>,
B::Error: Into<BoxError>, B::Error: Into<BoxError>,
T: DeserializeOwned, T: DeserializeOwned,
S: Send + Sync,
{ {
type Rejection = Infallible; type Rejection = Infallible;
async fn from_request(req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
// `Stream::lines` isn't a thing so we have to convert it into an `AsyncRead` // `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` // so we can call `AsyncRead::lines` and then convert it back to a `Stream`
let body = BodyStream { let body = BodyStream {
+3 -4
View File
@@ -5,7 +5,7 @@ use axum::{
body::{Bytes, HttpBody}, body::{Bytes, HttpBody},
extract::{rejection::BytesRejection, FromRequest}, extract::{rejection::BytesRejection, FromRequest},
response::{IntoResponse, Response}, response::{IntoResponse, Response},
BoxError, BoxError, RequestExt,
}; };
use bytes::BytesMut; use bytes::BytesMut;
use http::{Request, StatusCode}; use http::{Request, StatusCode};
@@ -103,12 +103,11 @@ where
B: HttpBody + Send + 'static, B: HttpBody + Send + 'static,
B::Data: Send, B::Data: Send,
B::Error: Into<BoxError>, B::Error: Into<BoxError>,
S: Send + Sync,
{ {
type Rejection = ProtoBufRejection; type Rejection = ProtoBufRejection;
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> { async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
let mut bytes = Bytes::from_request(req, state).await?; let mut bytes: Bytes = req.extract().await?;
match T::decode(&mut bytes) { match T::decode(&mut bytes) {
Ok(value) => Ok(ProtoBuf(value)), Ok(value) => Ok(ProtoBuf(value)),
+8 -17
View File
@@ -135,17 +135,14 @@ fn expand_named_fields(
let from_request_impl = quote! { let from_request_impl = quote! {
#[::axum::async_trait] #[::axum::async_trait]
#[automatically_derived] #[automatically_derived]
impl<S> ::axum::extract::FromRequestParts<S> for #ident impl<S> ::axum::extract::FromRequestParts<S> for #ident {
where
S: Send + Sync,
{
type Rejection = #rejection_assoc_type; type Rejection = #rejection_assoc_type;
async fn from_request_parts( async fn from_request_parts(
parts: &mut ::axum::http::request::Parts, parts: &mut ::axum::http::request::Parts,
state: &S, _: &S,
) -> ::std::result::Result<Self, Self::Rejection> { ) -> ::std::result::Result<Self, Self::Rejection> {
::axum::extract::Path::from_request_parts(parts, state) ::axum::extract::Path::from_request_parts(parts, &())
.await .await
.map(|path| path.0) .map(|path| path.0)
#map_err_rejection #map_err_rejection
@@ -240,17 +237,14 @@ fn expand_unnamed_fields(
let from_request_impl = quote! { let from_request_impl = quote! {
#[::axum::async_trait] #[::axum::async_trait]
#[automatically_derived] #[automatically_derived]
impl<S> ::axum::extract::FromRequestParts<S> for #ident impl<S> ::axum::extract::FromRequestParts<S> for #ident {
where
S: Send + Sync,
{
type Rejection = #rejection_assoc_type; type Rejection = #rejection_assoc_type;
async fn from_request_parts( async fn from_request_parts(
parts: &mut ::axum::http::request::Parts, parts: &mut ::axum::http::request::Parts,
state: &S, _: &S,
) -> ::std::result::Result<Self, Self::Rejection> { ) -> ::std::result::Result<Self, Self::Rejection> {
::axum::extract::Path::from_request_parts(parts, state) ::axum::extract::Path::from_request_parts(parts, &())
.await .await
.map(|path| path.0) .map(|path| path.0)
#map_err_rejection #map_err_rejection
@@ -324,15 +318,12 @@ fn expand_unit_fields(
let from_request_impl = quote! { let from_request_impl = quote! {
#[::axum::async_trait] #[::axum::async_trait]
#[automatically_derived] #[automatically_derived]
impl<S> ::axum::extract::FromRequestParts<S> for #ident impl<S> ::axum::extract::FromRequestParts<S> for #ident {
where
S: Send + Sync,
{
type Rejection = #rejection_assoc_type; type Rejection = #rejection_assoc_type;
async fn from_request_parts( async fn from_request_parts(
parts: &mut ::axum::http::request::Parts, parts: &mut ::axum::http::request::Parts,
_state: &S, _: &S,
) -> ::std::result::Result<Self, Self::Rejection> { ) -> ::std::result::Result<Self, Self::Rejection> {
if parts.uri.path() == <Self as ::axum_extra::routing::TypedPath>::PATH { if parts.uri.path() == <Self as ::axum_extra::routing::TypedPath>::PATH {
Ok(Self) Ok(Self)
@@ -1,8 +1,4 @@
use axum::{ use axum::{async_trait, extract::FromRequest, http::Request};
async_trait,
extract::FromRequest,
http::Request,
};
use axum_macros::debug_handler; use axum_macros::debug_handler;
struct A; struct A;
@@ -11,11 +7,10 @@ struct A;
impl<S, B> FromRequest<S, B> for A impl<S, B> FromRequest<S, B> for A
where where
B: Send + 'static, B: Send + 'static,
S: Send + Sync,
{ {
type Rejection = (); type Rejection = ();
async fn from_request(_req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request(_req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
unimplemented!() unimplemented!()
} }
} }
@@ -1,8 +1,4 @@
use axum::{ use axum::{async_trait, extract::FromRequest, http::Request};
async_trait,
extract::FromRequest,
http::Request,
};
use axum_macros::debug_handler; use axum_macros::debug_handler;
struct A; struct A;
@@ -11,11 +7,10 @@ struct A;
impl<S, B> FromRequest<S, B> for A impl<S, B> FromRequest<S, B> for A
where where
B: Send + 'static, B: Send + 'static,
S: Send + Sync,
{ {
type Rejection = (); type Rejection = ();
async fn from_request(_req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request(_req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
unimplemented!() unimplemented!()
} }
} }
@@ -116,13 +116,10 @@ impl A {
} }
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for A impl<S> FromRequestParts<S> for A {
where
S: Send + Sync,
{
type Rejection = (); type Rejection = ();
async fn from_request_parts(_parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(_parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
unimplemented!() unimplemented!()
} }
} }
@@ -1,8 +1,4 @@
use axum::{ use axum::{async_trait, extract::FromRequest, http::Request};
async_trait,
extract::FromRequest,
http::Request,
};
use axum_macros::debug_handler; use axum_macros::debug_handler;
struct A; struct A;
@@ -11,11 +7,10 @@ struct A;
impl<S, B> FromRequest<S, B> for A impl<S, B> FromRequest<S, B> for A
where where
B: Send + 'static, B: Send + 'static,
S: Send + Sync,
{ {
type Rejection = (); type Rejection = ();
async fn from_request(_req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request(_req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
unimplemented!() unimplemented!()
} }
} }
@@ -24,11 +19,10 @@ where
impl<S, B> FromRequest<S, B> for Box<A> impl<S, B> FromRequest<S, B> for Box<A>
where where
B: Send + 'static, B: Send + 'static,
S: Send + Sync,
{ {
type Rejection = (); type Rejection = ();
async fn from_request(_req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request(_req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
unimplemented!() unimplemented!()
} }
} }
@@ -1,7 +1,7 @@
use axum_macros::debug_handler;
use axum::extract::{FromRef, FromRequest};
use axum::async_trait; use axum::async_trait;
use axum::extract::{FromRef, FromRequest};
use axum::http::Request; use axum::http::Request;
use axum_macros::debug_handler;
#[debug_handler(state = AppState)] #[debug_handler(state = AppState)]
async fn handler(_: A) {} async fn handler(_: A) {}
@@ -15,12 +15,11 @@ struct A;
impl<S, B> FromRequest<S, B> for A impl<S, B> FromRequest<S, B> for A
where where
B: Send + 'static, B: Send + 'static,
S: Send + Sync,
AppState: FromRef<S>, AppState: FromRef<S>,
{ {
type Rejection = (); type Rejection = ();
async fn from_request(_req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request(_req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
unimplemented!() unimplemented!()
} }
} }
@@ -1,7 +1,7 @@
use axum::{ use axum::{
async_trait, async_trait,
extract::{rejection::ExtensionRejection, FromRequest}, extract::{rejection::ExtensionRejection, FromRequest},
http::{StatusCode, Request}, http::{Request, StatusCode},
response::{IntoResponse, Response}, response::{IntoResponse, Response},
routing::get, routing::get,
Extension, Router, Extension, Router,
@@ -31,12 +31,11 @@ struct OtherExtractor;
impl<S, B> FromRequest<S, B> for OtherExtractor impl<S, B> FromRequest<S, B> for OtherExtractor
where where
B: Send + 'static, B: Send + 'static,
S: Send + Sync,
{ {
// this rejection doesn't implement `Display` and `Error` // this rejection doesn't implement `Display` and `Error`
type Rejection = (StatusCode, String); type Rejection = (StatusCode, String);
async fn from_request(_req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request(_req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
todo!() todo!()
} }
} }
@@ -28,14 +28,11 @@ struct MyExtractor {
struct OtherExtractor; struct OtherExtractor;
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for OtherExtractor impl<S> FromRequestParts<S> for OtherExtractor {
where
S: Send + Sync,
{
// this rejection doesn't implement `Display` and `Error` // this rejection doesn't implement `Display` and `Error`
type Rejection = (StatusCode, String); type Rejection = (StatusCode, String);
async fn from_request_parts(_parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(_parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
todo!() todo!()
} }
} }
+15 -29
View File
@@ -424,13 +424,10 @@ use axum::{
struct ExtractUserAgent(HeaderValue); struct ExtractUserAgent(HeaderValue);
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for ExtractUserAgent impl<S> FromRequestParts<S> for ExtractUserAgent {
where
S: Send + Sync,
{
type Rejection = (StatusCode, &'static str); type Rejection = (StatusCode, &'static str);
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
if let Some(user_agent) = parts.headers.get(USER_AGENT) { if let Some(user_agent) = parts.headers.get(USER_AGENT) {
Ok(ExtractUserAgent(user_agent.clone())) Ok(ExtractUserAgent(user_agent.clone()))
} else { } else {
@@ -456,16 +453,15 @@ If your extractor needs to consume the request body you must implement [`FromReq
```rust,no_run ```rust,no_run
use axum::{ use axum::{
async_trait, async_trait,
extract::FromRequest,
response::{Response, IntoResponse},
body::Bytes, body::Bytes,
routing::get, extract::FromRequest,
Router,
http::{ http::{
StatusCode,
header::{HeaderValue, USER_AGENT}, header::{HeaderValue, USER_AGENT},
Request, Request, StatusCode,
}, },
response::{IntoResponse, Response},
routing::get,
RequestExt, Router,
}; };
struct ValidatedBody(Bytes); struct ValidatedBody(Bytes);
@@ -475,14 +471,11 @@ impl<S, B> FromRequest<S, B> for ValidatedBody
where where
Bytes: FromRequest<S, B>, Bytes: FromRequest<S, B>,
B: Send + 'static, B: Send + 'static,
S: Send + Sync,
{ {
type Rejection = Response; type Rejection = Response;
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> { async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
let body = Bytes::from_request(req, state) let body: Bytes = req.extract().await.map_err(IntoResponse::into_response)?;
.await
.map_err(IntoResponse::into_response)?;
// do validation... // do validation...
@@ -523,12 +516,11 @@ struct MyExtractor;
#[async_trait] #[async_trait]
impl<S, B> FromRequest<S, B> for MyExtractor impl<S, B> FromRequest<S, B> for MyExtractor
where where
S: Send + Sync,
B: Send + 'static, B: Send + 'static,
{ {
type Rejection = Infallible; type Rejection = Infallible;
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> { async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
// ... // ...
# todo!() # todo!()
} }
@@ -536,13 +528,10 @@ where
// and `FromRequestParts` // and `FromRequestParts`
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for MyExtractor impl<S> FromRequestParts<S> for MyExtractor {
where
S: Send + Sync,
{
type Rejection = Infallible; type Rejection = Infallible;
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
// ... // ...
# todo!() # todo!()
} }
@@ -588,16 +577,13 @@ struct AuthenticatedUser {
} }
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for AuthenticatedUser impl<S> FromRequestParts<S> for AuthenticatedUser {
where
S: Send + Sync,
{
type Rejection = Response; type Rejection = Response;
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
// You can either call them directly... // You can either call them directly...
let TypedHeader(Authorization(token)) = let TypedHeader(Authorization(token)) =
TypedHeader::<Authorization<Bearer>>::from_request_parts(parts, state) TypedHeader::<Authorization<Bearer>>::from_request_parts(parts, &())
.await .await
.map_err(|err| err.into_response())?; .map_err(|err| err.into_response())?;
+1 -2
View File
@@ -76,11 +76,10 @@ pub struct Extension<T>(pub T);
impl<T, S> FromRequestParts<S> for Extension<T> impl<T, S> FromRequestParts<S> for Extension<T>
where where
T: Clone + Send + Sync + 'static, T: Clone + Send + Sync + 'static,
S: Send + Sync,
{ {
type Rejection = ExtensionRejection; type Rejection = ExtensionRejection;
async fn from_request_parts(req: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(req: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
let value = req let value = req
.extensions .extensions
.get::<T>() .get::<T>()
+3 -3
View File
@@ -7,6 +7,7 @@
use super::{Extension, FromRequestParts}; use super::{Extension, FromRequestParts};
use crate::middleware::AddExtension; use crate::middleware::AddExtension;
use async_trait::async_trait; use async_trait::async_trait;
use axum_core::RequestPartsExt;
use http::request::Parts; use http::request::Parts;
use hyper::server::conn::AddrStream; use hyper::server::conn::AddrStream;
use std::{ use std::{
@@ -131,13 +132,12 @@ pub struct ConnectInfo<T>(pub T);
#[async_trait] #[async_trait]
impl<S, T> FromRequestParts<S> for ConnectInfo<T> impl<S, T> FromRequestParts<S> for ConnectInfo<T>
where where
S: Send + Sync,
T: Clone + Send + Sync + 'static, T: Clone + Send + Sync + 'static,
{ {
type Rejection = <Extension<Self> as FromRequestParts<S>>::Rejection; type Rejection = <Extension<Self> as FromRequestParts<S>>::Rejection;
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
let Extension(connect_info) = Extension::<Self>::from_request_parts(parts, state).await?; let Extension(connect_info): Extension<Self> = parts.extract().await?;
Ok(connect_info) Ok(connect_info)
} }
} }
+2 -5
View File
@@ -24,13 +24,10 @@ const X_FORWARDED_HOST_HEADER_KEY: &str = "X-Forwarded-Host";
pub struct Host(pub String); pub struct Host(pub String);
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for Host impl<S> FromRequestParts<S> for Host {
where
S: Send + Sync,
{
type Rejection = HostRejection; type Rejection = HostRejection;
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
if let Some(host) = parse_forwarded(&parts.headers) { if let Some(host) = parse_forwarded(&parts.headers) {
return Ok(Host(host.to_owned())); return Ok(Host(host.to_owned()));
} }
+2 -5
View File
@@ -65,13 +65,10 @@ impl MatchedPath {
} }
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for MatchedPath impl<S> FromRequestParts<S> for MatchedPath {
where
S: Send + Sync,
{
type Rejection = MatchedPathRejection; type Rejection = MatchedPathRejection;
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
let matched_path = parts let matched_path = parts
.extensions .extensions
.get::<Self>() .get::<Self>()
+3 -4
View File
@@ -60,15 +60,14 @@ where
B: HttpBody + Send + 'static, B: HttpBody + Send + 'static,
B::Data: Into<Bytes>, B::Data: Into<Bytes>,
B::Error: Into<BoxError>, B::Error: Into<BoxError>,
S: Send + Sync,
{ {
type Rejection = MultipartRejection; type Rejection = MultipartRejection;
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> { async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
let boundary = parse_boundary(req.headers()).ok_or(InvalidBoundary)?; let boundary = parse_boundary(req.headers()).ok_or(InvalidBoundary)?;
let stream_result = match req.with_limited_body() { let stream_result = match req.with_limited_body() {
Ok(limited) => BodyStream::from_request(limited, state).await, Ok(limited) => limited.extract::<BodyStream, _>().await,
Err(unlimited) => BodyStream::from_request(unlimited, state).await, Err(unlimited) => unlimited.extract::<BodyStream, _>().await,
}; };
let stream = stream_result.unwrap_or_else(|err| match err {}); let stream = stream_result.unwrap_or_else(|err| match err {});
let multipart = multer::Multipart::new(stream, boundary); let multipart = multer::Multipart::new(stream, boundary);
+1 -2
View File
@@ -170,11 +170,10 @@ impl<T> DerefMut for Path<T> {
impl<T, S> FromRequestParts<S> for Path<T> impl<T, S> FromRequestParts<S> for Path<T>
where where
T: DeserializeOwned + Send, T: DeserializeOwned + Send,
S: Send + Sync,
{ {
type Rejection = PathRejection; type Rejection = PathRejection;
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
let params = match parts.extensions.get::<UrlParams>() { let params = match parts.extensions.get::<UrlParams>() {
Some(UrlParams::Params(params)) => params, Some(UrlParams::Params(params)) => params,
Some(UrlParams::InvalidUtf8InPathParam { key }) => { Some(UrlParams::InvalidUtf8InPathParam { key }) => {
+1 -2
View File
@@ -53,11 +53,10 @@ pub struct Query<T>(pub T);
impl<T, S> FromRequestParts<S> for Query<T> impl<T, S> FromRequestParts<S> for Query<T>
where where
T: DeserializeOwned, T: DeserializeOwned,
S: Send + Sync,
{ {
type Rejection = QueryRejection; type Rejection = QueryRejection;
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
let query = parts.uri.query().unwrap_or_default(); let query = parts.uri.query().unwrap_or_default();
let value = serde_urlencoded::from_str(query) let value = serde_urlencoded::from_str(query)
.map_err(FailedToDeserializeQueryString::__private_new)?; .map_err(FailedToDeserializeQueryString::__private_new)?;
+3 -4
View File
@@ -1,5 +1,5 @@
use async_trait::async_trait; use async_trait::async_trait;
use axum_core::extract::FromRequest; use axum_core::{extract::FromRequest, RequestExt};
use bytes::{Bytes, BytesMut}; use bytes::{Bytes, BytesMut};
use http::{Method, Request}; use http::{Method, Request};
@@ -40,11 +40,10 @@ where
B: HttpBody + Send + 'static, B: HttpBody + Send + 'static,
B::Data: Send, B::Data: Send,
B::Error: Into<BoxError>, B::Error: Into<BoxError>,
S: Send + Sync,
{ {
type Rejection = RawFormRejection; type Rejection = RawFormRejection;
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> { async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
if req.method() == Method::GET { if req.method() == Method::GET {
let mut bytes = BytesMut::new(); let mut bytes = BytesMut::new();
@@ -58,7 +57,7 @@ where
return Err(InvalidFormContentType.into()); return Err(InvalidFormContentType.into());
} }
Ok(Self(Bytes::from_request(req, state).await?)) Ok(Self(req.extract::<Bytes, _>().await?))
} }
} }
} }
+2 -5
View File
@@ -28,13 +28,10 @@ use std::convert::Infallible;
pub struct RawQuery(pub Option<String>); pub struct RawQuery(pub Option<String>);
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for RawQuery impl<S> FromRequestParts<S> for RawQuery {
where
S: Send + Sync,
{
type Rejection = Infallible; type Rejection = Infallible;
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
let query = parts.uri.query().map(|query| query.to_owned()); let query = parts.uri.query().map(|query| query.to_owned());
Ok(Self(query)) Ok(Self(query))
} }
+7 -10
View File
@@ -4,6 +4,7 @@ use crate::{
BoxError, Error, BoxError, Error,
}; };
use async_trait::async_trait; use async_trait::async_trait;
use axum_core::RequestPartsExt;
use futures_util::stream::Stream; use futures_util::stream::Stream;
use http::{request::Parts, Request, Uri}; use http::{request::Parts, Request, Uri};
use std::{ use std::{
@@ -86,14 +87,12 @@ pub struct OriginalUri(pub Uri);
#[cfg(feature = "original-uri")] #[cfg(feature = "original-uri")]
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for OriginalUri impl<S> FromRequestParts<S> for OriginalUri {
where
S: Send + Sync,
{
type Rejection = Infallible; type Rejection = Infallible;
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
let uri = Extension::<Self>::from_request_parts(parts, state) let uri = parts
.extract::<Extension<Self>>()
.await .await
.unwrap_or_else(|_| Extension(OriginalUri(parts.uri.clone()))) .unwrap_or_else(|_| Extension(OriginalUri(parts.uri.clone())))
.0; .0;
@@ -151,11 +150,10 @@ where
B: HttpBody + Send + 'static, B: HttpBody + Send + 'static,
B::Data: Into<Bytes>, B::Data: Into<Bytes>,
B::Error: Into<BoxError>, B::Error: Into<BoxError>,
S: Send + Sync,
{ {
type Rejection = Infallible; type Rejection = Infallible;
async fn from_request(req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
let body = req let body = req
.into_body() .into_body()
.map_data(Into::into) .map_data(Into::into)
@@ -212,11 +210,10 @@ pub struct RawBody<B = Body>(pub B);
impl<S, B> FromRequest<S, B> for RawBody<B> impl<S, B> FromRequest<S, B> for RawBody<B>
where where
B: Send, B: Send,
S: Send + Sync,
{ {
type Rejection = Infallible; type Rejection = Infallible;
async fn from_request(req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
Ok(Self(req.into_body())) Ok(Self(req.into_body()))
} }
} }
+2 -5
View File
@@ -276,13 +276,10 @@ impl WebSocketUpgrade {
} }
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for WebSocketUpgrade impl<S> FromRequestParts<S> for WebSocketUpgrade {
where
S: Send + Sync,
{
type Rejection = WebSocketUpgradeRejection; type Rejection = WebSocketUpgradeRejection;
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
if parts.method != Method::GET { if parts.method != Method::GET {
return Err(MethodNotGet.into()); return Err(MethodNotGet.into());
} }
+1 -2
View File
@@ -68,11 +68,10 @@ where
B: HttpBody + Send + 'static, B: HttpBody + Send + 'static,
B::Data: Send, B::Data: Send,
B::Error: Into<BoxError>, B::Error: Into<BoxError>,
S: Send + Sync,
{ {
type Rejection = FormRejection; type Rejection = FormRejection;
async fn from_request(req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
match req.extract().await { match req.extract().await {
Ok(RawForm(bytes)) => { Ok(RawForm(bytes)) => {
let value = serde_urlencoded::from_bytes(&bytes) let value = serde_urlencoded::from_bytes(&bytes)
+6 -4
View File
@@ -4,7 +4,10 @@ use crate::{
BoxError, BoxError,
}; };
use async_trait::async_trait; use async_trait::async_trait;
use axum_core::response::{IntoResponse, Response}; use axum_core::{
response::{IntoResponse, Response},
RequestExt,
};
use bytes::{BufMut, BytesMut}; use bytes::{BufMut, BytesMut};
use http::{ use http::{
header::{self, HeaderMap, HeaderValue}, header::{self, HeaderMap, HeaderValue},
@@ -106,13 +109,12 @@ where
B: HttpBody + Send + 'static, B: HttpBody + Send + 'static,
B::Data: Send, B::Data: Send,
B::Error: Into<BoxError>, B::Error: Into<BoxError>,
S: Send + Sync,
{ {
type Rejection = JsonRejection; type Rejection = JsonRejection;
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> { async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
if json_content_type(req.headers()) { 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 deserializer = &mut serde_json::Deserializer::from_slice(&bytes);
let value = match serde_path_to_error::deserialize(deserializer) { let value = match serde_path_to_error::deserialize(deserializer) {
+4 -10
View File
@@ -45,13 +45,10 @@ use tower_service::Service;
/// struct RequireAuth; /// struct RequireAuth;
/// ///
/// #[async_trait] /// #[async_trait]
/// impl<S> FromRequestParts<S> for RequireAuth /// impl<S> FromRequestParts<S> for RequireAuth {
/// where
/// S: Send + Sync,
/// {
/// type Rejection = StatusCode; /// type Rejection = StatusCode;
/// ///
/// async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { /// async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
/// let auth_header = parts /// let auth_header = parts
/// .headers /// .headers
/// .get(header::AUTHORIZATION) /// .get(header::AUTHORIZATION)
@@ -370,15 +367,12 @@ mod tests {
struct MyExtractor; struct MyExtractor;
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for MyExtractor impl<S> FromRequestParts<S> for MyExtractor {
where
S: Send + Sync,
{
type Rejection = std::convert::Infallible; type Rejection = std::convert::Infallible;
async fn from_request_parts( async fn from_request_parts(
_parts: &mut Parts, _parts: &mut Parts,
_state: &S, _: &S,
) -> Result<Self, Self::Rejection> { ) -> Result<Self, Self::Rejection> {
unimplemented!() unimplemented!()
} }
+1 -2
View File
@@ -56,11 +56,10 @@ pub struct TypedHeader<T>(pub T);
impl<T, S> FromRequestParts<S> for TypedHeader<T> impl<T, S> FromRequestParts<S> for TypedHeader<T>
where where
T: headers::Header, T: headers::Header,
S: Send + Sync,
{ {
type Rejection = TypedHeaderRejection; type Rejection = TypedHeaderRejection;
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
match parts.headers.typed_try_get::<T>() { match parts.headers.typed_try_get::<T>() {
Ok(Some(value)) => Ok(Self(value)), Ok(Some(value)) => Ok(Self(value)),
Ok(None) => Err(TypedHeaderRejection { Ok(None) => Err(TypedHeaderRejection {
@@ -81,16 +81,11 @@ struct BufferRequestBody(Bytes);
// we must implement `FromRequest` (and not `FromRequestParts`) to consume the body // we must implement `FromRequest` (and not `FromRequestParts`) to consume the body
#[async_trait] #[async_trait]
impl<S> FromRequest<S, BoxBody> for BufferRequestBody impl<S> FromRequest<S, BoxBody> for BufferRequestBody {
where
S: Send + Sync,
{
type Rejection = Response; type Rejection = Response;
async fn from_request(req: Request<BoxBody>, state: &S) -> Result<Self, Self::Rejection> { async fn from_request(req: Request<BoxBody>, _: &S) -> Result<Self, Self::Rejection> {
let body = Bytes::from_request(req, state) let body: Bytes = req.extract().await.map_err(|err| err.into_response())?;
.await
.map_err(|err| err.into_response())?;
do_thing_with_request_body(body.clone()); do_thing_with_request_body(body.clone());
+2 -5
View File
@@ -122,13 +122,10 @@ impl AuthBody {
} }
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for Claims impl<S> FromRequestParts<S> for Claims {
where
S: Send + Sync,
{
type Rejection = AuthError; type Rejection = AuthError;
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
// Extract the token from the authorization header // Extract the token from the authorization header
let TypedHeader(Authorization(bearer)) = parts let TypedHeader(Authorization(bearer)) = parts
.extract::<TypedHeader<Authorization<Bearer>>>() .extract::<TypedHeader<Authorization<Bearer>>>()
@@ -60,7 +60,6 @@ enum JsonOrForm<T, K = T> {
impl<S, B, T, U> FromRequest<S, B> for JsonOrForm<T, U> impl<S, B, T, U> FromRequest<S, B> for JsonOrForm<T, U>
where where
B: Send + 'static, B: Send + 'static,
S: Send + Sync,
Json<T>: FromRequest<(), B>, Json<T>: FromRequest<(), B>,
Form<U>: FromRequest<(), B>, Form<U>: FromRequest<(), B>,
T: 'static, T: 'static,
@@ -68,7 +67,7 @@ where
{ {
type Rejection = Response; type Rejection = Response;
async fn from_request(req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request(req: Request<B>, _: &S) -> Result<Self, Self::Rejection> {
let content_type_header = req.headers().get(CONTENT_TYPE); let content_type_header = req.headers().get(CONTENT_TYPE);
let content_type = content_type_header.and_then(|value| value.to_str().ok()); let content_type = content_type_header.and_then(|value| value.to_str().ok());
+1 -1
View File
@@ -70,7 +70,7 @@ where
type Rejection = ServerError; type Rejection = ServerError;
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> { async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
let Form(value) = Form::<T>::from_request(req, state).await?; let Form(value): Form<T> = req.extract().await?;
value.validate()?; value.validate()?;
Ok(ValidatedForm(value)) Ok(ValidatedForm(value))
} }
+2 -5
View File
@@ -48,13 +48,10 @@ enum Version {
} }
#[async_trait] #[async_trait]
impl<S> FromRequestParts<S> for Version impl<S> FromRequestParts<S> for Version {
where
S: Send + Sync,
{
type Rejection = Response; type Rejection = Response;
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
let params: Path<HashMap<String, String>> = let params: Path<HashMap<String, String>> =
parts.extract().await.map_err(IntoResponse::into_response)?; parts.extract().await.map_err(IntoResponse::into_response)?;