Trait futures_lite::io::AsyncReadExt
source · [−]pub trait AsyncReadExt: AsyncRead {
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>ⓘNotable traits for ReadFuture<'_, R>impl<R: AsyncRead + Unpin + ?Sized> Future for ReadFuture<'_, R> type Output = Result<usize>;
where
Self: Unpin,
{ ... }
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self>ⓘNotable traits for ReadVectoredFuture<'_, R>impl<R: AsyncRead + Unpin + ?Sized> Future for ReadVectoredFuture<'_, R> type Output = Result<usize>;
where
Self: Unpin,
{ ... }
fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>
) -> ReadToEndFuture<'a, Self>ⓘNotable traits for ReadToEndFuture<'_, R>impl<R: AsyncRead + Unpin + ?Sized> Future for ReadToEndFuture<'_, R> type Output = Result<usize>;
where
Self: Unpin,
{ ... }
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self>ⓘNotable traits for ReadToStringFuture<'_, R>impl<R: AsyncRead + Unpin + ?Sized> Future for ReadToStringFuture<'_, R> type Output = Result<usize>;
where
Self: Unpin,
{ ... }
fn read_exact<'a>(
&'a mut self,
buf: &'a mut [u8]
) -> ReadExactFuture<'a, Self>ⓘNotable traits for ReadExactFuture<'_, R>impl<R: AsyncRead + Unpin + ?Sized> Future for ReadExactFuture<'_, R> type Output = Result<()>;
where
Self: Unpin,
{ ... }
fn take(self, limit: u64) -> Take<Self>
where
Self: Sized,
{ ... }
fn bytes(self) -> Bytes<Self>
where
Self: Sized,
{ ... }
fn chain<R: AsyncRead>(self, next: R) -> Chain<Self, R>
where
Self: Sized,
{ ... }
fn boxed_reader<'a>(self) -> Pin<Box<dyn AsyncRead + Send + 'a>>
where
Self: Sized + Send + 'a,
{ ... }
}
Expand description
Extension trait for AsyncRead
.
Provided methods
Reads some bytes from the byte stream.
On success, returns the total number of bytes read.
If the return value is Ok(n)
, then it must be guaranteed that
0 <= n <= buf.len()
. A nonzero n
value indicates that the buffer has been
filled with n
bytes of data. If n
is 0
, then it can indicate one of two
scenarios:
- This reader has reached its “end of file” and will likely no longer be able to produce bytes. Note that this does not mean that the reader will always no longer be able to produce bytes.
- The buffer specified was 0 bytes in length.
Examples
use futures_lite::io::{AsyncReadExt, BufReader};
let input: &[u8] = b"hello";
let mut reader = BufReader::new(input);
let mut buf = vec![0; 1024];
let n = reader.read(&mut buf).await?;
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self>ⓘNotable traits for ReadVectoredFuture<'_, R>impl<R: AsyncRead + Unpin + ?Sized> Future for ReadVectoredFuture<'_, R> type Output = Result<usize>;
where
Self: Unpin,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self>ⓘNotable traits for ReadVectoredFuture<'_, R>impl<R: AsyncRead + Unpin + ?Sized> Future for ReadVectoredFuture<'_, R> type Output = Result<usize>;
where
Self: Unpin,
fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>
) -> ReadToEndFuture<'a, Self>ⓘNotable traits for ReadToEndFuture<'_, R>impl<R: AsyncRead + Unpin + ?Sized> Future for ReadToEndFuture<'_, R> type Output = Result<usize>;
where
Self: Unpin,
fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>
) -> ReadToEndFuture<'a, Self>ⓘNotable traits for ReadToEndFuture<'_, R>impl<R: AsyncRead + Unpin + ?Sized> Future for ReadToEndFuture<'_, R> type Output = Result<usize>;
where
Self: Unpin,
Reads the entire contents and appends them to a Vec
.
On success, returns the total number of bytes read.
Examples
use futures_lite::io::{AsyncReadExt, Cursor};
let mut reader = Cursor::new(vec![1, 2, 3]);
let mut contents = Vec::new();
let n = reader.read_to_end(&mut contents).await?;
assert_eq!(n, 3);
assert_eq!(contents, [1, 2, 3]);
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self>ⓘNotable traits for ReadToStringFuture<'_, R>impl<R: AsyncRead + Unpin + ?Sized> Future for ReadToStringFuture<'_, R> type Output = Result<usize>;
where
Self: Unpin,
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self>ⓘNotable traits for ReadToStringFuture<'_, R>impl<R: AsyncRead + Unpin + ?Sized> Future for ReadToStringFuture<'_, R> type Output = Result<usize>;
where
Self: Unpin,
Reads the entire contents and appends them to a String
.
On success, returns the total number of bytes read.
Examples
use futures_lite::io::{AsyncReadExt, Cursor};
let mut reader = Cursor::new(&b"hello");
let mut contents = String::new();
let n = reader.read_to_string(&mut contents).await?;
assert_eq!(n, 5);
assert_eq!(contents, "hello");
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>ⓘNotable traits for ReadExactFuture<'_, R>impl<R: AsyncRead + Unpin + ?Sized> Future for ReadExactFuture<'_, R> type Output = Result<()>;
where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>ⓘNotable traits for ReadExactFuture<'_, R>impl<R: AsyncRead + Unpin + ?Sized> Future for ReadExactFuture<'_, R> type Output = Result<()>;
where
Self: Unpin,
Reads the exact number of bytes required to fill buf
.
On success, returns the total number of bytes read.
Examples
use futures_lite::io::{AsyncReadExt, Cursor};
let mut reader = Cursor::new(&b"hello");
let mut contents = vec![0; 3];
reader.read_exact(&mut contents).await?;
assert_eq!(contents, b"hel");
Creates an adapter which will read at most limit
bytes from it.
This method returns a new instance of AsyncRead
which will read at most
limit
bytes, after which it will always return Ok(0)
indicating EOF.
Examples
use futures_lite::io::{AsyncReadExt, Cursor};
let mut reader = Cursor::new(&b"hello");
let mut contents = String::new();
let n = reader.take(3).read_to_string(&mut contents).await?;
assert_eq!(n, 3);
assert_eq!(contents, "hel");
Converts this AsyncRead
into a Stream
of bytes.
The returned type implements Stream
where Item
is io::Result<u8>
.
use futures_lite::io::{AsyncReadExt, Cursor};
use futures_lite::stream::StreamExt;
let reader = Cursor::new(&b"hello");
let mut bytes = reader.bytes();
while let Some(byte) = bytes.next().await {
println!("byte: {}", byte?);
}
Creates an adapter which will chain this stream with another.
The returned AsyncRead
instance will first read all bytes from this reader
until EOF is found, and then continue with next
.
Examples
use futures_lite::io::{AsyncReadExt, Cursor};
let r1 = Cursor::new(&b"hello");
let r2 = Cursor::new(&b"world");
let mut reader = r1.chain(r2);
let mut contents = String::new();
reader.read_to_string(&mut contents).await?;
assert_eq!(contents, "helloworld");