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