mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-29 00:00:11 +02:00
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:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user