# tokio::io::AsyncBufReadExt - Rust
API documentation for the Rust `AsyncBufReadExt` trait in crate `tokio`.
Keywords: rust, rustlang, rust-lang, AsyncBufReadExt

# Trait tokio::io::AsyncBufReadExt [−] [src]

    pub trait AsyncBufReadExt: AsyncBufRead {
        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 split(self, byte: u8) -> Split<Self>
        where
            Self: Sized + Unpin,
        { ... }
        fn lines(self) -> Lines<Self>
        where
            Self: Sized,
        { ... }
    }

An extension trait which adds utility methods to `AsyncBufRead` types.

## Provided methods

### `fn read_until<'a>(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>
) -> ReadUntil<'a, Self> where
Self: Unpin,` [src]

Reads all bytes into `buf` until the delimiter `byte` or 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::Interrupted` and will otherwise return any errors returned by `fill_buf`.

If an I/O error is encountered then all bytes read so far will be present in `buf` and its length will have been adjusted appropriately.

# Examples

`std::io::Cursor` is a type that implements `BufRead`. In this example, we use `Cursor` to read all the bytes in a byte slice in hyphen delimited segments:

    use tokio::io::AsyncBufReadExt;

    use std::io::Cursor;

    #[tokio::main]
    async fn main() {
        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
            .expect("reading from cursor won't fail");

        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
            .expect("reading from cursor won't fail");

        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
            .expect("reading from cursor won't fail");
        assert_eq!(num_bytes, 0);
        assert_eq!(buf, b"");
    }

### `fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLine<'a, Self> where
Self: Unpin,` [src]

Reads 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_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

`std::io::Cursor` is a type that implements `AsyncBufRead`. In this example, we use `Cursor` to read all the lines in a byte slice:

    use tokio::io::AsyncBufReadExt;

    use std::io::Cursor;

    #[tokio::main]
    async fn main() {
        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
            .expect("reading from cursor won't fail");

        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
            .expect("reading from cursor won't fail");

        assert_eq!(num_bytes, 3);
        assert_eq!(buf, "bar");
        buf.clear();

        // cursor is at EOF
        let num_bytes = cursor.read_line(&mut buf)
            .await
            .expect("reading from cursor won't fail");

        assert_eq!(num_bytes, 0);
        assert_eq!(buf, "");
    }

### `fn split(self, byte: u8) -> Split<Self> where
Self: Sized + Unpin,` [src]

Returns 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` `<` `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 tokio::io::AsyncBufReadExt;

    let mut segments = my_buf_read.split(b'f');

    while let Some(segment) = segments.next_segment().await? {
        println!("length = {}", segment.len())
    }

### `fn lines(self) -> Lines<Self> where
Self: Sized,` [src]

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

`std::io::Cursor` is a type that implements `BufRead`. In this example, we use `Cursor` to iterate over all the lines in a byte slice.

    use tokio::io::AsyncBufReadExt;

    use std::io::Cursor;

    #[tokio::main]
    async fn main() {
        let cursor = Cursor::new(b"lorem\nipsum\r\ndolor");

        let mut lines = cursor.lines();

        assert_eq!(lines.next_line().await.unwrap(), Some(String::from("lorem")));
        assert_eq!(lines.next_line().await.unwrap(), Some(String::from("ipsum")));
        assert_eq!(lines.next_line().await.unwrap(), Some(String::from("dolor")));
        assert_eq!(lines.next_line().await.unwrap(), None);
    }

Loading content...

## Implementors

### `impl<R: AsyncBufRead + ?Sized> AsyncBufReadExt for R` [src]

Loading content...