futures_lite::io

Trait AsyncBufReadExt

source
pub trait AsyncBufReadExt: AsyncBufRead {
    // Provided methods
    fn fill_buf(&mut self) -> FillBuf<'_, Self> 
       where Self: Unpin { ... }
    fn consume(&mut self, amt: usize)
       where Self: Unpin { ... }
    fn read_until<'a>(
        &'a mut self,
        byte: u8,
        buf: &'a mut Vec<u8>,
    ) -> ReadUntilFuture<'_, Self> 
       where Self: Unpin { ... }
    fn read_line<'a>(
        &'a mut self,
        buf: &'a mut String,
    ) -> ReadLineFuture<'_, Self> 
       where Self: Unpin { ... }
    fn lines(self) -> Lines<Self>
       where Self: Unpin + Sized { ... }
    fn split(self, byte: u8) -> Split<Self>
       where Self: Sized { ... }
}
Expand description

Extension trait for AsyncBufRead.

Provided Methods§

source

fn fill_buf(&mut self) -> FillBuf<'_, Self>
where Self: Unpin,

Returns the contents of the internal buffer, filling it with more data if empty.

If the stream has reached EOF, an empty buffer will be returned.

§Examples
use futures_lite::io::{AsyncBufReadExt, BufReader};
use std::pin::Pin;

let input: &[u8] = b"hello world";
let mut reader = BufReader::with_capacity(5, input);

assert_eq!(reader.fill_buf().await?, b"hello");
reader.consume(2);
assert_eq!(reader.fill_buf().await?, b"llo");
reader.consume(3);
assert_eq!(reader.fill_buf().await?, b" worl");
source

fn consume(&mut self, amt: usize)
where Self: Unpin,

Consumes amt buffered bytes.

This method does not perform any I/O, it simply consumes some amount of bytes from the internal buffer.

The amt must be <= the number of bytes in the buffer returned by fill_buf().

§Examples
use futures_lite::io::{AsyncBufReadExt, BufReader};
use std::pin::Pin;

let input: &[u8] = b"hello";
let mut reader = BufReader::with_capacity(4, input);

assert_eq!(reader.fill_buf().await?, b"hell");
reader.consume(2);
assert_eq!(reader.fill_buf().await?, b"ll");
source

fn read_until<'a>( &'a mut self, byte: u8, buf: &'a mut Vec<u8>, ) -> ReadUntilFuture<'_, Self>
where Self: Unpin,

Reads all bytes and appends them into buf until the delimiter byte or EOF is found.

This method will read bytes from the underlying stream until the delimiter or EOF is found. All bytes up to and including the delimiter (if found) will be appended to buf.

If successful, returns the total number of bytes read.

§Examples
use futures_lite::io::{AsyncBufReadExt, BufReader};

let input: &[u8] = b"hello";
let mut reader = BufReader::new(input);

let mut buf = Vec::new();
let n = reader.read_until(b'\n', &mut buf).await?;
source

fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'_, Self>
where Self: Unpin,

Reads all bytes and appends them into buf until a newline (the 0xA byte) or EOF is found.

This method will read bytes from the underlying stream until the newline delimiter (the 0xA byte) or EOF is found. All bytes up to, and including, the newline delimiter (if found) will be appended to buf.

If successful, returns the total number of bytes read.

§Examples
use futures_lite::io::{AsyncBufReadExt, BufReader};

let input: &[u8] = b"hello";
let mut reader = BufReader::new(input);

let mut line = String::new();
let n = reader.read_line(&mut line).await?;
source

fn lines(self) -> Lines<Self>
where Self: Unpin + Sized,

Returns a stream over the lines of this byte stream.

The stream returned from this method yields items of type 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 futures_lite::io::{AsyncBufReadExt, BufReader};
use futures_lite::stream::StreamExt;

let input: &[u8] = b"hello\nworld\n";
let mut reader = BufReader::new(input);
let mut lines = reader.lines();

while let Some(line) = lines.next().await {
    println!("{}", line?);
}
source

fn split(self, byte: u8) -> Split<Self>
where Self: Sized,

Returns a stream over the contents of this reader split on the specified byte.

The stream returned from this method yields items of type io::Result<Vec<u8>>. Each vector returned will not have the delimiter byte at the end.

§Examples
use futures_lite::io::{AsyncBufReadExt, Cursor};
use futures_lite::stream::StreamExt;

let cursor = Cursor::new(b"lorem-ipsum-dolor");
let items: Vec<Vec<u8>> = cursor.split(b'-').try_collect().await?;

assert_eq!(items[0], b"lorem");
assert_eq!(items[1], b"ipsum");
assert_eq!(items[2], b"dolor");

Object Safety§

This trait is not object safe.

Implementors§