Read

Trait Read 

Source
pub trait Read: Send {
    type Error: Error + ErrorAsErrorCode + Send + Sync + 'static;

    // Required method
    fn read(
        &mut self,
        buf: &mut [u8],
    ) -> impl Future<Output = Result<NonZeroUsize, Self::Error>> + Send;
}
Expand description

Read the data from the stream.

Required Associated Types§

Source

type Error: Error + ErrorAsErrorCode + Send + Sync + 'static

An error that can occur while reading the stream.

Required Methods§

Source

fn read( &mut self, buf: &mut [u8], ) -> impl Future<Output = Result<NonZeroUsize, Self::Error>> + Send

Read the data from the stream into a given buffer and return the amount of bytes filled in the buffer or None if the stream is closed and does not have any pending unread data.

A would-be zero-length read should be reported as a error with a zero error code. There is a handy way to check that condition:

let result = stream.read(buf).await;
let data = match result {
    Ok(read) => buf[..read.get()],
    Err(err) if err.is_closed() => {
        // Stream ended.
        return;
    }
    Err(err) => {
        panic!("something went wrong");
    }
};

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§