Trait Read

Source
pub trait Read: MaybeSend + MaybeSync {
    // Required methods
    fn read_exact_at<B: IoBufMut>(
        &mut self,
        buf: B,
        pos: u64,
    ) -> impl Future<Output = (Result<(), Error>, B)> + MaybeSend;
    fn read_to_end_at(
        &mut self,
        buf: Vec<u8>,
        pos: u64,
    ) -> impl Future<Output = (Result<(), Error>, Vec<u8>)> + MaybeSend;
    fn size(&self) -> impl Future<Output = Result<u64, Error>> + MaybeSend;
}
Expand description

The core trait for reading data.

It is similar to std::io::Read, but it takes ownership of the buffer, because completion-based IO requires the buffer to be pinned and should be safe to cancellation.

Read represents “random exactly read” semantics, which means the read operation will start at the specified position, and the buffer will be exactly filled with the data read.

The buffer will be returned with the result, whether the operation is successful or not. Fusio promises that the returned buffer will be the same as the input buffer.

If you want sequential reading, try SeqRead (not yet implemented).

§Examples

use fusio_core::{IoBufMut, Read};

async fn read_at_position<R: Read>(
    mut reader: R,
    pos: u64,
    len: usize,
) -> Result<Vec<u8>, fusio_core::error::Error> {
    let mut buf = vec![0u8; len];
    let (result, buf) = reader.read_exact_at(buf, pos).await;
    result?;
    Ok(buf)
}

§Dyn Compatibility

This trait is not dyn compatible. If you want to use Read trait in a dynamic way, you could use DynRead trait.

Required Methods§

Source

fn read_exact_at<B: IoBufMut>( &mut self, buf: B, pos: u64, ) -> impl Future<Output = (Result<(), Error>, B)> + MaybeSend

Source

fn read_to_end_at( &mut self, buf: Vec<u8>, pos: u64, ) -> impl Future<Output = (Result<(), Error>, Vec<u8>)> + MaybeSend

Available on crate feature alloc only.
Source

fn size(&self) -> impl Future<Output = Result<u64, Error>> + MaybeSend

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.

Implementations on Foreign Types§

Source§

impl Read for &mut Vec<u8>

Source§

async fn read_exact_at<B: IoBufMut>( &mut self, buf: B, pos: u64, ) -> (Result<(), Error>, B)

Source§

async fn read_to_end_at( &mut self, buf: Vec<u8>, pos: u64, ) -> (Result<(), Error>, Vec<u8>)

Available on crate feature alloc only.
Source§

async fn size(&self) -> Result<u64, Error>

Source§

impl Read for Box<dyn DynRead + '_>

Available on crate feature alloc only.
Source§

async fn read_exact_at<B: IoBufMut>( &mut self, buf: B, pos: u64, ) -> (Result<(), Error>, B)

Source§

async fn read_to_end_at( &mut self, buf: Vec<u8>, pos: u64, ) -> (Result<(), Error>, Vec<u8>)

Source§

async fn size(&self) -> Result<u64, Error>

Source§

impl<R: Read> Read for &mut R

Source§

fn read_exact_at<B: IoBufMut>( &mut self, buf: B, pos: u64, ) -> impl Future<Output = (Result<(), Error>, B)> + MaybeSend

Source§

fn read_to_end_at( &mut self, buf: Vec<u8>, pos: u64, ) -> impl Future<Output = (Result<(), Error>, Vec<u8>)> + MaybeSend

Available on crate feature alloc only.
Source§

fn size(&self) -> impl Future<Output = Result<u64, Error>> + MaybeSend

Implementors§