Remove B type param (#1751)

Co-authored-by: Jonas Platte <[email protected]>
Co-authored-by: Michael Scofield <[email protected]>
This commit is contained in:
David Pedersen
2023-04-21 17:45:31 +02:00
co-authored by Jonas Platte Michael Scofield
parent 9be0ea934c
commit 4e4c29175f
100 changed files with 966 additions and 1160 deletions
+11 -56
View File
@@ -4,7 +4,7 @@
//!
//! [`axum::extract`]: https://docs.rs/axum/latest/axum/extract/index.html
use crate::response::IntoResponse;
use crate::{body::Body, response::IntoResponse};
use async_trait::async_trait;
use http::{request::Parts, Request};
use std::convert::Infallible;
@@ -64,48 +64,6 @@ pub trait FromRequestParts<S>: Sized {
///
/// See [`axum::extract`] for more general docs about extractors.
///
/// # What is the `B` type parameter?
///
/// `FromRequest` is generic over the request body (the `B` in
/// [`http::Request<B>`]). This is to allow `FromRequest` to be usable with any
/// type of request body. This is necessary because some middleware change the
/// request body, for example to add timeouts.
///
/// If you're writing your own `FromRequest` that wont be used outside your
/// application, and not using any middleware that changes the request body, you
/// can most likely use `axum::body::Body`.
///
/// If you're writing a library that's intended for others to use, it's recommended
/// to keep the generic type parameter:
///
/// ```rust
/// use axum::{
/// async_trait,
/// extract::FromRequest,
/// http::{self, Request},
/// };
///
/// struct MyExtractor;
///
/// #[async_trait]
/// impl<S, B> FromRequest<S, B> for MyExtractor
/// where
/// // these bounds are required by `async_trait`
/// B: Send + 'static,
/// S: Send + Sync,
/// {
/// type Rejection = http::StatusCode;
///
/// async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
/// // ...
/// # unimplemented!()
/// }
/// }
/// ```
///
/// This ensures your extractor is as flexible as possible.
///
/// [`http::Request<B>`]: http::Request
/// [`axum::extract`]: https://docs.rs/axum/0.6.0/axum/extract/index.html
#[async_trait]
#[cfg_attr(
@@ -114,25 +72,24 @@ pub trait FromRequestParts<S>: Sized {
note = "Function argument is not a valid axum extractor. \nSee `https://docs.rs/axum/latest/axum/extract/index.html` for details",
)
)]
pub trait FromRequest<S, B, M = private::ViaRequest>: Sized {
pub trait FromRequest<S, M = private::ViaRequest>: Sized {
/// If the extractor fails it'll use this "rejection" type. A rejection is
/// a kind of error that can be converted into a response.
type Rejection: IntoResponse;
/// Perform the extraction.
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection>;
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection>;
}
#[async_trait]
impl<S, B, T> FromRequest<S, B, private::ViaParts> for T
impl<S, T> FromRequest<S, private::ViaParts> for T
where
B: Send + 'static,
S: Send + Sync,
T: FromRequestParts<S>,
{
type Rejection = <Self as FromRequestParts<S>>::Rejection;
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
let (mut parts, _) = req.into_parts();
Self::from_request_parts(&mut parts, state).await
}
@@ -155,15 +112,14 @@ where
}
#[async_trait]
impl<S, T, B> FromRequest<S, B> for Option<T>
impl<S, T> FromRequest<S> for Option<T>
where
T: FromRequest<S, B>,
B: Send + 'static,
T: FromRequest<S>,
S: Send + Sync,
{
type Rejection = Infallible;
async fn from_request(req: Request<B>, state: &S) -> Result<Option<T>, Self::Rejection> {
async fn from_request(req: Request<Body>, state: &S) -> Result<Option<T>, Self::Rejection> {
Ok(T::from_request(req, state).await.ok())
}
}
@@ -182,15 +138,14 @@ where
}
#[async_trait]
impl<S, T, B> FromRequest<S, B> for Result<T, T::Rejection>
impl<S, T> FromRequest<S> for Result<T, T::Rejection>
where
T: FromRequest<S, B>,
B: Send + 'static,
T: FromRequest<S>,
S: Send + Sync,
{
type Rejection = Infallible;
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
Ok(T::from_request(req, state).await)
}
}