An extension trait which adds utility methods to AsyncBufRead
types.
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.
#![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![];
let num_bytes = await!(cursor.read_until(b'-', &mut buf))?;
assert_eq!(num_bytes, 6);
assert_eq!(buf, b"lorem-");
buf.clear();
let num_bytes = await!(cursor.read_until(b'-', &mut buf))?;
assert_eq!(num_bytes, 5);
assert_eq!(buf, b"ipsum");
buf.clear();
let num_bytes = await!(cursor.read_until(b'-', &mut buf))?;
assert_eq!(num_bytes, 0);
assert_eq!(buf, b"");