hyper_async_io/tokio/
asyncread.rs

1use crate::BodyReader;
2use futures::StreamExt;
3
4impl tokio::io::AsyncRead for BodyReader {
5    fn poll_read(
6        self: std::pin::Pin<&mut Self>,
7        cx: &mut std::task::Context<'_>,
8        buf: &mut tokio::io::ReadBuf<'_>,
9    ) -> std::task::Poll<std::io::Result<()>> {
10        let this = self.get_mut();
11        match this.body.poll_next_unpin(cx) {
12            std::task::Poll::Ready(Some(Ok(chunk))) => {
13                buf.put_slice(&chunk);
14                std::task::Poll::Ready(Ok(()))
15            }
16            std::task::Poll::Ready(Some(Err(e))) => std::task::Poll::Ready(Err(
17                std::io::Error::new(std::io::ErrorKind::Other, format!("BodyReader: {}", e)),
18            )),
19            std::task::Poll::Ready(None) => std::task::Poll::Ready(Ok(())),
20            std::task::Poll::Pending => std::task::Poll::Pending,
21        }
22    }
23}