1use bytes::Bytes;
2use http_body::{Body as HttpBody, Frame};
3use std::convert::Infallible;
4use std::pin::Pin;
5use std::task::{Context, Poll};
6use tokio::sync::mpsc;
7
8pub enum Body {
9 Full(Option<Bytes>),
10 Chan(mpsc::Receiver<Bytes>),
11}
12
13impl Body {
14 pub fn full(b: impl Into<Bytes>) -> Self {
15 Body::Full(Some(b.into()))
16 }
17}
18
19impl HttpBody for Body {
20 type Data = Bytes;
21 type Error = Infallible;
22
23 fn poll_frame(
24 mut self: Pin<&mut Self>,
25 cx: &mut Context<'_>,
26 ) -> Poll<Option<Result<Frame<Bytes>, Infallible>>> {
27 match &mut *self {
28 Body::Full(b) => Poll::Ready(
29 b.take()
30 .filter(|b| !b.is_empty())
31 .map(|b| Ok(Frame::data(b))),
32 ),
33 Body::Chan(rx) => rx.poll_recv(cx).map(|o| o.map(|b| Ok(Frame::data(b)))),
34 }
35 }
36}