Skip to main content

hyper_util/rt/
io.rs

1use std::pin::Pin;
2use std::task::{Poll, ready};
3
4use hyper::rt::{Read, ReadBuf, Write};
5
6use std::future::poll_fn;
7
8pub(crate) async fn read<T>(io: &mut T, buf: &mut [u8]) -> Result<usize, std::io::Error>
9where
10    T: Read + Unpin,
11{
12    poll_fn(move |cx| {
13        let mut buf = ReadBuf::new(buf);
14        ready!(Pin::new(&mut *io).poll_read(cx, buf.unfilled()))?;
15        Poll::Ready(Ok(buf.filled().len()))
16    })
17    .await
18}
19
20pub(crate) async fn write_all<T>(io: &mut T, buf: &[u8]) -> Result<(), std::io::Error>
21where
22    T: Write + Unpin,
23{
24    let mut n = 0;
25    poll_fn(move |cx| {
26        while n < buf.len() {
27            n += ready!(Pin::new(&mut *io).poll_write(cx, &buf[n..])?);
28        }
29        Poll::Ready(Ok(()))
30    })
31    .await
32}