Skip to content

Commit

Permalink
Remove B type param: Follow ups (#1789)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
Co-authored-by: Michael Scofield <mscofield0@tutanota.com>
  • Loading branch information
3 people committed Mar 23, 2023
1 parent 0d50d17 commit 2ae0cdf
Show file tree
Hide file tree
Showing 70 changed files with 428 additions and 527 deletions.
16 changes: 2 additions & 14 deletions axum-core/src/body.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! HTTP body utilities.

use crate::response::{IntoResponse, Response};
use crate::{BoxError, Error};
use bytes::Bytes;
use bytes::{Buf, BufMut};
Expand All @@ -13,14 +12,9 @@ use std::pin::Pin;
use std::task::{Context, Poll};
use sync_wrapper::SyncWrapper;

/// A boxed [`Body`] trait object.
///
/// This is used in axum as the response body type for applications. It's
/// necessary to unify multiple response bodies types into one.
pub type BoxBody = http_body::combinators::UnsyncBoxBody<Bytes, Error>;
type BoxBody = http_body::combinators::UnsyncBoxBody<Bytes, Error>;

/// Convert a [`http_body::Body`] into a [`BoxBody`].
pub fn boxed<B>(body: B) -> BoxBody
fn boxed<B>(body: B) -> BoxBody
where
B: http_body::Body<Data = Bytes> + Send + 'static,
B::Error: Into<BoxError>,
Expand Down Expand Up @@ -230,12 +224,6 @@ where
}
}

impl IntoResponse for Body {
fn into_response(self) -> Response {
Response::new(self.0)
}
}

