[][src]Trait futures::io::AsyncReadExt

pub trait AsyncReadExt: AsyncRead {
    fn copy_into<W>(&'a mut self, writer: &'a mut W) -> CopyInto<'a, Self, W>
    where
        Self: Unpin,
        W: AsyncWrite + Unpin
, { ... }
fn read(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>
    where
        Self: Unpin
, { ... }
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>
    where
        Self: Unpin
, { ... }
fn read_to_end(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self>
    where
        Self: Unpin
, { ... }
fn split(self) -> (ReadHalf<Self>, WriteHalf<Self>)
    where
        Self: AsyncWrite
, { ... } }

An extension trait which adds utility methods to AsyncRead types.

Provided methods

Important traits for CopyInto<'_, R, W>
fn copy_into<W>(&'a mut self, writer: &'a mut W) -> CopyInto<'a, Self, W> where
    Self: Unpin,
    W: AsyncWrite + Unpin

Creates a future which copies all the bytes from one object to another.

The returned future will copy all the bytes read from this AsyncRead into the writer specified. This future will only complete once the reader has hit EOF and all bytes have been written to and flushed from the writer provided.

On success the number of bytes is returned.

Examples

#![feature(async_await)]
use futures::io::AsyncReadExt;
use std::io::Cursor;

let mut reader = Cursor::new([1, 2, 3, 4]);
let mut writer = Cursor::new([0u8; 5]);

let bytes = reader.copy_into(&mut writer).await?;

assert_eq!(bytes, 4);
assert_eq!(writer.into_inner(), [1, 2, 3, 4, 0]);

Important traits for Read<'_, R>
fn read(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self> where
    Self: Unpin

Tries to read some bytes directly into the given buf in asynchronous manner, returning a future type.

The returned future will resolve to the number of bytes read once the read operation is completed.

Examples

#![feature(async_await)]
use futures::io::AsyncReadExt;
use std::io::Cursor;

let mut reader = Cursor::new([1, 2, 3, 4]);
let mut output = [0u8; 5];

let bytes = reader.read(&mut output[..]).await?;

// This is only guaranteed to be 4 because `&[u8]` is a synchronous
// reader. In a real system you could get anywhere from 1 to
// `output.len()` bytes in a single read.
assert_eq!(bytes, 4);
assert_eq!(output, [1, 2, 3, 4, 0]);

Important traits for ReadExact<'_, R>
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self> where
    Self: Unpin

Creates a future which will read exactly enough bytes to fill buf, returning an error if end of file (EOF) is hit sooner.

The returned future will resolve once the read operation is completed.

In the case of an error the buffer and the object will be discarded, with the error yielded.

Examples

#![feature(async_await)]
use futures::io::AsyncReadExt;
use std::io::Cursor;

let mut reader = Cursor::new([1, 2, 3, 4]);
let mut output = [0u8; 4];

reader.read_exact(&mut output).await?;

assert_eq!(output, [1, 2, 3, 4]);

EOF is hit before buf is filled

#![feature(async_await)]
use futures::io::AsyncReadExt;
use std::io::{self, Cursor};

let mut reader = Cursor::new([1, 2, 3, 4]);
let mut output = [0u8; 5];

let result = reader.read_exact(&mut output).await;

assert_eq!(result.unwrap_err().kind(), io::ErrorKind::UnexpectedEof);

Important traits for ReadToEnd<'_, A>
fn read_to_end(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self> where
    Self: Unpin

Creates a future which will read all the bytes from this AsyncRead.

Examples

#![feature(async_await)]
use futures::io::AsyncReadExt;
use std::io::Cursor;

let mut reader = Cursor::new([1, 2, 3, 4]);
let mut output = Vec::with_capacity(4);

reader.read_to_end(&mut output).await?;

assert_eq!(output, vec![1, 2, 3, 4]);

fn split(self) -> (ReadHalf<Self>, WriteHalf<Self>) where
    Self: AsyncWrite

Helper method for splitting this read/write object into two halves.

The two halves returned implement the AsyncRead and AsyncWrite traits, respectively.

Examples

#![feature(async_await)]
use futures::io::AsyncReadExt;
use std::io::Cursor;

// Note that for `Cursor` the read and write halves share a single
// seek position. This may or may not be true for other types that
// implement both `AsyncRead` and `AsyncWrite`.

let mut reader = Cursor::new([1, 2, 3, 4]);
let mut buffer = Cursor::new([0, 0, 0, 0, 5, 6, 7, 8]);
let mut writer = Cursor::new([0u8; 5]);

{
    let (mut buffer_reader, mut buffer_writer) = (&mut buffer).split();
    reader.copy_into(&mut buffer_writer).await?;
    buffer_reader.copy_into(&mut writer).await?;
}

assert_eq!(buffer.into_inner(), [1, 2, 3, 4, 5, 6, 7, 8]);
assert_eq!(writer.into_inner(), [5, 6, 7, 8, 0]);
Loading content...

Implementors

impl<R> AsyncReadExt for R where
    R: AsyncRead + ?Sized
[src]

Important traits for CopyInto<'_, R, W>
fn copy_into<W>(&'a mut self, writer: &'a mut W) -> CopyInto<'a, Self, W> where
    Self: Unpin,
    W: AsyncWrite + Unpin
[src]

Important traits for Read<'_, R>
fn read(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self> where
    Self: Unpin
[src]

Important traits for ReadExact<'_, R>
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self> where
    Self: Unpin
[src]

Important traits for ReadToEnd<'_, A>
fn read_to_end(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self> where
    Self: Unpin
[src]

fn split(self) -> (ReadHalf<Self>, WriteHalf<Self>) where
    Self: AsyncWrite
[src]

Loading content...