Skip to main content

http_body_alt/
stream_impl.rs

1//! [`Stream`] trait impl for types already implemented [`Body`] trait
2//!
3//! # IMPORTANT
4//!
5//! [`Stream::size_hint`] does not provide a fixed state for expressing [`SizeHint::None`].
6//! Therefore this crate decides to use [`SizeHint::NO_BODY_HINT`] as mapped expression to it.
7
8use core::{
9    pin::Pin,
10    task::{Context, Poll, ready},
11};
12
13use futures_core::stream::Stream;
14
15use super::{
16    body::Body,
17    frame::Frame,
18    util::{StreamBody, StreamDataBody},
19};
20
21impl<B> Stream for StreamBody<B>
22where
23    B: Body,
24{
25    type Item = Result<Frame<B::Data>, B::Error>;
26
27    #[inline]
28    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
29        self.project().value.poll_frame(cx)
30    }
31
32    #[inline]
33    fn size_hint(&self) -> (usize, Option<usize>) {
34        Body::size_hint(&self.value).as_stream_size_hint()
35    }
36}
37
38impl<B> Stream for StreamDataBody<B>
39where
40    B: Body,
41{
42    type Item = Result<B::Data, B::Error>;
43
44    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
45        let opt = match ready!(self.project().value.poll_frame(cx)) {
46            Some(Ok(Frame::Data(data))) => Some(Ok(data)),
47            Some(Err(e)) => Some(Err(e)),
48            Some(_) | None => None,
49        };
50        Poll::Ready(opt)
51    }
52
53    #[inline]
54    fn size_hint(&self) -> (usize, Option<usize>) {
55        self.value.size_hint().as_stream_size_hint()
56    }
57}