use std::convert::Infallible;
use std::pin::Pin;
use std::task::{Context, Poll};
use axum::body::{Body, Bytes, HttpBody};
use http_body::Frame;
use tokio::sync::mpsc;
pub(crate) fn channel() -> (mpsc::Sender<Bytes>, Body) {
let (tx, rx) = mpsc::channel(1);
(tx, Body::new(ChannelBody { rx }))
}
struct ChannelBody {
rx: mpsc::Receiver<Bytes>,
}
impl HttpBody for ChannelBody {
type Data = Bytes;
type Error = Infallible;
fn poll_frame(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Bytes>, Infallible>>> {
self.get_mut()
.rx
.poll_recv(cx)
.map(|b| b.map(|b| Ok(Frame::data(b))))
}
}