Trait smol::io::AsyncBufReadExt
source · [−]pub trait AsyncBufReadExt: AsyncBufRead {
fn fill_buf(&mut self) -> FillBuf<'_, Self>ⓘNotable traits for FillBuf<'a, R>impl<'a, R> Future for FillBuf<'a, R> where
R: AsyncBufRead + Unpin + ?Sized, type Output = Result<&'a [u8], Error>;
where
Self: Unpin,
{ ... }
fn consume(&mut self, amt: usize)
where
Self: Unpin,
{ ... }
fn read_until(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8, Global>
) -> ReadUntilFuture<'a, Self>ⓘNotable traits for ReadUntilFuture<'_, R>impl<'_, R> Future for ReadUntilFuture<'_, R> where
R: AsyncBufRead + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
{ ... }
fn read_line(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self>ⓘNotable traits for ReadLineFuture<'_, R>impl<'_, R> Future for ReadLineFuture<'_, R> where
R: AsyncBufRead + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
{ ... }
fn lines(self) -> Lines<Self>
where
Self: Unpin,
{ ... }
fn split(self, byte: u8) -> Split<Self> { ... }
}
Expand description
Extension trait for AsyncBufRead
.
Provided methods
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");
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");
fn read_until(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8, Global>
) -> ReadUntilFuture<'a, Self>ⓘNotable traits for ReadUntilFuture<'_, R>impl<'_, R> Future for ReadUntilFuture<'_, R> where
R: AsyncBufRead + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
fn read_until(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8, Global>
) -> ReadUntilFuture<'a, Self>ⓘNotable traits for ReadUntilFuture<'_, R>impl<'_, R> Future for ReadUntilFuture<'_, R> where
R: AsyncBufRead + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
R: AsyncBufRead + Unpin + ?Sized, type Output = Result<usize, Error>;
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?;
fn read_line(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self>ⓘNotable traits for ReadLineFuture<'_, R>impl<'_, R> Future for ReadLineFuture<'_, R> where
R: AsyncBufRead + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
fn read_line(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self>ⓘNotable traits for ReadLineFuture<'_, R>impl<'_, R> Future for ReadLineFuture<'_, R> where
R: AsyncBufRead + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
R: AsyncBufRead + Unpin + ?Sized, type Output = Result<usize, Error>;
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?;
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?);
}
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");