Skip to content

Commit 26e2142

Browse files
committed
Implement AsyncRead/AsyncWrite for &mut T.
Provide blanket implementations: - `impl<R: AsyncRead + ?Sized> AsyncRead for &mut R` and - `impl<W: AsyncWrite + ?Sized> AsyncWrite for &mut W` This is similar to what is done for similar traits in `std::io` ([`Read`](https://doc.rust-lang.org/stable/std/io/trait.Read.html#impl-Read-for-%26mut+R), [`Write`](https://doc.rust-lang.org/stable/std/io/trait.Write.html#impl-Write-for-%26mut+W)), `futures-io` ([`AsyncRead`](https://docs.rs/futures-io/latest/futures_io/trait.AsyncRead.html#impl-AsyncRead-for-%26mut+T), [`AsyncWrite`](https://docs.rs/futures-io/latest/futures_io/trait.AsyncWrite.html#impl-AsyncWrite-for-%26mut+T)), and `tokio` ([`AsyncRead`](https://docs.rs/tokio/latest/tokio/io/trait.AsyncRead.html#impl-AsyncRead-for-%26mut+T), [`AsyncWrite`](https://docs.rs/tokio/latest/tokio/io/trait.AsyncWrite.html#impl-AsyncWrite-for-%26mut+T)).
1 parent d8ce53c commit 26e2142

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/io/read.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,15 @@ pub trait AsyncRead {
2525
}
2626
}
2727
}
28+
29+
impl<R: AsyncRead + ?Sized> AsyncRead for &mut R {
30+
#[inline]
31+
async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
32+
(*self).read(buf).await
33+
}
34+
35+
#[inline]
36+
async fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
37+
(*self).read_to_end(buf).await
38+
}
39+
}

src/io/write.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,20 @@ pub trait AsyncWrite {
1717
}
1818
}
1919
}
20+
21+
impl<W: AsyncWrite + ?Sized> AsyncWrite for &mut W {
22+
#[inline]
23+
async fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
24+
(*self).write(buf).await
25+
}
26+
27+
#[inline]
28+
async fn flush(&mut self) -> io::Result<()> {
29+
(*self).flush().await
30+
}
31+
32+
#[inline]
33+
async fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
34+
(*self).write_all(buf).await
35+
}
36+
}

0 commit comments

Comments
 (0)