From aa90d1de0b6fb7695037762ffbc3e7bb873eff22 Mon Sep 17 00:00:00 2001 From: Gabriel de Perthuis Date: Mon, 23 May 2022 20:38:02 +0200 Subject: [PATCH] Move adapters to `http-body-util` (#56) --- .github/workflows/CI.yml | 2 +- Cargo.toml | 37 +---------- http-body-util/CHANGELOG.md | 3 + http-body-util/Cargo.toml | 35 +++++++++++ .../src}/combinators/box_body.rs | 8 ++- .../src}/combinators/map_data.rs | 2 +- .../src}/combinators/map_err.rs | 4 +- .../src}/combinators/mod.rs | 0 {src => http-body-util/src}/empty.rs | 2 +- {src => http-body-util/src}/full.rs | 2 +- http-body-util/src/lib.rs | 63 +++++++++++++++++++ {src => http-body-util/src}/limited.rs | 2 +- CHANGELOG.md => http-body/CHANGELOG.md | 0 http-body/Cargo.toml | 30 +++++++++ {src => http-body/src}/lib.rs | 46 +------------- {src => http-body/src}/next.rs | 0 {src => http-body/src}/size_hint.rs | 0 {tests => http-body/tests}/is_end_stream.rs | 5 +- 18 files changed, 149 insertions(+), 92 deletions(-) create mode 100644 http-body-util/CHANGELOG.md create mode 100644 http-body-util/Cargo.toml rename {src => http-body-util/src}/combinators/box_body.rs (95%) rename {src => http-body-util/src}/combinators/map_data.rs (99%) rename {src => http-body-util/src}/combinators/map_err.rs (96%) rename {src => http-body-util/src}/combinators/mod.rs (100%) rename {src => http-body-util/src}/empty.rs (97%) rename {src => http-body-util/src}/full.rs (98%) create mode 100644 http-body-util/src/lib.rs rename {src => http-body-util/src}/limited.rs (99%) rename CHANGELOG.md => http-body/CHANGELOG.md (100%) create mode 100644 http-body/Cargo.toml rename {src => http-body/src}/lib.rs (86%) rename {src => http-body/src}/next.rs (100%) rename {src => http-body/src}/size_hint.rs (100%) rename {tests => http-body/tests}/is_end_stream.rs (95%) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 6f09a5d..bf7c3c4 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -27,4 +27,4 @@ jobs: - name: Install Rust run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }} - name: Run tests - run: cargo test + run: cargo test --workspace diff --git a/Cargo.toml b/Cargo.toml index 4324486..5e25e24 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,34 +1,3 @@ -[package] -name = "http-body" -# When releasing to crates.io: -# - Remove path dependencies -# - Update html_root_url. -# - Update doc url -# - Cargo.toml -# - README.md -# - Update CHANGELOG.md. -# - Create "vx.y.z" git tag. -version = "0.4.5" -authors = [ - "Carl Lerche ", - "Lucio Franco ", - "Sean McArthur ", -] -edition = "2018" -readme = "README.md" -documentation = "https://docs.rs/http-body" -repository = "https://github.com/hyperium/http-body" -license = "MIT" -description = """ -Trait representing an asynchronous, streaming, HTTP request or response body. -""" -keywords = ["http"] -categories = ["web-programming"] - -[dependencies] -bytes = "1" -http = "0.2" -pin-project-lite = "0.2" - -[dev-dependencies] -tokio = { version = "1", features = ["macros", "rt"] } +[workspace] +members = ["http-body", "http-body-util"] +resolver = "2" diff --git a/http-body-util/CHANGELOG.md b/http-body-util/CHANGELOG.md new file mode 100644 index 0000000..e999fe0 --- /dev/null +++ b/http-body-util/CHANGELOG.md @@ -0,0 +1,3 @@ +# Unreleased + +- Initial release, split from http-body 0.4.5. diff --git a/http-body-util/Cargo.toml b/http-body-util/Cargo.toml new file mode 100644 index 0000000..e74a31b --- /dev/null +++ b/http-body-util/Cargo.toml @@ -0,0 +1,35 @@ +[package] +name = "http-body-util" +# When releasing to crates.io: +# - Remove path dependencies +# - Update html_root_url. +# - Update doc url +# - Cargo.toml +# - README.md +# - Update CHANGELOG.md. +# - Create "http-body-util-x.y.z" git tag. +version = "0.1.0" +authors = [ + "Carl Lerche ", + "Lucio Franco ", + "Sean McArthur ", +] +edition = "2018" +readme = "README.md" +documentation = "https://docs.rs/http-body-util" +repository = "https://github.com/hyperium/http-body" +license = "MIT" +description = """ +Combinators and adapters for HTTP request or response bodies. +""" +keywords = ["http"] +categories = ["web-programming"] + +[dependencies] +bytes = "1" +http = "0.2" +http-body = { path = "../http-body" } +pin-project-lite = "0.2" + +[dev-dependencies] +tokio = { version = "1", features = ["macros", "rt"] } diff --git a/src/combinators/box_body.rs b/http-body-util/src/combinators/box_body.rs similarity index 95% rename from src/combinators/box_body.rs rename to http-body-util/src/combinators/box_body.rs index 97c8313..19bc120 100644 --- a/src/combinators/box_body.rs +++ b/http-body-util/src/combinators/box_body.rs @@ -1,5 +1,7 @@ -use crate::Body; +use crate::BodyExt as _; + use bytes::Buf; +use http_body::{Body, SizeHint}; use std::{ fmt, pin::Pin, @@ -60,7 +62,7 @@ where self.inner.is_end_stream() } - fn size_hint(&self) -> crate::SizeHint { + fn size_hint(&self) -> SizeHint { self.inner.size_hint() } } @@ -119,7 +121,7 @@ where self.inner.is_end_stream() } - fn size_hint(&self) -> crate::SizeHint { + fn size_hint(&self) -> SizeHint { self.inner.size_hint() } } diff --git a/src/combinators/map_data.rs b/http-body-util/src/combinators/map_data.rs similarity index 99% rename from src/combinators/map_data.rs rename to http-body-util/src/combinators/map_data.rs index 6d9c5a8..d01e476 100644 --- a/src/combinators/map_data.rs +++ b/http-body-util/src/combinators/map_data.rs @@ -1,5 +1,5 @@ -use crate::Body; use bytes::Buf; +use http_body::Body; use pin_project_lite::pin_project; use std::{ any::type_name, diff --git a/src/combinators/map_err.rs b/http-body-util/src/combinators/map_err.rs similarity index 96% rename from src/combinators/map_err.rs rename to http-body-util/src/combinators/map_err.rs index c77168d..5b80e8d 100644 --- a/src/combinators/map_err.rs +++ b/http-body-util/src/combinators/map_err.rs @@ -1,4 +1,4 @@ -use crate::Body; +use http_body::{Body, SizeHint}; use pin_project_lite::pin_project; use std::{ any::type_name, @@ -79,7 +79,7 @@ where self.inner.is_end_stream() } - fn size_hint(&self) -> crate::SizeHint { + fn size_hint(&self) -> SizeHint { self.inner.size_hint() } } diff --git a/src/combinators/mod.rs b/http-body-util/src/combinators/mod.rs similarity index 100% rename from src/combinators/mod.rs rename to http-body-util/src/combinators/mod.rs diff --git a/src/empty.rs b/http-body-util/src/empty.rs similarity index 97% rename from src/empty.rs rename to http-body-util/src/empty.rs index 7d63ceb..c495054 100644 --- a/src/empty.rs +++ b/http-body-util/src/empty.rs @@ -1,6 +1,6 @@ -use super::{Body, SizeHint}; use bytes::Buf; use http::HeaderMap; +use http_body::{Body, SizeHint}; use std::{ convert::Infallible, fmt, diff --git a/src/full.rs b/http-body-util/src/full.rs similarity index 98% rename from src/full.rs rename to http-body-util/src/full.rs index f1d063b..a88db9e 100644 --- a/src/full.rs +++ b/http-body-util/src/full.rs @@ -1,6 +1,6 @@ -use crate::{Body, SizeHint}; use bytes::{Buf, Bytes}; use http::HeaderMap; +use http_body::{Body, SizeHint}; use pin_project_lite::pin_project; use std::borrow::Cow; use std::convert::{Infallible, TryFrom}; diff --git a/http-body-util/src/lib.rs b/http-body-util/src/lib.rs new file mode 100644 index 0000000..2e55b27 --- /dev/null +++ b/http-body-util/src/lib.rs @@ -0,0 +1,63 @@ +#![deny( + missing_debug_implementations, + missing_docs, + unreachable_pub, + rustdoc::broken_intra_doc_links +)] +#![cfg_attr(test, deny(warnings))] + +//! Utilities for [`http_body::Body`]. +//! +//! [`BodyExt`] adds extensions to the common trait. +//! +//! [`Empty`] and [`Full`] provide simple implementations. + +pub mod combinators; +mod empty; +mod full; +mod limited; + +use self::combinators::{BoxBody, MapData, MapErr, UnsyncBoxBody}; +pub use self::empty::Empty; +pub use self::full::Full; +pub use self::limited::{LengthLimitError, Limited}; + +/// An extension trait for [`http_body::Body`] adding various combinators and adapters +pub trait BodyExt: http_body::Body { + /// Maps this body's data value to a different value. + fn map_data(self, f: F) -> MapData + where + Self: Sized, + F: FnMut(Self::Data) -> B, + B: bytes::Buf, + { + MapData::new(self, f) + } + + /// Maps this body's error value to a different value. + fn map_err(self, f: F) -> MapErr + where + Self: Sized, + F: FnMut(Self::Error) -> E, + { + MapErr::new(self, f) + } + + /// Turn this body into a boxed trait object. + fn boxed(self) -> BoxBody + where + Self: Sized + Send + Sync + 'static, + { + BoxBody::new(self) + } + + /// Turn this body into a boxed trait object that is !Sync. + fn boxed_unsync(self) -> UnsyncBoxBody + where + Self: Sized + Send + 'static, + { + UnsyncBoxBody::new(self) + } +} + +impl BodyExt for T where T: http_body::Body {} diff --git a/src/limited.rs b/http-body-util/src/limited.rs similarity index 99% rename from src/limited.rs rename to http-body-util/src/limited.rs index a40add9..3f3ceae 100644 --- a/src/limited.rs +++ b/http-body-util/src/limited.rs @@ -1,6 +1,6 @@ -use crate::{Body, SizeHint}; use bytes::Buf; use http::HeaderMap; +use http_body::{Body, SizeHint}; use pin_project_lite::pin_project; use std::error::Error; use std::fmt; diff --git a/CHANGELOG.md b/http-body/CHANGELOG.md similarity index 100% rename from CHANGELOG.md rename to http-body/CHANGELOG.md diff --git a/http-body/Cargo.toml b/http-body/Cargo.toml new file mode 100644 index 0000000..45f2919 --- /dev/null +++ b/http-body/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "http-body" +# When releasing to crates.io: +# - Remove path dependencies +# - Update html_root_url. +# - Update doc url +# - Cargo.toml +# - README.md +# - Update CHANGELOG.md. +# - Create "http-body-x.y.z" git tag. +version = "0.4.5" +authors = [ + "Carl Lerche ", + "Lucio Franco ", + "Sean McArthur ", +] +edition = "2018" +readme = "README.md" +documentation = "https://docs.rs/http-body" +repository = "https://github.com/hyperium/http-body" +license = "MIT" +description = """ +Trait representing an asynchronous, streaming, HTTP request or response body. +""" +keywords = ["http"] +categories = ["web-programming"] + +[dependencies] +bytes = "1" +http = "0.2" diff --git a/src/lib.rs b/http-body/src/lib.rs similarity index 86% rename from src/lib.rs rename to http-body/src/lib.rs index 84efd91..d372072 100644 --- a/src/lib.rs +++ b/http-body/src/lib.rs @@ -3,7 +3,7 @@ missing_debug_implementations, missing_docs, unreachable_pub, - broken_intra_doc_links + rustdoc::broken_intra_doc_links )] #![cfg_attr(test, deny(warnings))] @@ -13,21 +13,12 @@ //! //! [`Body`]: trait.Body.html -mod empty; -mod full; -mod limited; mod next; mod size_hint; -pub mod combinators; - -pub use self::empty::Empty; -pub use self::full::Full; -pub use self::limited::{LengthLimitError, Limited}; pub use self::next::{Data, Trailers}; pub use self::size_hint::SizeHint; -use self::combinators::{BoxBody, MapData, MapErr, UnsyncBoxBody}; use bytes::{Buf, Bytes}; use http::HeaderMap; use std::convert::Infallible; @@ -98,41 +89,6 @@ pub trait Body { { Trailers(self) } - - /// Maps this body's data value to a different value. - fn map_data(self, f: F) -> MapData - where - Self: Sized, - F: FnMut(Self::Data) -> B, - B: Buf, - { - MapData::new(self, f) - } - - /// Maps this body's error value to a different value. - fn map_err(self, f: F) -> MapErr - where - Self: Sized, - F: FnMut(Self::Error) -> E, - { - MapErr::new(self, f) - } - - /// Turn this body into a boxed trait object. - fn boxed(self) -> BoxBody - where - Self: Sized + Send + Sync + 'static, - { - BoxBody::new(self) - } - - /// Turn this body into a boxed trait object that is !Sync. - fn boxed_unsync(self) -> UnsyncBoxBody - where - Self: Sized + Send + 'static, - { - UnsyncBoxBody::new(self) - } } impl Body for &mut T { diff --git a/src/next.rs b/http-body/src/next.rs similarity index 100% rename from src/next.rs rename to http-body/src/next.rs diff --git a/src/size_hint.rs b/http-body/src/size_hint.rs similarity index 100% rename from src/size_hint.rs rename to http-body/src/size_hint.rs diff --git a/tests/is_end_stream.rs b/http-body/tests/is_end_stream.rs similarity index 95% rename from tests/is_end_stream.rs rename to http-body/tests/is_end_stream.rs index beaeb0b..ca4a808 100644 --- a/tests/is_end_stream.rs +++ b/http-body/tests/is_end_stream.rs @@ -70,9 +70,8 @@ fn is_end_stream_default_false() { size_hint: SizeHint::default(), }; - assert_eq!( - false, - Pin::new(&mut mock).is_end_stream(), + assert!( + !Pin::new(&mut mock).is_end_stream(), "size_hint = {:?}", mock.size_hint.clone() );