From ebe57e10a38cfbc0b176285718d7b3d411133122 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Fri, 27 Sep 2019 15:44:00 -0700 Subject: [PATCH] Put Stream APIs behind unstable-stream feature --- .travis.yml | 7 +++++++ Cargo.toml | 2 ++ src/async_impl/body.rs | 17 ++++++++++++++++- src/async_impl/multipart.rs | 6 +++--- src/async_impl/response.rs | 2 +- src/lib.rs | 3 ++- tests/multipart.rs | 1 + 7 files changed, 32 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0a803b2..1a61b79 100644 --- a/.travis.yml +++ b/.travis.yml @@ -47,6 +47,13 @@ matrix: - rust: beta env: FEATURES="--features json" + # optional unstable-stream + #- rust: stable + - rust: beta + env: FEATURES="--features unstable-stream" + + + # socks #- rust: stable # env: FEATURES="--features socks" diff --git a/Cargo.toml b/Cargo.toml index 4cfa10f..bb1977e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,8 @@ json = ["serde_json"] #trust-dns = ["trust-dns-resolver"] +unstable-stream = [] + [dependencies] http = "0.1.15" url = "2.1" diff --git a/src/async_impl/body.rs b/src/async_impl/body.rs index e26918d..b218b99 100644 --- a/src/async_impl/body.rs +++ b/src/async_impl/body.rs @@ -5,7 +5,6 @@ use std::task::{Context, Poll}; use bytes::Bytes; use futures_core::Stream; -use futures_util::TryStreamExt; use http_body::Body as HttpBody; use tokio::timer::Delay; @@ -55,12 +54,28 @@ impl Body { /// let body = Body::wrap_stream(stream); /// # } /// ``` + /// + /// # Unstable + /// + /// This requires the `unstable-stream` feature to be enabled. + #[cfg(feature = "unstable-stream")] pub fn wrap_stream(stream: S) -> Body where S: futures_core::stream::TryStream + Send + Sync + 'static, S::Error: Into>, hyper::Chunk: From, { + Body::stream(stream) + } + + pub(crate) fn stream(stream: S) -> Body + where + S: futures_core::stream::TryStream + Send + Sync + 'static, + S::Error: Into>, + hyper::Chunk: From, + { + use futures_util::TryStreamExt; + let body = Box::pin(WrapStream( stream.map_ok(hyper::Chunk::from).map_err(Into::into), )); diff --git a/src/async_impl/multipart.rs b/src/async_impl/multipart.rs index 78d08cd..3598dab 100644 --- a/src/async_impl/multipart.rs +++ b/src/async_impl/multipart.rs @@ -121,7 +121,7 @@ impl Form { let last = stream::once(future::ready(Ok( format!("--{}--\r\n", self.boundary()).into() ))); - Body::wrap_stream(stream.chain(last)) + Body::stream(stream.chain(last)) } /// Generate a hyper::Body stream for a single Part instance of a Form request. @@ -505,7 +505,7 @@ mod tests { let mut form = Form::new() .part( "reader1", - Part::stream(Body::wrap_stream(stream::once(future::ready::< + Part::stream(Body::stream(stream::once(future::ready::< Result, >(Ok( "part1".to_owned(), @@ -515,7 +515,7 @@ mod tests { .part("key2", Part::text("value2").mime(mime::IMAGE_BMP)) .part( "reader2", - Part::stream(Body::wrap_stream(stream::once(future::ready::< + Part::stream(Body::stream(stream::once(future::ready::< Result, >(Ok( "part2".to_owned(), diff --git a/src/async_impl/response.rs b/src/async_impl/response.rs index c82a764..c6c5af6 100644 --- a/src/async_impl/response.rs +++ b/src/async_impl/response.rs @@ -394,7 +394,7 @@ impl> From> for Response { /// A `Response` can be piped as the `Body` of another request. impl From for Body { fn from(r: Response) -> Body { - Body::wrap_stream(r.body) + Body::stream(r.body) } } diff --git a/src/lib.rs b/src/lib.rs index f6d36fc..98a0445 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -156,11 +156,11 @@ //! - **default-tls** *(enabled by default)*: Provides TLS support via the //! `native-tls` library to connect over HTTPS. //! - **default-tls-vendored**: Enables the `vendored` feature of `native-tls`. -//! - **rustls-tls**: Provides TLS support via the `rustls` library. //! - **blocking**: Provides the [blocking][] client API. //! - **cookies**: Provides cookie session support. //! - **gzip**: Provides response body gzip decompression. //! - **json**: Provides serialization and deserialization for JSON bodies. +//! - **unstable-stream** *(unstable)*: Adds support for `futures::Stream`. //! //! //! [hyper]: http://hyper.rs @@ -173,6 +173,7 @@ //! [Proxy]: ./struct.Proxy.html //! [cargo-features]: https://doc.rust-lang.org/stable/cargo/reference/manifest.html#the-features-section +////! - **rustls-tls**: Provides TLS support via the `rustls` library. ////! - **socks**: Provides SOCKS5 proxy support. ////! - **trust-dns**: Enables a trust-dns async resolver instead of default ////! threadpool using `getaddrinfo`. diff --git a/tests/multipart.rs b/tests/multipart.rs index a2eda93..ad22f82 100644 --- a/tests/multipart.rs +++ b/tests/multipart.rs @@ -51,6 +51,7 @@ async fn text_part() { assert_eq!(res.status(), reqwest::StatusCode::OK); } +#[cfg(feature = "unstable-stream")] #[tokio::test] async fn stream_part() { use futures_util::{future, stream};