diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a4e63a7..122d2048 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Implement `Deref` most extractors ([#56](https://github.com/tokio-rs/axum/pull/56)) - Return `405 Method Not Allowed` for unsupported method for route ([#63](https://github.com/tokio-rs/axum/pull/63)) - Add extractor for remote connection info ([#55](https://github.com/tokio-rs/axum/pull/55)) +- Clarify required response body type when routing to `tower::Service`s ([#69](https://github.com/tokio-rs/axum/pull/69)) +- Add `axum::body::box_body` to converting an `http_body::Body` to `axum::body::BoxBody` ([#69](https://github.com/tokio-rs/axum/pull/69)) ## Breaking changes diff --git a/src/body.rs b/src/body.rs index 667d544e..767f2daf 100644 --- a/src/body.rs +++ b/src/body.rs @@ -9,11 +9,12 @@ pub use hyper::body::Body; /// A boxed [`Body`] trait object. /// -/// This is used in axum as the response body type for applications. Its necessary to unify -/// multiple response bodies types into one. +/// This is used in axum as the response body type for applications. Its +/// necessary to unify multiple response bodies types into one. pub type BoxBody = http_body::combinators::BoxBody; -pub(crate) fn box_body(body: B) -> BoxBody +/// Convert a [`http_body::Body`] into a [`BoxBody`]. +pub fn box_body(body: B) -> BoxBody where B: http_body::Body + Send + Sync + 'static, B::Error: Into, diff --git a/src/lib.rs b/src/lib.rs index 90dee214..b4c452e5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -487,11 +487,24 @@ //! let app = route( //! // Any request to `/` goes to a service //! "/", +//! // Services who's response body is not `axum::body::BoxBody` +//! // can be wrapped in `axum::service::any` (or one of the other routing filters) +//! // to have the response body mapped //! service::any(service_fn(|_: Request| async { //! let res = Response::new(Body::from("Hi from `GET /`")); //! Ok(res) //! })) //! ).route( +//! "/foo", +//! // This service's response body is `axum::body::BoxBody` so +//! // it can be routed to directly. +//! service_fn(|req: Request| async move { +//! let body = Body::from(format!("Hi from `{} /foo`", req.method())); +//! let body = axum::body::box_body(body); +//! let res = Response::new(body); +//! Ok(res) +//! }) +//! ).route( //! // GET `/static/Cargo.toml` goes to a service from tower-http //! "/static/Cargo.toml", //! service::get(ServeFile::new("Cargo.toml"))