Skip to content

Commit

Permalink
use Body rather than BoxBody
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpdrsn committed Jul 11, 2022
1 parent f865576 commit 0e76ded
Show file tree
Hide file tree
Showing 31 changed files with 178 additions and 237 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ members = [
# with `cargo +nightly update -Z minimal-versions`
"internal-minimal-versions",
]

[patch.crates-io]
hyper = { git = "https://github.com/hyperium/hyper", branch = "david/body-wrap-body" }
2 changes: 1 addition & 1 deletion axum-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ bytes = "1.0"
futures-util = { version = "0.3", default-features = false, features = ["alloc"] }
http = "0.2.7"
http-body = "0.4.5"
hyper = "0.14"
mime = "0.3.16"

[dev-dependencies]
axum = { path = "../axum", version = "0.5" }
futures-util = "0.3"
hyper = "0.14"
tokio = { version = "1.0", features = ["macros"] }
7 changes: 5 additions & 2 deletions axum-core/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
use crate::{BoxError, Error};
use bytes::Bytes;
use bytes::{Buf, BufMut};
use http_body::Body;
use http_body::Body as _;

#[doc(no_inline)]
pub use hyper::Body;

/// A boxed [`Body`] trait object.
///
Expand Down Expand Up @@ -55,7 +58,7 @@ where
// THE SOFTWARE.
pub(crate) async fn to_bytes<T>(body: T) -> Result<Bytes, T::Error>
where
T: Body,
T: http_body::Body,
{
futures_util::pin_mut!(body);

Expand Down
7 changes: 0 additions & 7 deletions axum-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ impl Error {
inner: error.into(),
}
}

pub(crate) fn downcast_inner<T>(self) -> Result<Box<T>, BoxError>
where
T: std::error::Error + 'static,
{
self.inner.downcast()
}
}

