Skip to content

Commit ce72f73

Browse files
authored
feat(lib): remove stream cargo feature (#2896)
Closes #2855
1 parent a563404 commit ce72f73

File tree

14 files changed

+272
-410
lines changed

14 files changed

+272
-410
lines changed

.github/workflows/CI.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ jobs:
144144

145145
- name: Test
146146
# Can't enable tcp feature since Miri does not support the tokio runtime
147-
run: MIRIFLAGS="-Zmiri-disable-isolation" cargo miri test --features http1,http2,client,server,stream,nightly
147+
run: MIRIFLAGS="-Zmiri-disable-isolation" cargo miri test --features http1,http2,client,server,nightly
148148

149149
features:
150150
name: features

Cargo.toml

+2-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ futures-core = { version = "0.3", default-features = false }
2727
futures-channel = "0.3"
2828
futures-util = { version = "0.3", default-features = false }
2929
http = "0.2"
30-
http-body = "0.4"
30+
http-body = { git = "https://github.com/hyperium/http-body", branch = "master" }
31+
http-body-util = { git = "https://github.com/hyperium/http-body", branch = "master" }
3132
httpdate = "1.0"
3233
httparse = "1.6"
3334
h2 = { version = "0.3.9", optional = true }
@@ -80,7 +81,6 @@ full = [
8081
"http1",
8182
"http2",
8283
"server",
83-
"stream",
8484
"runtime",
8585
]
8686

@@ -92,9 +92,6 @@ http2 = ["h2"]
9292
client = []
9393
server = []
9494

95-
# `impl Stream` for things
96-
stream = []
97-
9895
# Tokio support
9996
runtime = [
10097
"tcp",

benches/body.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ extern crate test;
66
use bytes::Buf;
77
use futures_util::stream;
88
use futures_util::StreamExt;
9-
use hyper::body::Body;
9+
use http_body_util::StreamBody;
1010

1111
macro_rules! bench_stream {
1212
($bencher:ident, bytes: $bytes:expr, count: $count:expr, $total_ident:ident, $body_pat:pat, $block:expr) => {{
@@ -20,9 +20,10 @@ macro_rules! bench_stream {
2020

2121
$bencher.iter(|| {
2222
rt.block_on(async {
23-
let $body_pat = Body::wrap_stream(
23+
let $body_pat = StreamBody::new(
2424
stream::iter(__s.iter()).map(|&s| Ok::<_, std::convert::Infallible>(s)),
2525
);
26+
2627
$block;
2728
});
2829
});

benches/server.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ use std::sync::mpsc;
99
use std::time::Duration;
1010

1111
use futures_util::{stream, StreamExt};
12+
use http_body_util::StreamBody;
1213
use tokio::sync::oneshot;
1314

1415
use hyper::service::{make_service_fn, service_fn};
15-
use hyper::{Body, Response, Server};
16+
use hyper::{Response, Server};
1617

1718
macro_rules! bench_server {
1819
($b:ident, $header:expr, $body:expr) => {{
@@ -101,7 +102,7 @@ fn throughput_fixedsize_large_payload(b: &mut test::Bencher) {
101102
fn throughput_fixedsize_many_chunks(b: &mut test::Bencher) {
102103
bench_server!(b, ("content-length", "1000000"), || {
103104
static S: &[&[u8]] = &[&[b'x'; 1_000] as &[u8]; 1_000] as _;
104-
Body::wrap_stream(stream::iter(S.iter()).map(|&s| Ok::<_, String>(s)))
105+
StreamBody::new(stream::iter(S.iter()).map(|&s| Ok::<_, String>(s)))
105106
})
106107
}
107108

@@ -123,7 +124,7 @@ fn throughput_chunked_large_payload(b: &mut test::Bencher) {
123124
fn throughput_chunked_many_chunks(b: &mut test::Bencher) {
124125
bench_server!(b, ("transfer-encoding", "chunked"), || {
125126
static S: &[&[u8]] = &[&[b'x'; 1_000] as &[u8]; 1_000] as _;
126-
Body::wrap_stream(stream::iter(S.iter()).map(|&s| Ok::<_, String>(s)))
127+
StreamBody::new(stream::iter(S.iter()).map(|&s| Ok::<_, String>(s)))
127128
})
128129
}
129130

examples/echo.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![deny(warnings)]
22

3-
use futures_util::TryStreamExt;
43
use hyper::service::{make_service_fn, service_fn};
54
use hyper::{Body, Method, Request, Response, Server, StatusCode};
65

@@ -16,16 +15,17 @@ async fn echo(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
1615
// Simply echo the body back to the client.
1716
(&Method::POST, "/echo") => Ok(Response::new(req.into_body())),
1817

18+
// TODO: Fix this, broken in PR #2896
1919
// Convert to uppercase before sending back to client using a stream.
20-
(&Method::POST, "/echo/uppercase") => {
21-
let chunk_stream = req.into_body().map_ok(|chunk| {
22-
chunk
23-
.iter()
24-
.map(|byte| byte.to_ascii_uppercase())
25-
.collect::<Vec<u8>>()
26-
});
27-
Ok(Response::new(Body::wrap_stream(chunk_stream)))
28-
}
20+
// (&Method::POST, "/echo/uppercase") => {
21+
// let chunk_stream = req.into_body().map_ok(|chunk| {
22+
// chunk
23+
// .iter()
24+
// .map(|byte| byte.to_ascii_uppercase())
25+
// .collect::<Vec<u8>>()
26+
// });
27+
// Ok(Response::new(Body::wrap_stream(chunk_stream)))
28+
// }
2929

3030
// Reverse the entire body before sending back to the client.
3131
//

examples/send_file.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#![deny(warnings)]
22

3-
use tokio::fs::File;
4-
5-
use tokio_util::codec::{BytesCodec, FramedRead};
6-
73
use hyper::service::{make_service_fn, service_fn};
84
use hyper::{Body, Method, Request, Response, Result, Server, StatusCode};
95

@@ -48,11 +44,8 @@ fn not_found() -> Response<Body> {
4844
}
4945

5046
async fn simple_file_send(filename: &str) -> Result<Response<Body>> {
51-
// Serve a file by asynchronously reading it by chunks using tokio-util crate.
52-
53-
if let Ok(file) = File::open(filename).await {
54-
let stream = FramedRead::new(file, BytesCodec::new());
55-
let body = Body::wrap_stream(stream);
47+
if let Ok(contents) = tokio::fs::read(filename).await {
48+
let body = contents.into();
5649
return Ok(Response::new(body));
5750
}
5851

examples/web_api.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![deny(warnings)]
22

33
use bytes::Buf;
4-
use futures_util::{stream, StreamExt};
54
use hyper::client::HttpConnector;
65
use hyper::service::{make_service_fn, service_fn};
76
use hyper::{header, Body, Client, Method, Request, Response, Server, StatusCode};
@@ -24,18 +23,10 @@ async fn client_request_response(client: &Client<HttpConnector>) -> Result<Respo
2423
.unwrap();
2524

2625
let web_res = client.request(req).await?;
27-
// Compare the JSON we sent (before) with what we received (after):
28-
let before = stream::once(async {
29-
Ok(format!(
30-
"<b>POST request body</b>: {}<br><b>Response</b>: ",
31-
POST_DATA,
32-
)
33-
.into())
34-
});
35-
let after = web_res.into_body();
36-
let body = Body::wrap_stream(before.chain(after));
3726

38-
Ok(Response::new(body))
27+
let res_body = web_res.into_body();
28+
29+
Ok(Response::new(res_body))
3930
}
4031

4132
async fn api_post_response(req: Request<Body>) -> Result<Response<Body>> {

src/body/body.rs

-82
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
use std::borrow::Cow;
2-
#[cfg(feature = "stream")]
3-
use std::error::Error as StdError;
42
use std::fmt;
53

64
use bytes::Bytes;
75
use futures_channel::mpsc;
86
use futures_channel::oneshot;
97
use futures_core::Stream; // for mpsc::Receiver
10-
#[cfg(feature = "stream")]
11-
use futures_util::TryStreamExt;
128
use http::HeaderMap;
139
use http_body::{Body as HttpBody, SizeHint};
1410

1511
use super::DecodedLength;
16-
#[cfg(feature = "stream")]
17-
use crate::common::sync_wrapper::SyncWrapper;
1812
use crate::common::Future;
1913
#[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))]
2014
use crate::common::Never;
@@ -56,12 +50,6 @@ enum Kind {
5650
},
5751
#[cfg(feature = "ffi")]
5852
Ffi(crate::ffi::UserBody),
59-
#[cfg(feature = "stream")]
60-
Wrapped(
61-
SyncWrapper<
62-
Pin<Box<dyn Stream<Item = Result<Bytes, Box<dyn StdError + Send + Sync>>> + Send>>,
63-
>,
64-
),
6553
}
6654

6755
struct Extra {
@@ -164,39 +152,6 @@ impl Body {
164152
(tx, rx)
165153
}
166154

167-
/// Wrap a futures `Stream` in a box inside `Body`.
168-
///
169-
/// # Example
170-
///
171-
/// ```
172-
/// # use hyper::Body;
173-
/// let chunks: Vec<Result<_, std::io::Error>> = vec![
174-
/// Ok("hello"),
175-
/// Ok(" "),
176-
/// Ok("world"),
177-
/// ];
178-
///
179-
/// let stream = futures_util::stream::iter(chunks);
180-
///
181-
/// let body = Body::wrap_stream(stream);
182-
/// ```
183-
///
184-
/// # Optional
185-
///
186-
/// This function requires enabling the `stream` feature in your
187-
/// `Cargo.toml`.
188-
#[cfg(feature = "stream")]
189-
#[cfg_attr(docsrs, doc(cfg(feature = "stream")))]
190-
pub fn wrap_stream<S, O, E>(stream: S) -> Body
191-
where
192-
S: Stream<Item = Result<O, E>> + Send + 'static,
193-
O: Into<Bytes> + 'static,
194-
E: Into<Box<dyn StdError + Send + Sync>> + 'static,
195-
{
196-
let mapped = stream.map_ok(Into::into).map_err(Into::into);
197-
Body::new(Kind::Wrapped(SyncWrapper::new(Box::pin(mapped))))
198-
}
199-
200155
fn new(kind: Kind) -> Body {
201156
Body { kind, extra: None }
202157
}
@@ -329,12 +284,6 @@ impl Body {
329284

330285
#[cfg(feature = "ffi")]
331286
Kind::Ffi(ref mut body) => body.poll_data(cx),
332-
333-
#[cfg(feature = "stream")]
334-
Kind::Wrapped(ref mut s) => match ready!(s.get_mut().as_mut().poll_next(cx)) {
335-
Some(res) => Poll::Ready(Some(res.map_err(crate::Error::new_body))),
336-
None => Poll::Ready(None),
337-
},
338287
}
339288
}
340289

@@ -405,8 +354,6 @@ impl HttpBody for Body {
405354
Kind::H2 { recv: ref h2, .. } => h2.is_end_stream(),
406355
#[cfg(feature = "ffi")]
407356
Kind::Ffi(..) => false,
408-
#[cfg(feature = "stream")]
409-
Kind::Wrapped(..) => false,
410357
}
411358
}
412359

@@ -426,8 +373,6 @@ impl HttpBody for Body {
426373
match self.kind {
427374
Kind::Once(Some(ref val)) => SizeHint::with_exact(val.len() as u64),
428375
Kind::Once(None) => SizeHint::with_exact(0),
429-
#[cfg(feature = "stream")]
430-
Kind::Wrapped(..) => SizeHint::default(),
431376
Kind::Chan { content_length, .. } => opt_len!(content_length),
432377
#[cfg(all(feature = "http2", any(feature = "client", feature = "server")))]
433378
Kind::H2 { content_length, .. } => opt_len!(content_length),
@@ -457,33 +402,6 @@ impl fmt::Debug for Body {
457402
}
458403
}
459404

460-
/// # Optional
461-
///
462-
/// This function requires enabling the `stream` feature in your
463-
/// `Cargo.toml`.
464-
#[cfg(feature = "stream")]
465-
impl Stream for Body {
466-
type Item = crate::Result<Bytes>;
467-
468-
fn poll_next(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Option<Self::Item>> {
469-
HttpBody::poll_data(self, cx)
470-
}
471-
}
472-
473-
/// # Optional
474-
///
475-
/// This function requires enabling the `stream` feature in your
476-
/// `Cargo.toml`.
477-
#[cfg(feature = "stream")]
478-
impl From<Box<dyn Stream<Item = Result<Bytes, Box<dyn StdError + Send + Sync>>> + Send>> for Body {
479-
#[inline]
480-
fn from(
481-
stream: Box<dyn Stream<Item = Result<Bytes, Box<dyn StdError + Send + Sync>>> + Send>,
482-
) -> Body {
483-
Body::new(Kind::Wrapped(SyncWrapper::new(stream.into())))
484-
}
485-
}
486-
487405
impl From<Bytes> for Body {
488406
#[inline]
489407
fn from(chunk: Bytes) -> Body {

src/common/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ pub(crate) mod io;
1818
#[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))]
1919
mod lazy;
2020
mod never;
21-
#[cfg(any(
22-
feature = "stream",
23-
all(feature = "client", any(feature = "http1", feature = "http2"))
24-
))]
21+
#[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))]
2522
pub(crate) mod sync_wrapper;
2623
pub(crate) mod task;
2724
pub(crate) mod watch;

src/error.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub(super) enum Kind {
4848
#[cfg(all(feature = "http1", feature = "server", feature = "runtime"))]
4949
HeaderTimeout,
5050
/// Error while reading a body from connection.
51-
#[cfg(any(feature = "http1", feature = "http2", feature = "stream"))]
51+
#[cfg(any(feature = "http1", feature = "http2"))]
5252
Body,
5353
/// Error while writing a body to connection.
5454
#[cfg(any(feature = "http1", feature = "http2"))]
@@ -294,7 +294,7 @@ impl Error {
294294
Error::new(Kind::ChannelClosed)
295295
}
296296

297-
#[cfg(any(feature = "http1", feature = "http2", feature = "stream"))]
297+
#[cfg(any(feature = "http1", feature = "http2"))]
298298
pub(super) fn new_body<E: Into<Cause>>(cause: E) -> Error {
299299
Error::new(Kind::Body).with(cause)
300300
}
@@ -440,7 +440,7 @@ impl Error {
440440
Kind::Accept => "error accepting connection",
441441
#[cfg(all(feature = "http1", feature = "server", feature = "runtime"))]
442442
Kind::HeaderTimeout => "read header from client timeout",
443-
#[cfg(any(feature = "http1", feature = "http2", feature = "stream"))]
443+
#[cfg(any(feature = "http1", feature = "http2"))]
444444
Kind::Body => "error reading a body from connection",
445445
#[cfg(any(feature = "http1", feature = "http2"))]
446446
Kind::BodyWrite => "error writing a body to connection",

src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
//! - `runtime`: Enables convenient integration with `tokio`, providing
5353
//! connectors and acceptors for TCP, and a default executor.
5454
//! - `tcp`: Enables convenient implementations over TCP (using tokio).
55-
//! - `stream`: Provides `futures::Stream` capabilities.
5655
//!
5756
//! [feature flags]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section
5857

0 commit comments

Comments
 (0)