#[test]
fn test_try_downcast() {
assert_eq!(try_downcast::<i32, _>(5_u32), Err(5_u32));
Expand Down
35 changes: 16 additions & 19 deletions axum-core/src/ext_traits/request.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::body::Body;
use crate::extract::{DefaultBodyLimitKind, FromRequest, FromRequestParts};
use crate::extract::{DefaultBodyLimitKind, FromRequest, FromRequestParts, Request};
use futures_util::future::BoxFuture;
use http::Request;
use http_body::Limited;

mod sealed {
Expand All @@ -23,9 +22,9 @@ pub trait RequestExt: sealed::Sealed + Sized {
/// ```
/// use axum::{
/// async_trait,
/// extract::FromRequest,
/// extract::{Request, FromRequest},
/// body::Body,
/// http::{header::CONTENT_TYPE, Request, StatusCode},
/// http::{header::CONTENT_TYPE, StatusCode},
/// response::{IntoResponse, Response},
/// Form, Json, RequestExt,
/// };
Expand All @@ -42,7 +41,7 @@ pub trait RequestExt: sealed::Sealed + Sized {
/// {
/// 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 content_type = req
/// .headers()
/// .get(CONTENT_TYPE)
Expand Down Expand Up @@ -87,8 +86,7 @@ pub trait RequestExt: sealed::Sealed + Sized {
/// use axum::{
/// async_trait,
/// body::Body,
/// extract::{FromRef, FromRequest},
/// http::Request,
/// extract::{Request, FromRef, FromRequest},
/// RequestExt,
/// };
///
Expand All @@ -104,7 +102,7 @@ pub trait RequestExt: sealed::Sealed + Sized {
/// {
/// type Rejection = std::convert::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> {
/// let requires_state = req.extract_with_state::<RequiresState, _, _>(state).await?;
///
/// Ok(Self { requires_state })
Expand All @@ -122,7 +120,7 @@ pub trait RequestExt: sealed::Sealed + Sized {
/// {
/// // ...
/// # type Rejection = std::convert::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> {
/// # todo!()
/// # }
/// }
Expand All @@ -141,9 +139,8 @@ pub trait RequestExt: sealed::Sealed + Sized {
/// ```
/// use axum::{
/// async_trait,
/// extract::FromRequest,
/// extract::{Request, FromRequest},
/// headers::{authorization::Bearer, Authorization},
/// http::Request,
/// response::{IntoResponse, Response},
/// body::Body,
/// Json, RequestExt, TypedHeader,
Expand All @@ -163,7 +160,7 @@ pub trait RequestExt: sealed::Sealed + Sized {
/// {
/// type Rejection = Response;
///
/// async fn from_request(mut req: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
/// async fn from_request(mut req: Request, _state: &S) -> Result<Self, Self::Rejection> {
/// let TypedHeader(auth_header) = req
/// .extract_parts::<TypedHeader<Authorization<Bearer>>>()
/// .await
Expand Down Expand Up @@ -194,8 +191,8 @@ pub trait RequestExt: sealed::Sealed + Sized {
/// ```
/// use axum::{
/// async_trait,
/// extract::{FromRef, FromRequest, FromRequestParts},
/// http::{request::Parts, Request},
/// extract::{Request, FromRef, FromRequest, FromRequestParts},
/// http::request::Parts,
/// response::{IntoResponse, Response},
/// body::Body,
/// Json, RequestExt,
Expand All @@ -216,7 +213,7 @@ pub trait RequestExt: sealed::Sealed + Sized {
/// {
/// type Rejection = Response;
///
/// async fn from_request(mut req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
/// async fn from_request(mut req: Request, state: &S) -> Result<Self, Self::Rejection> {
/// let requires_state = req
/// .extract_parts_with_state::<RequiresState, _>(state)
/// .await
Expand Down Expand Up @@ -260,15 +257,15 @@ pub trait RequestExt: sealed::Sealed + Sized {
/// Apply the [default body limit](crate::extract::DefaultBodyLimit).
///
/// If it is disabled, return the request as-is in `Err`.
fn with_limited_body(self) -> Result<Request<Limited<Body>>, Request<Body>>;
fn with_limited_body(self) -> Result<Request<Limited<Body>>, Request>;

/// Consumes the request, returning the body wrapped in [`Limited`] if a
/// [default limit](crate::extract::DefaultBodyLimit) is in place, or not wrapped if the
/// default limit is disabled.
fn into_limited_body(self) -> Result<Limited<Body>, Body>;
}

impl RequestExt for Request<Body> {
impl RequestExt for Request {
fn extract<E, M>(self) -> BoxFuture<'static, Result<E, E::Rejection>>
where
E: FromRequest<(), M> + 'static,
Expand Down Expand Up @@ -321,7 +318,7 @@ impl RequestExt for Request<Body> {
})
}

fn with_limited_body(self) -> Result<Request<Limited<Body>>, Request<Body>> {
fn with_limited_body(self) -> Result<Request<Limited<Body>>, Request> {
// update docs in `axum-core/src/extract/default_body_limit.rs` and
// `axum/src/docs/extract.md` if this changes
const DEFAULT_LIMIT: usize = 2_097_152; // 2 mb
Expand Down Expand Up @@ -426,7 +423,7 @@ mod tests {
{
type Rejection = <String as FromRequest<()>>::Rejection;

async fn from_request(mut req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
async fn from_request(mut req: Request, state: &S) -> Result<Self, Self::Rejection> {
let RequiresState(from_state) = req.extract_parts_with_state(state).await.unwrap();
let method = req.extract_parts().await.unwrap();
let body = req.extract().await?;
Expand Down
9 changes: 4 additions & 5 deletions axum-core/src/extract/default_body_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
///
Expand All @@ -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;
Expand Down
14 changes: 9 additions & 5 deletions axum-core/src/extract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {}
Expand Down Expand Up @@ -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]
Expand All @@ -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
}
Expand Down Expand Up @@ -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())
}
}
Expand All @@ -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)
}
}
26 changes: 19 additions & 7 deletions axum-core/src/extract/request_parts.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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())
}
}
7 changes: 3 additions & 4 deletions axum-core/src/extract/tuple.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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();

$(
Expand Down
Loading

0 comments on commit 2ae0cdf

Please sign in to comment.