pub trait AsyncRead: Send {
fn async_read(&mut self, buf: &mut [u8])
-> impl Future<Output = std::io::Result<usize>> + Send;
fn async_read_exact(
&mut self,
buf: &mut [u8],
) -> impl Future<Output = std::io::Result<()>> + Send {
async move {
let mut buf = buf;
while !buf.is_empty() {
let n = self.async_read(buf).await?;
buf = &mut buf[n..];
}
Ok(())
}
}
}
pub trait AsyncWrite: Send {
fn async_write(&mut self, buf: &[u8]) -> impl Future<Output = std::io::Result<usize>> + Send;
fn async_write_all(&mut self, buf: &[u8]) -> impl Future<Output = std::io::Result<()>> + Send {
async move {
let mut buf = buf;
while !buf.is_empty() {
let n = self.async_write(buf).await?;
buf = &buf[n..];
}
Ok(())
}
}
}