Remove B type param: Follow ups (#1789)

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 72c1b7a80c
commit 6703f8634c
70 changed files with 429 additions and 531 deletions
+4 -5
View File
@@ -30,22 +30,21 @@ use tower_layer::Layer;
/// Router,
/// routing::post,
/// body::Body,
/// extract::{DefaultBodyLimit, RawBody},
/// http::Request,
/// extract::{Request, DefaultBodyLimit},
/// };
///
/// let app = Router::new()
/// .route(
/// "/",
/// // even with `DefaultBodyLimit` the request body is still just `Body`
/// post(|request: Request<Body>| async {}),
/// post(|request: Request| async {}),
/// )
/// .layer(DefaultBodyLimit::max(1024));
/// # let _: Router = app;
/// ```
///
/// ```
/// use axum::{Router, routing::post, body::Body, extract::RawBody, http::Request};
/// use axum::{Router, routing::post, body::Body, extract::Request};
/// use tower_http::limit::RequestBodyLimitLayer;
/// use http_body::Limited;
///
@@ -54,7 +53,7 @@ use tower_layer::Layer;
/// "/",
/// // `RequestBodyLimitLayer` changes the request body type to `Limited<Body>`
/// // extracting a different body type wont work
/// post(|request: Request<Body>| async {}),
/// post(|request: Request| async {}),
/// )
/// .layer(RequestBodyLimitLayer::new(1024));
/// # let _: Router = app;
+9 -5
View File
@@ -6,7 +6,7 @@
use crate::{body::Body, response::IntoResponse};
use async_trait::async_trait;
use http::{request::Parts, Request};
use http::request::Parts;
use std::convert::Infallible;
pub mod rejection;
@@ -19,6 +19,10 @@ mod tuple;
pub(crate) use self::default_body_limit::DefaultBodyLimitKind;
pub use self::{default_body_limit::DefaultBodyLimit, from_ref::FromRef};
/// Type alias for [`http::Request`] whose body type defaults to [`Body`], the most common body
/// type used with axum.
pub type Request<T = Body> = http::Request<T>;
mod private {
#[derive(Debug, Clone, Copy)]
pub enum ViaParts {}
@@ -78,7 +82,7 @@ pub trait FromRequest<S, M = private::ViaRequest>: Sized {
type Rejection: IntoResponse;
/// Perform the extraction.
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection>;
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection>;
}
#[async_trait]
@@ -89,7 +93,7 @@ where
{
type Rejection = <Self as FromRequestParts<S>>::Rejection;
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
let (mut parts, _) = req.into_parts();
Self::from_request_parts(&mut parts, state).await
}
@@ -119,7 +123,7 @@ where
{
type Rejection = Infallible;
async fn from_request(req: Request<Body>, state: &S) -> Result<Option<T>, Self::Rejection> {
async fn from_request(req: Request, state: &S) -> Result<Option<T>, Self::Rejection> {
Ok(T::from_request(req, state).await.ok())
}
}
@@ -145,7 +149,7 @@ where
{
type Rejection = Infallible;
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
Ok(T::from_request(req, state).await)
}
}
+19 -7
View File
@@ -1,18 +1,18 @@
use super::{rejection::*, FromRequest, FromRequestParts};
use super::{rejection::*, FromRequest, FromRequestParts, Request};
use crate::{body::Body, RequestExt};
use async_trait::async_trait;
use bytes::Bytes;
use http::{request::Parts, HeaderMap, Method, Request, Uri, Version};
use http::{request::Parts, HeaderMap, Method, Uri, Version};
use std::convert::Infallible;
#[async_trait]
impl<S> FromRequest<S> for Request<Body>
impl<S> FromRequest<S> for Request
where
S: Send + Sync,
{
type Rejection = Infallible;
async fn from_request(req: Request<Body>, _: &S) -> Result<Self, Self::Rejection> {
async fn from_request(req: Request, _: &S) -> Result<Self, Self::Rejection> {
Ok(req)
}
}
@@ -77,7 +77,7 @@ where
{
type Rejection = BytesRejection;
async fn from_request(req: Request<Body>, _: &S) -> Result<Self, Self::Rejection> {
async fn from_request(req: Request, _: &S) -> Result<Self, Self::Rejection> {
let bytes = match req.into_limited_body() {
Ok(limited_body) => crate::body::to_bytes(limited_body)
.await
@@ -98,7 +98,7 @@ where
{
type Rejection = StringRejection;
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
let bytes = Bytes::from_request(req, state)
.await
.map_err(|err| match err {
@@ -122,7 +122,19 @@ where
{
type Rejection = Infallible;
async fn from_request(req: Request<Body>, _: &S) -> Result<Self, Self::Rejection> {
async fn from_request(req: Request, _: &S) -> Result<Self, Self::Rejection> {
Ok(req.into_parts().0)
}
}
#[async_trait]
impl<S> FromRequest<S> for Body
where
S: Send + Sync,
{
type Rejection = Infallible;
async fn from_request(req: Request, _: &S) -> Result<Self, Self::Rejection> {
Ok(req.into_body())
}
}
+3 -4
View File
@@ -1,8 +1,7 @@
use super::{FromRequest, FromRequestParts};
use crate::body::Body;
use super::{FromRequest, FromRequestParts, Request};
use crate::response::{IntoResponse, Response};
use async_trait::async_trait;
use http::request::{Parts, Request};
use http::request::Parts;
use std::convert::Infallible;
#[async_trait]
@@ -57,7 +56,7 @@ macro_rules! impl_from_request {
{
type Rejection = Response;
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
let (mut parts, body) = req.into_parts();
$(