hyper_util/rt/
io.rs

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