1use std::marker::Unpin;
2use std::pin::Pin;
3use std::task::{ready, Poll};
4
5use hyper::rt::{Read, ReadBuf, Write};
6
7use std::future::poll_fn;
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 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 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}