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§
Sourcefn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<Result<usize, Self::Error>>
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§
Sourcefn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self> ⓘwhere
Self: Unpin,
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", so this trait is not object safe.