[−][src]Trait async_std::io::BufRead
Allows reading from a buffered byte stream.
This trait is an async version of std::io::BufRead
.
While it is currently not possible to implement this trait directly, it gets implemented
automatically for all types that implement futures::io::AsyncBufRead
.
Provided methods
fn read_until<'a>(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>
) -> ImplFuture<'a, Result<usize>> where
Self: Unpin,
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>
) -> ImplFuture<'a, Result<usize>> where
Self: Unpin,
Reads all bytes into buf
until the delimiter byte
or EOF is reached.
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
.
If successful, this function will return the total number of bytes read.
Examples
use async_std::fs::File; use async_std::io::BufReader; use async_std::prelude::*; let mut file = BufReader::new(File::open("a.txt").await?); let mut buf = vec![0; 1024]; let n = file.read_until(b'\n', &mut buf).await?;
fn read_line<'a>(
&'a mut self,
buf: &'a mut String
) -> ImplFuture<'a, Result<usize>> where
Self: Unpin,
&'a mut self,
buf: &'a mut String
) -> ImplFuture<'a, Result<usize>> where
Self: Unpin,
Reads all bytes and appends them into buf
until a newline (the 0xA byte) is reached.
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
.
If successful, this function will return the total number of bytes read.
If this function returns Ok(0)
, the stream has reached EOF.
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 async_std::fs::File; use async_std::io::BufReader; use async_std::prelude::*; let mut file = BufReader::new(File::open("a.txt").await?); let mut buf = String::new(); file.read_line(&mut buf).await?;
fn lines(self) -> Lines<Self> where
Self: Unpin + Sized,
Self: Unpin + Sized,
Returns a stream over the lines of this byte stream.
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.
Examples
use async_std::fs::File; use async_std::io::BufReader; use async_std::prelude::*; let file = File::open("a.txt").await?; let mut lines = BufReader::new(file).lines(); let mut count = 0; while let Some(line) = lines.next().await { line?; count += 1; }