Trait futures_util::io::AsyncBufReadExt
source · pub trait AsyncBufReadExt: AsyncBufRead {
// Provided methods
fn fill_buf(&mut self) -> FillBuf<'_, Self> ⓘ
where Self: Unpin { ... }
fn consume_unpin(&mut self, amt: usize)
where Self: Unpin { ... }
fn read_until<'a>(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>,
) -> ReadUntil<'a, Self> ⓘ
where Self: Unpin { ... }
fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLine<'a, Self> ⓘ
where Self: Unpin { ... }
fn lines(self) -> Lines<Self>
where Self: Sized { ... }
}
Expand description
An extension trait which adds utility methods to AsyncBufRead
types.
Provided Methods§
sourcefn fill_buf(&mut self) -> FillBuf<'_, Self> ⓘwhere
Self: Unpin,
fn fill_buf(&mut self) -> FillBuf<'_, Self> ⓘwhere
Self: Unpin,
Creates a future which will wait for a non-empty buffer to be available from this I/O object or EOF to be reached.
This method is the async equivalent to BufRead::fill_buf
.
use futures::{io::AsyncBufReadExt as _, stream::{iter, TryStreamExt as _}};
let mut stream = iter(vec![Ok(vec![1, 2, 3]), Ok(vec![4, 5, 6])]).into_async_read();
assert_eq!(stream.fill_buf().await?, vec![1, 2, 3]);
stream.consume_unpin(2);
assert_eq!(stream.fill_buf().await?, vec![3]);
stream.consume_unpin(1);
assert_eq!(stream.fill_buf().await?, vec![4, 5, 6]);
stream.consume_unpin(3);
assert_eq!(stream.fill_buf().await?, vec![]);
sourcefn consume_unpin(&mut self, amt: usize)where
Self: Unpin,
fn consume_unpin(&mut self, amt: usize)where
Self: Unpin,
A convenience for calling AsyncBufRead::consume
on Unpin
IO types.
use futures::{io::AsyncBufReadExt as _, stream::{iter, TryStreamExt as _}};
let mut stream = iter(vec![Ok(vec![1, 2, 3])]).into_async_read();
assert_eq!(stream.fill_buf().await?, vec![1, 2, 3]);
stream.consume_unpin(2);
assert_eq!(stream.fill_buf().await?, vec![3]);
stream.consume_unpin(1);
assert_eq!(stream.fill_buf().await?, vec![]);
sourcefn read_until<'a>(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>,
) -> ReadUntil<'a, Self> ⓘwhere
Self: Unpin,
fn read_until<'a>(
&'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
use futures::io::{AsyncBufReadExt, Cursor};
let mut cursor = Cursor::new(b"lorem-ipsum");
let mut buf = vec![];
// cursor is at 'l'
let num_bytes = cursor.read_until(b'-', &mut buf).await?;
assert_eq!(num_bytes, 6);
assert_eq!(buf, b"lorem-");
buf.clear();
// cursor is at 'i'
let num_bytes = cursor.read_until(b'-', &mut buf).await?;
assert_eq!(num_bytes, 5);
assert_eq!(buf, b"ipsum");
buf.clear();
// cursor is at EOF
let num_bytes = cursor.read_until(b'-', &mut buf).await?;
assert_eq!(num_bytes, 0);
assert_eq!(buf, b"");
sourcefn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLine<'a, Self> ⓘwhere
Self: Unpin,
fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLine<'a, Self> ⓘwhere
Self: Unpin,
Creates a future which will read all the bytes associated with this I/O
object into buf
until a newline (the 0xA byte) or EOF is reached,
This method is the async equivalent to BufRead::read_line
.
This function will read bytes from the underlying stream until the
newline delimiter (the 0xA byte) 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.
§Errors
This function has the same error semantics as read_until
and will
also return an error if the read bytes are not valid UTF-8. If an I/O
error is encountered then buf
may contain some bytes already read in
the event that all data read so far was valid UTF-8.
§Examples
use futures::io::{AsyncBufReadExt, Cursor};
let mut cursor = Cursor::new(b"foo\nbar");
let mut buf = String::new();
// cursor is at 'f'
let num_bytes = cursor.read_line(&mut buf).await?;
assert_eq!(num_bytes, 4);
assert_eq!(buf, "foo\n");
buf.clear();
// cursor is at 'b'
let num_bytes = cursor.read_line(&mut buf).await?;
assert_eq!(num_bytes, 3);
assert_eq!(buf, "bar");
buf.clear();
// cursor is at EOF
let num_bytes = cursor.read_line(&mut buf).await?;
assert_eq!(num_bytes, 0);
assert_eq!(buf, "");
sourcefn lines(self) -> Lines<Self>where
Self: Sized,
fn lines(self) -> Lines<Self>where
Self: Sized,
Returns a stream over the lines of this reader.
This method is the async equivalent to BufRead::lines
.
The stream returned from this function will yield instances of
io::Result
<
String
>
. Each string returned will not have a newline
byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end.
§Errors
Each line of the stream has the same error semantics as AsyncBufReadExt::read_line
.
§Examples
use futures::io::{AsyncBufReadExt, Cursor};
use futures::stream::StreamExt;
let cursor = Cursor::new(b"lorem\nipsum\xc2\r\ndolor");
let mut lines_stream = cursor.lines().map(|l| l.unwrap_or(String::from("invalid UTF_8")));
assert_eq!(lines_stream.next().await, Some(String::from("lorem")));
assert_eq!(lines_stream.next().await, Some(String::from("invalid UTF_8")));
assert_eq!(lines_stream.next().await, Some(String::from("dolor")));
assert_eq!(lines_stream.next().await, None);