impl fmt::Display for Error {
Expand Down
22 changes: 8 additions & 14 deletions axum-core/src/extract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@
//! [`axum::extract`]: https://docs.rs/axum/latest/axum/extract/index.html
use self::rejection::*;
use crate::{body::BoxBody, response::IntoResponse, BoxError};
use crate::{body::Body, response::IntoResponse, BoxError};
use async_trait::async_trait;
use bytes::Bytes;
use http::{Extensions, HeaderMap, Method, Uri, Version};
use http::{Extensions, HeaderMap, Method, Request, Uri, Version};
use std::convert::Infallible;

pub mod rejection;

mod request_parts;
mod tuple;

/// Type alias for [`http::Request`] whose body type defaults to [`BoxBody`], the most common body
/// type used with axum.
pub type Request<T = BoxBody> = http::Request<T>;

/// Types that can be created from requests.
///
/// See [`axum::extract`] for more details.
Expand Down Expand Up @@ -84,7 +80,7 @@ pub struct RequestParts {
version: Version,
headers: HeaderMap,
extensions: Extensions,
body: Option<BoxBody>,
body: Option<Body>,
}

impl RequestParts {
Expand Down Expand Up @@ -112,15 +108,13 @@ impl RequestParts {
body,
) = req.into_parts();

let body = crate::body::boxed(body);

RequestParts {
method,
uri,
version,
headers,
extensions,
body: Some(body),
body: Some(Body::wrap_body(body)),
}
}

Expand Down Expand Up @@ -165,7 +159,7 @@ impl RequestParts {
/// been called.
///
/// [`take_body`]: RequestParts::take_body
pub fn try_into_request(self) -> Result<Request<BoxBody>, BodyAlreadyExtracted> {
pub fn try_into_request(self) -> Result<Request<Body>, BodyAlreadyExtracted> {
let Self {
method,
uri,
Expand Down Expand Up @@ -243,20 +237,20 @@ impl RequestParts {
/// Gets a reference to the request body.
///
/// Returns `None` if the body has been taken by another extractor.
pub fn body(&self) -> Option<&BoxBody> {
pub fn body(&self) -> Option<&Body> {
self.body.as_ref()
}

/// Gets a mutable reference to the request body.
///
/// Returns `None` if the body has been taken by another extractor.
// this returns `&mut Option<B>` rather than `Option<&mut B>` such that users can use it to set the body.
pub fn body_mut(&mut self) -> &mut Option<BoxBody> {
pub fn body_mut(&mut self) -> &mut Option<Body> {
&mut self.body
}

/// Takes the body out of the request, leaving a `None` in its place.
pub fn take_body(&mut self) -> Option<BoxBody> {
pub fn take_body(&mut self) -> Option<Body> {
self.body.take()
}
}
Expand Down
15 changes: 8 additions & 7 deletions axum-core/src/extract/rejection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ impl FailedToBufferBody {
where
E: Into<BoxError>,
{
let err = match err.into().downcast::<crate::Error>() {
Ok(err) => err,
Err(err) => return Self::UnknownBodyError(UnknownBodyError::from_err(err)),
};
match err.downcast_inner::<http_body::LengthLimitError>() {
Ok(err) => Self::LengthLimitError(LengthLimitError::from_err(err)),
Err(err) => Self::UnknownBodyError(UnknownBodyError::from_err(err)),
let err = err.into();
if err
.source()
.map_or(false, |source| source.is::<http_body::LengthLimitError>())
{
Self::LengthLimitError(LengthLimitError::from_err(err))
} else {
Self::UnknownBodyError(UnknownBodyError::from_err(err))
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions axum-core/src/extract/request_parts.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::{rejection::*, FromRequest, RequestParts};
use crate::body::BoxBody;
use crate::body::Body;
use async_trait::async_trait;
use bytes::Bytes;
use http::{Extensions, HeaderMap, Method, Request, Uri, Version};
use std::convert::Infallible;

#[async_trait]
impl FromRequest for Request<BoxBody> {
impl FromRequest for Request<Body> {
type Rejection = BodyAlreadyExtracted;

async fn from_request(req: &mut RequestParts) -> Result<Self, Self::Rejection> {
Expand Down Expand Up @@ -68,7 +68,7 @@ impl FromRequest for HeaderMap {
}

#[async_trait]
impl FromRequest for BoxBody {
impl FromRequest for Body {
type Rejection = BodyAlreadyExtracted;

async fn from_request(req: &mut RequestParts) -> Result<Self, Self::Rejection> {
Expand Down Expand Up @@ -140,6 +140,6 @@ fn unwrap_infallible<T>(result: Result<T, Infallible>) -> T {
}
}

pub(crate) fn take_body(req: &mut RequestParts) -> Result<BoxBody, BodyAlreadyExtracted> {
pub(crate) fn take_body(req: &mut RequestParts) -> Result<Body, BodyAlreadyExtracted> {
req.take_body().ok_or(BodyAlreadyExtracted)
}
8 changes: 5 additions & 3 deletions axum-extra/src/json_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use axum::{
async_trait,
body::{BoxBody, Bytes, HttpBody, StreamBody},
body::{Body, Bytes, HttpBody, StreamBody},
extract::{rejection::BodyAlreadyExtracted, FromRequest, RequestParts},
response::{IntoResponse, Response},
BoxError,
Expand Down Expand Up @@ -136,15 +136,17 @@ where
pin_project! {
struct BodyStream {
#[pin]
body: BoxBody,
body: Body,
}
}

impl Stream for BodyStream {
type Item = Result<Bytes, axum::Error>;

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Pin::new(&mut self.body).poll_data(cx)
Pin::new(&mut self.body)
.poll_data(cx)
.map_err(axum::Error::new)
}
}

Expand Down
12 changes: 3 additions & 9 deletions axum-extra/src/routing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Additional types for defining routes.
use axum::{
body::BoxBody,
body::Body,
handler::Handler,
http::Request,
response::{Redirect, Response},
Expand Down Expand Up @@ -162,10 +162,7 @@ pub trait RouterExt: sealed::Sealed {
/// ```
fn route_with_tsr<T>(self, path: &str, service: T) -> Self
where
T: Service<Request<BoxBody>, Response = Response, Error = Infallible>
+ Clone
+ Send
+ 'static,
T: Service<Request<Body>, Response = Response, Error = Infallible> + Clone + Send + 'static,
T::Future: Send + 'static,
Self: Sized;
}
Expand Down Expand Up @@ -253,10 +250,7 @@ impl RouterExt for Router {

fn route_with_tsr<T>(mut self, path: &str, service: T) -> Self
where
T: Service<Request<BoxBody>, Response = Response, Error = Infallible>
+ Clone
+ Send
+ 'static,
T: Service<Request<Body>, Response = Response, Error = Infallible> + Clone + Send + 'static,
T::Future: Send + 'static,
Self: Sized,
{
Expand Down
17 changes: 4 additions & 13 deletions axum-extra/src/routing/resource.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use axum::{
body::BoxBody,
body::Body,
handler::Handler,
http::Request,
response::Response,
Expand Down Expand Up @@ -138,10 +138,7 @@ impl Resource {
/// The routes will be nested at `/{resource_name}/:{resource_name}_id`.
pub fn nest<T>(mut self, svc: T) -> Self
where
T: Service<Request<BoxBody>, Response = Response, Error = Infallible>
+ Clone
+ Send
+ 'static,
T: Service<Request<Body>, Response = Response, Error = Infallible> + Clone + Send + 'static,
T::Future: Send + 'static,
{
let path = self.show_update_destroy_path();
Expand All @@ -154,10 +151,7 @@ impl Resource {
/// The routes will be nested at `/{resource_name}`.
pub fn nest_collection<T>(mut self, svc: T) -> Self
where
T: Service<Request<BoxBody>, Response = Response, Error = Infallible>
+ Clone
+ Send
+ 'static,
T: Service<Request<Body>, Response = Response, Error = Infallible> + Clone + Send + 'static,
T::Future: Send + 'static,
{
let path = self.index_create_path();
Expand All @@ -175,10 +169,7 @@ impl Resource {

fn route<T>(mut self, path: &str, svc: T) -> Self
where
T: Service<Request<BoxBody>, Response = Response, Error = Infallible>
+ Clone
+ Send
+ 'static,
T: Service<Request<Body>, Response = Response, Error = Infallible> + Clone + Send + 'static,
T::Future: Send + 'static,
{
self.router = self.router.route(path, svc);
Expand Down
6 changes: 3 additions & 3 deletions axum-extra/src/routing/spa.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use axum::{
body::BoxBody,
body::Body,
error_handling::HandleError,
response::Response,
routing::{get_service, Route},
Expand Down Expand Up @@ -151,8 +151,8 @@ impl<F, T> From<SpaRouter<T, F>> for Router
where
F: Clone + Send + 'static,
HandleError<Route<io::Error>, F, T>:
Service<Request<BoxBody>, Response = Response, Error = Infallible>,
<HandleError<Route<io::Error>, F, T> as Service<Request<BoxBody>>>::Future: Send,
Service<Request<Body>, Response = Response, Error = Infallible>,
<HandleError<Route<io::Error>, F, T> as Service<Request<Body>>>::Future: Send,
T: 'static,
{
fn from(spa: SpaRouter<T, F>) -> Self {
Expand Down
4 changes: 2 additions & 2 deletions axum-macros/tests/debug_handler/fail/request_not_last.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use axum::extract::{Extension, Request};
use axum::{extract::Extension, body::Body, http::Request};
use axum_macros::debug_handler;

#[debug_handler]
async fn handler(_: Request, _: Extension<String>) {}
async fn handler(_: Request<Body>, _: Extension<String>) {}

fn main() {}
4 changes: 2 additions & 2 deletions axum-macros/tests/debug_handler/fail/request_not_last.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: `Request` extractor should always be last
--> tests/debug_handler/fail/request_not_last.rs:5:18
|
5 | async fn handler(_: Request, _: Extension<String>) {}
| ^^^^^^^^^^
5 | async fn handler(_: Request<Body>, _: Extension<String>) {}
| ^^^^^^^^^^^^^^^^
4 changes: 2 additions & 2 deletions axum-macros/tests/debug_handler/pass/request_last.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use axum::extract::{Extension, Request};
use axum::{extract::Extension, body::Body, http::Request};
use axum_macros::debug_handler;

#[debug_handler]
async fn handler(_: Extension<String>, _: Request) {}
async fn handler(_: Extension<String>, _: Request<Body>) {}

fn main() {}
Loading

0 comments on commit 0e76ded

Please sign in to comment.