Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BodyStream. #91

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion http-body-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub use self::either::Either;
pub use self::empty::Empty;
pub use self::full::Full;
pub use self::limited::{LengthLimitError, Limited};
pub use self::stream::StreamBody;
pub use self::stream::{BodyStream, StreamBody};

/// An extension trait for [`http_body::Body`] adding various combinators and adapters
pub trait BodyExt: http_body::Body {
Expand Down
98 changes: 97 additions & 1 deletion http-body-util/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,57 @@ impl<S: Stream> Stream for StreamBody<S> {
}
}

pin_project! {
/// A stream created from a [`Body`].
#[derive(Clone, Copy, Debug)]
pub struct BodyStream<B> {
#[pin]
body: B,
}
}

impl<B> BodyStream<B> {
/// Create a new `BodyStream`.
pub fn new(body: B) -> Self {
Self { body }
}
}

impl<B> Body for BodyStream<B>
where
B: Body,
{
type Data = B::Data;
type Error = B::Error;

fn poll_frame(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
self.project().body.poll_frame(cx)
}
}

impl<B> Stream for BodyStream<B>
where
B: Body,
{
type Item = Result<Frame<B::Data>, B::Error>;

fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match self.project().body.poll_frame(cx) {
Poll::Ready(Some(frame)) => Poll::Ready(Some(frame)),
Poll::Ready(None) => Poll::Ready(None),
Poll::Pending => Poll::Pending,
}
}
}

#[cfg(test)]
mod tests {
use crate::{BodyExt, StreamBody};
use crate::{BodyExt, BodyStream, StreamBody};
use bytes::Bytes;
use futures_util::StreamExt;
use http_body::Frame;
use std::convert::Infallible;

Expand Down Expand Up @@ -105,4 +152,53 @@ mod tests {

assert!(body.frame().await.is_none());
}

#[tokio::test]
async fn stream_from_body() {
let chunks: Vec<Result<_, Infallible>> = vec![
Ok(Frame::data(Bytes::from(vec![1]))),
Ok(Frame::data(Bytes::from(vec![2]))),
Ok(Frame::data(Bytes::from(vec![3]))),
];
let stream = futures_util::stream::iter(chunks);
let body = StreamBody::new(stream);

let mut stream = BodyStream::new(body);

assert_eq!(
stream
.next()
.await
.unwrap()
.unwrap()
.into_data()
.unwrap()
.as_ref(),
[1]
);
assert_eq!(
stream
.next()
.await
.unwrap()
.unwrap()
.into_data()
.unwrap()
.as_ref(),
[2]
);
assert_eq!(
stream
.next()
.await
.unwrap()
.unwrap()
.into_data()
.unwrap()
.as_ref(),
[3]
);

assert!(stream.next().await.is_none());
}
}