io: implement AsyncBufRead for &[u8] and Cursor (#1397)

* `impl AsyncRead for &[u8]`
* `impl AsyncBufRead for &[u8]`
* `impl<T: AsRef<[u8]> + Unpin> AsyncRead for Cursor<T>`
* `impl<T: AsRef<[u8]> + Unpin> AsyncBufRead for Cursor<T>`
This commit is contained in:
tmiasko
2019-08-07 12:57:37 -07:00
committed by Carl Lerche
parent 7174c63bf9
commit 53a94c025d
8 changed files with 67 additions and 185 deletions
+26
View File
@@ -92,3 +92,29 @@ where
self.get_mut().as_mut().consume(amt)
}
}
impl AsyncBufRead for &[u8] {
fn poll_fill_buf<'a>(
self: Pin<&'a mut Self>,
_cx: &mut Context<'_>,
) -> Poll<io::Result<&'a [u8]>> {
Poll::Ready(Ok(*self))
}
fn consume(mut self: Pin<&mut Self>, amt: usize) {
*self = &self[amt..];
}
}
impl<T: AsRef<[u8]> + Unpin> AsyncBufRead for io::Cursor<T> {
fn poll_fill_buf<'a>(
self: Pin<&'a mut Self>,
_cx: &mut Context<'_>,
) -> Poll<io::Result<&'a [u8]>> {
Poll::Ready(io::BufRead::fill_buf(self.get_mut()))
}
fn consume(self: Pin<&mut Self>, amt: usize) {
io::BufRead::consume(self.get_mut(), amt)
}
}