Skip to main content

ReadExt

Trait ReadExt 

Source
pub trait ReadExt: Read + Send {
    // Required methods
    fn read_at_most(
        &mut self,
        len: usize,
    ) -> impl Future<Output = Result<BytesView>> + Send;
    fn read_exactly(
        &mut self,
        len: usize,
    ) -> impl Future<Output = Result<BytesView>> + Send;
    fn read_at_most_while<F>(
        &mut self,
        len: usize,
        inspect_fn: F,
    ) -> impl Future<Output = Result<(BytesView, BytesBuf)>> + Send
       where F: FnMut(BytesView) -> ReadInspectDecision + Send;
    fn read_at_most_into_while<F>(
        &mut self,
        len: usize,
        into: BytesBuf,
        inspect_fn: F,
    ) -> impl Future<Output = Result<(BytesView, BytesBuf)>> + Send
       where F: FnMut(BytesView) -> ReadInspectDecision + Send;
    fn into_futures_stream(self) -> Pin<Box<ReadAsFuturesStream<Self>>>
       where Self: Sized;
}
Expand description

Universal convenience methods built on top of Read.

This trait is implemented for all types that implement Read.

Required Methods§

Source

fn read_at_most( &mut self, len: usize, ) -> impl Future<Output = Result<BytesView>> + Send

Reads at most len bytes into a new buffer.

§Security

This method is insecure if the side producing the bytes is not trusted. An attacker may trickle data byte-by-byte, consuming a large amount of I/O resources.

Robust code working with untrusted sources should take precautions such as only processing read data when either a time or length threshold is reached and reusing buffers that have remaining capacity, appending additional data to existing buffers using read_more_into() instead of reserving new buffers for each read operation.

§Example
use bytesbuf_io::ReadExt;

let mut source = get_source();

let data = source.read_at_most(123).await.unwrap();

println!("read {} bytes of data", data.len());
Source

fn read_exactly( &mut self, len: usize, ) -> impl Future<Output = Result<BytesView>> + Send

Reads exactly len bytes into a new buffer.

The call will complete only when enough data has been read (or an error occurs).

§Example
use bytesbuf_io::ReadExt;

let mut source = get_source();

let data = source.read_exactly(10).await.unwrap();
assert_eq!(data.len(), 10);
Source

fn read_at_most_while<F>( &mut self, len: usize, inspect_fn: F, ) -> impl Future<Output = Result<(BytesView, BytesBuf)>> + Send

Conditionally reads at most len bytes into a new buffer.

The provided inspector function is used to inspect the data as it is read and to decide whether to continue reading or stop/fail the operation.

Source

fn read_at_most_into_while<F>( &mut self, len: usize, into: BytesBuf, inspect_fn: F, ) -> impl Future<Output = Result<(BytesView, BytesBuf)>> + Send

Conditionally reads at most len bytes into the provided buffer.

The provided inspector function is used to inspect the data as it is read and to decide whether to continue reading or stop/fail the operation.

§Panics

Panics if the provided BytesBuf already contains len or more bytes, as this is likely to be a programming error that may lead to an infinite loop if not guarded against.

Source

fn into_futures_stream(self) -> Pin<Box<ReadAsFuturesStream<Self>>>
where Self: Sized,

Available on crate feature futures-stream only.

Transforms the Read into a futures::Stream.

Each item yielded by the stream corresponds to a sequence of one or more bytes read from this source.

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§

Source§

impl<T> ReadExt for T
where T: Read,