use bytes::Bytes;
use futures_core::Stream;
use http_body::{Body, Frame};
use hyper::body::Incoming;
use std::pin::Pin;
use std::task::{Context, Poll};
use crate::body::size_hint;
use crate::body::AnyBody;
use crate::Error;
#[must_use = "streams do nothing unless polled"]
pub struct BodyStream {
body: AnyBody<Incoming>,
}
impl BodyStream {
pub(crate) fn new(body: AnyBody<Incoming>) -> Self {
Self { body }
}
}
impl BodyStream {
fn project(self: Pin<&mut Self>) -> Pin<&mut AnyBody<Incoming>> {
let this = self.get_mut();
let ptr = &mut this.body;
Pin::new(ptr)
}
}
impl Stream for BodyStream {
type Item = Result<Frame<Bytes>, Error>;
fn poll_next(self: Pin<&mut Self>, context: &mut Context) -> Poll<Option<Self::Item>> {
self.project().poll_frame(context)
}
fn size_hint(&self) -> (usize, Option<usize>) {
let hint = self.body.size_hint();
size_hint::from_body_for_stream(hint)
}
}