Trait AsyncBufReadExt
trait AsyncBufReadExt: AsyncBufRead
An extension trait which adds utility methods to AsyncBufRead types.
Provided Methods
fn read_until<'a>(self: &'a mut Self, byte: u8, buf: &'a mut Vec<u8>) -> ReadUntil<'a, Self> where Self: UnpinReads all bytes into
bufuntil the delimiterbyteor EOF is reached.Equivalent to:
async fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize>;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.
If this function returns
Ok(0), the stream has reached EOF.Errors
This function will ignore all instances of
ErrorKind::Interruptedand will otherwise return any errors returned byfill_buf.If an I/O error is encountered then all bytes read so far will be present in
bufand its length will have been adjusted appropriately.Cancel safety
If the method is used as the event in a
tokio::select!statement and some other branch completes first, then some data may have been partially read. Any partially read bytes are appended tobuf, and the method can be called again to continue reading untilbyte.This method returns the total number of bytes read. If you cancel the call to
read_untiland then call it again to continue reading, the counter is reset.Examples
std::io::Cursoris a type that implementsBufRead. In this example, we useCursorto read all the bytes in a byte slice in hyphen delimited segments:use AsyncBufReadExt; use Cursor; asyncfn read_line<'a>(self: &'a mut Self, buf: &'a mut String) -> ReadLine<'a, Self> where Self: UnpinReads all bytes until a newline (the 0xA byte) is reached, and append them to the provided buffer.
Equivalent to:
async fn read_line(&mut self, buf: &mut String) -> io::Result<usize>;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_untiland will also return an error if the read bytes are not valid UTF-8. If an I/O error is encountered thenbufmay contain some bytes already read in the event that all data read so far was valid UTF-8.Cancel safety
This method is not cancellation safe. If the method is used as the event in a
tokio::select!statement and some other branch completes first, then some data may have been partially read, and this data is lost. There are no guarantees regarding the contents ofbufwhen the call is cancelled. The current implementation replacesbufwith the empty string, but this may change in the future.This function does not behave like
read_untilbecause of the requirement that a string contains only valid utf-8. If you need a cancellation saferead_line, there are three options:- Call
read_untilwith a newline character and manually perform the utf-8 check. - The stream returned by
lineshas a cancellation safenext_linemethod. - Use
tokio_util::codec::LinesCodec.
Examples
std::io::Cursoris a type that implementsAsyncBufRead. In this example, we useCursorto read all the lines in a byte slice:use AsyncBufReadExt; use Cursor; async- Call
fn split(self: Self, byte: u8) -> Split<Self> where Self: Sized + UnpinReturns a stream of the contents of this reader split on the byte
byte.This method is the asynchronous equivalent to
BufRead::split.The stream returned from this function will yield instances of
io::Result<Option<Vec<u8>>>. Each vector returned will not have the delimiter byte at the end.Errors
Each item of the stream has the same error semantics as
AsyncBufReadExt::read_until.Examples
# use AsyncBufRead; use AsyncBufReadExt; # asyncfn fill_buf(self: &mut Self) -> FillBuf<'_, Self> where Self: UnpinReturns the contents of the internal buffer, filling it with more data from the inner reader if it is empty.
This function is a lower-level call. It needs to be paired with the
consumemethod to function properly. When calling this method, none of the contents will be "read" in the sense that later callingreadmay return the same contents. As such,consumemust be called with the number of bytes that are consumed from this buffer to ensure that the bytes are never returned twice.An empty buffer returned indicates that the stream has reached EOF.
Equivalent to:
async fn fill_buf(&mut self) -> io::Result<&[u8]>;Errors
This function will return an I/O error if the underlying reader was read, but returned an error.
Cancel safety
This method is cancel safe. If you use it as the event in a
tokio::select!statement and some other branch completes first, then it is guaranteed that no data was read.fn consume(self: &mut Self, amt: usize) where Self: UnpinTells this buffer that
amtbytes have been consumed from the buffer, so they should no longer be returned in calls toread.This function is a lower-level call. It needs to be paired with the
fill_bufmethod to function properly. This function does not perform any I/O, it simply informs this object that some amount of its buffer, returned fromfill_buf, has been consumed and should no longer be returned. As such, this function may do odd things iffill_bufisn't called before calling it.The
amtmust be less than the number of bytes in the buffer returned byfill_buf.fn lines(self: Self) -> Lines<Self> where Self: SizedReturns 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<Option<String>>. Each string returned will not have a newline byte (the 0xA byte) orCRLF(0xD, 0xA bytes) at the end.Errors
Each line of the stream has the same error semantics as
AsyncBufReadExt::read_line.Examples
std::io::Cursoris a type that implementsBufRead. In this example, we useCursorto iterate over all the lines in a byte slice.use AsyncBufReadExt; use Cursor; async