[−][src]Trait futures_util::io::AsyncBufReadExt
An extension trait which adds utility methods to AsyncBufRead
types.
Provided methods
ⓘImportant traits for ReadUntil<'_, R>fn read_until<'a>(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>
) -> ReadUntil<'a, Self> where
Self: Unpin,
ⓘImportant traits for ReadUntil<'_, R>
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>
) -> ReadUntil<'a, Self> where
Self: Unpin,
Creates a future which will read all the bytes associated with this I/O
object into buf
until the delimiter byte
or EOF is reached.
This method is the async equivalent to BufRead::read_until
.
This function will read bytes from the underlying stream until the
delimiter or EOF is found. Once found, all bytes up to, and including,
the delimiter (if found) will be appended to buf
.
The returned future will resolve to the number of bytes read once the read operation is completed.
In the case of an error the buffer and the object will be discarded, with the error yielded.
Examples
#![feature(async_await, await_macro)] use futures::io::AsyncBufReadExt; use std::io::Cursor; let mut cursor = Cursor::new(b"lorem-ipsum"); let mut buf = vec![]; // cursor is at 'l' let num_bytes = await!(cursor.read_until(b'-', &mut buf))?; assert_eq!(num_bytes, 6); assert_eq!(buf, b"lorem-"); buf.clear(); // cursor is at 'i' let num_bytes = await!(cursor.read_until(b'-', &mut buf))?; assert_eq!(num_bytes, 5); assert_eq!(buf, b"ipsum"); buf.clear(); // cursor is at EOF let num_bytes = await!(cursor.read_until(b'-', &mut buf))?; assert_eq!(num_bytes, 0); assert_eq!(buf, b"");