Skip to content

Commit 3c09567

Browse files
committed
Move utilities and combinators to a separate package
Hyper works with all combinators removed. Removing methods from the trait is a breaking change, but it is possible to do the semver trick (release 0.5, then release 0.4.x with a dependency on the 0.5 trait) to not split the ecosystem. Addresses #52.
1 parent a97da64 commit 3c09567

File tree

11 files changed

+104
-55
lines changed

11 files changed

+104
-55
lines changed

.github/workflows/CI.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ jobs:
2727
- name: Install Rust
2828
run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
2929
- name: Run tests
30-
run: cargo test
30+
run: cargo test --workspace

Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ Trait representing an asynchronous, streaming, HTTP request or response body.
2424
"""
2525
keywords = ["http"]
2626
categories = ["web-programming"]
27+
resolver = "2"
28+
29+
[workspace]
30+
members = ["util"]
2731

2832
[dependencies]
2933
bytes = "1"
3034
http = "0.2"
31-
pin-project-lite = "0.2"
32-
33-
[dev-dependencies]
34-
tokio = { version = "1", features = ["macros", "rt"] }

src/lib.rs

-42
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,12 @@
1313
//!
1414
//! [`Body`]: trait.Body.html
1515
16-
mod empty;
17-
mod full;
1816
mod next;
1917
mod size_hint;
2018

21-
pub mod combinators;
22-
23-
pub use self::empty::Empty;
24-
pub use self::full::Full;
2519
pub use self::next::{Data, Trailers};
2620
pub use self::size_hint::SizeHint;
2721

28-
use self::combinators::{BoxBody, MapData, MapErr, UnsyncBoxBody};
2922
use bytes::Buf;
3023
use http::HeaderMap;
3124
use std::ops;
@@ -95,41 +88,6 @@ pub trait Body {
9588
{
9689
Trailers(self)
9790
}
98-
99-
/// Maps this body's data value to a different value.
100-
fn map_data<F, B>(self, f: F) -> MapData<Self, F>
101-
where
102-
Self: Sized,
103-
F: FnMut(Self::Data) -> B,
104-
B: Buf,
105-
{
106-
MapData::new(self, f)
107-
}
108-
109-
/// Maps this body's error value to a different value.
110-
fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
111-
where
112-
Self: Sized,
113-
F: FnMut(Self::Error) -> E,
114-
{
115-
MapErr::new(self, f)
116-
}
117-
118-
/// Turn this body into a boxed trait object.
119-
fn boxed(self) -> BoxBody<Self::Data, Self::Error>
120-
where
121-
Self: Sized + Send + Sync + 'static,
122-
{
123-
BoxBody::new(self)
124-
}
125-
126-
/// Turn this body into a boxed trait object that is !Sync.
127-
fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error>
128-
where
129-
Self: Sized + Send + 'static,
130-
{
131-
UnsyncBoxBody::new(self)
132-
}
13391
}
13492

13593
impl<T: Body + Unpin + ?Sized> Body for &mut T {

util/Cargo.toml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "http-body-util"
3+
version = "0.4.4"
4+
authors = [
5+
"Carl Lerche <[email protected]>",
6+
"Lucio Franco <[email protected]>",
7+
"Sean McArthur <[email protected]>",
8+
]
9+
edition = "2018"
10+
readme = "README.md"
11+
documentation = "https://docs.rs/http-body-util"
12+
repository = "https://github.com/hyperium/http-body"
13+
license = "MIT"
14+
description = """
15+
Combinators and adapters for HTTP request or response bodies.
16+
"""
17+
keywords = ["http"]
18+
categories = ["web-programming"]
19+
resolver = "2"
20+
21+
[dependencies]
22+
bytes = "1"
23+
http = "0.2"
24+
http-body = { path = ".." }
25+
pin-project-lite = "0.2"
26+
27+
[dev-dependencies]
28+
tokio = { version = "1", features = ["macros", "rt"] }

src/combinators/box_body.rs renamed to util/src/combinators/box_body.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::Body;
1+
use crate::BodyExt as _;
2+
3+
use http_body::{Body, SizeHint};
24
use bytes::Buf;
35
use std::{
46
fmt,
@@ -60,7 +62,7 @@ where
6062
self.inner.is_end_stream()
6163
}
6264

63-
fn size_hint(&self) -> crate::SizeHint {
65+
fn size_hint(&self) -> SizeHint {
6466
self.inner.size_hint()
6567
}
6668
}
@@ -119,7 +121,7 @@ where
119121
self.inner.is_end_stream()
120122
}
121123

122-
fn size_hint(&self) -> crate::SizeHint {
124+
fn size_hint(&self) -> SizeHint {
123125
self.inner.size_hint()
124126
}
125127
}

src/combinators/map_data.rs renamed to util/src/combinators/map_data.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::Body;
1+
use http_body::Body;
22
use bytes::Buf;
33
use pin_project_lite::pin_project;
44
use std::{

src/combinators/map_err.rs renamed to util/src/combinators/map_err.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::Body;
1+
use http_body::{Body, SizeHint};
22
use pin_project_lite::pin_project;
33
use std::{
44
any::type_name,
@@ -79,7 +79,7 @@ where
7979
self.inner.is_end_stream()
8080
}
8181

82-
fn size_hint(&self) -> crate::SizeHint {
82+
fn size_hint(&self) -> SizeHint {
8383
self.inner.size_hint()
8484
}
8585
}
File renamed without changes.

src/empty.rs renamed to util/src/empty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{Body, SizeHint};
1+
use http_body::{Body, SizeHint};
22
use bytes::Buf;
33
use http::HeaderMap;
44
use std::{

src/full.rs renamed to util/src/full.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Body, SizeHint};
1+
use http_body::{Body, SizeHint};
22
use bytes::{Buf, Bytes};
33
use http::HeaderMap;
44
use pin_project_lite::pin_project;

util/src/lib.rs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#![deny(
2+
missing_debug_implementations,
3+
missing_docs,
4+
unreachable_pub,
5+
broken_intra_doc_links
6+
)]
7+
#![cfg_attr(test, deny(warnings))]
8+
9+
//! Utilities for [`http_body::Body`].
10+
//!
11+
//! [`BodyExt`] adds extensions to the common trait.
12+
//!
13+
//! [`Empty`] and [`Full`] provide simple implementations.
14+
15+
pub mod combinators;
16+
mod empty;
17+
mod full;
18+
19+
pub use self::empty::Empty;
20+
pub use self::full::Full;
21+
use self::combinators::{BoxBody, MapData, MapErr, UnsyncBoxBody};
22+
23+
/// An extension trait for [`http_body::Body`] adding various combinators and adapters
24+
pub trait BodyExt: http_body::Body {
25+
/// Maps this body's data value to a different value.
26+
fn map_data<F, B>(self, f: F) -> MapData<Self, F>
27+
where
28+
Self: Sized,
29+
F: FnMut(Self::Data) -> B,
30+
B: bytes::Buf,
31+
{
32+
MapData::new(self, f)
33+
}
34+
35+
/// Maps this body's error value to a different value.
36+
fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
37+
where
38+
Self: Sized,
39+
F: FnMut(Self::Error) -> E,
40+
{
41+
MapErr::new(self, f)
42+
}
43+
44+
/// Turn this body into a boxed trait object.
45+
fn boxed(self) -> BoxBody<Self::Data, Self::Error>
46+
where
47+
Self: Sized + Send + Sync + 'static,
48+
{
49+
BoxBody::new(self)
50+
}
51+
52+
/// Turn this body into a boxed trait object that is !Sync.
53+
fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error>
54+
where
55+
Self: Sized + Send + 'static,
56+
{
57+
UnsyncBoxBody::new(self)
58+
}
59+
}
60+
61+
impl<T: ?Sized> BodyExt for T where T: http_body::Body {}

0 commit comments

Comments
 (0)