Skip to content

Commit

Permalink
make Response::bytes_stream return a concrete type
Browse files Browse the repository at this point in the history
  • Loading branch information
its-the-shrimp committed Sep 21, 2024
1 parent d85f44b commit 98b0fe3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/async_impl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub use self::body::Body;
pub use self::client::{Client, ClientBuilder};
pub use self::request::{Request, RequestBuilder};
pub use self::response::Response;
pub use self::response::{Response, BytesStream};
pub use self::upgrade::Upgraded;

#[cfg(feature = "blocking")]
Expand Down
27 changes: 23 additions & 4 deletions src/async_impl/response.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use std::fmt;
use std::net::SocketAddr;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;

use bytes::Bytes;
use futures_core::Stream;
use futures_util::StreamExt;
use http_body_util::BodyExt;
use hyper::{HeaderMap, StatusCode, Version};
use hyper_util::client::legacy::connect::HttpInfo;
#[cfg(feature = "json")]
use serde::de::DeserializeOwned;
#[cfg(feature = "json")]
use serde_json;
use tokio::time::Sleep;
use url::Url;

Expand All @@ -25,6 +26,24 @@ use encoding_rs::{Encoding, UTF_8};
#[cfg(feature = "charset")]
use mime::Mime;

/// Return type of [`Response::bytes_stream`]
#[cfg(feature = "stream")]
#[cfg_attr(docsrs, doc(cfg(feature = "stream")))]
#[expect(missing_debug_implementations)]
pub struct BytesStream(super::body::DataStream<Decoder>);

impl Stream for BytesStream {
type Item = crate::Result<Bytes>;

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.0.poll_next_unpin(cx)
}

fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}

/// A Response to a submitted `Request`.
pub struct Response {
pub(super) res: hyper::Response<Decoder>,
Expand Down Expand Up @@ -350,8 +369,8 @@ impl Response {
/// This requires the optional `stream` feature to be enabled.
#[cfg(feature = "stream")]
#[cfg_attr(docsrs, doc(cfg(feature = "stream")))]
pub fn bytes_stream(self) -> impl futures_core::Stream<Item = crate::Result<Bytes>> {
super::body::DataStream(self.res.into_body())
pub fn bytes_stream(self) -> BytesStream {
BytesStream(super::body::DataStream(self.res.into_body()))
}

// util methods
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ if_hyper! {
doctest!("../README.md");

pub use self::async_impl::{
Body, Client, ClientBuilder, Request, RequestBuilder, Response, Upgraded,
Body, Client, ClientBuilder, Request, RequestBuilder, Response, Upgraded, BytesStream,
};
pub use self::proxy::{Proxy,NoProxy};
#[cfg(feature = "__tls")]
Expand Down

0 comments on commit 98b0fe3

Please sign in to comment.