Skip to main content

AsyncRead

Trait AsyncRead 

Source
pub trait AsyncRead {
    type Error;

    // Required method
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut [u8],
    ) -> Poll<Result<usize, Self::Error>>;

    // Provided method
    fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self> 
       where Self: Unpin { ... }
}
Available on crate feature io only.
Expand description

Read bytes asynchronously.

Required Associated Types§

Required Methods§

Source

fn poll_read( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8], ) -> Poll<Result<usize, Self::Error>>

Attempt to read from the AsyncRead into buf. On success, returns Poll::Ready(Ok(num_bytes_read)). If no data is available for reading, this method returns Poll::Pending and arranges for the current task to be woken.

Provided Methods§

Source

fn read<'a>(&'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 both the I/O stream and the buffer as well as the number of bytes read once the read operation is completed.

use async_hal::io::AsyncRead;

let mut bytes = [1, 2, 3].as_ref();
let mut buf = [0; 3];

bytes.read(&mut buf).await?;   

assert_eq!([1, 2, 3], buf);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl AsyncRead for &[u8]

Source§

type Error = Void

Source§

fn poll_read( self: Pin<&mut Self>, _cx: &mut Context<'_>, buf: &mut [u8], ) -> Poll<Result<usize, Void>>

Source§

impl<T: ?Sized + AsyncRead + Unpin> AsyncRead for &mut T

Source§

type Error = <T as AsyncRead>::Error

Source§

fn poll_read( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8], ) -> Poll<Result<usize, Self::Error>>

Implementors§

Source§

impl<R: AsyncRead> AsyncRead for BufReader<'_, R>

Source§

impl<T, E> AsyncRead for Reader<T>
where T: Stream<Item = Result<u8, E>> + Unpin,

Source§

type Error = E