ChunkSource

Trait ChunkSource 

Source
pub trait ChunkSource {
    type Error;
    type Chunk<'a>: AsRef<[u8]> + 'a
       where Self: 'a;

    // Required method
    fn next_chunk<'a>(
        &'a mut self,
    ) -> Option<Result<Self::Chunk<'a>, Self::Error>>;
}
Expand description

This trait is designed to handle byte streams efficiently, especially when the source needs to borrow from an internal buffer between calls. A simple closure (FnMut() -> Option<Result<B, E>>) cannot express this lifetime relationship, as the returned slice would need to outlive the closure call itself. This trait solves that by making the source a mutable object that you call repeatedly.

Async functionality can be achieved by the original API

Required Associated Types§

Source

type Error

The type of error that can occur when reading a chunk.

Source

type Chunk<'a>: AsRef<[u8]> + 'a where Self: 'a

The type of chunk returned, which must implement AsRef<u8>

Required Methods§

Source

fn next_chunk<'a>(&'a mut self) -> Option<Result<Self::Chunk<'a>, Self::Error>>

Get the next chunk of bytes.

Returns None when the source is exhausted, Some(Ok(bytes)) for a successful chunk, or Some(Err(e)) if an error occurred.

The returned slice is valid until the next call to next_chunk or until the source is dropped.

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<T> ChunkSource for &mut T
where T: ChunkSource,

Source§

type Error = <T as ChunkSource>::Error

Source§

type Chunk<'a> = <T as ChunkSource>::Chunk<'a> where Self: 'a

Source§

fn next_chunk<'a>(&'a mut self) -> Option<Result<Self::Chunk<'a>, Self::Error>>

Implementors§

Source§

impl<'s, F, B, E> ChunkSource for FnMutChunkSource<'s, F, B, E>
where F: FnMut() -> Option<Result<B, E>>, B: AsRef<[u8]> + 's,

Source§

type Error = E

Source§

type Chunk<'a> = B where Self: 'a

Source§

impl<R, B> ChunkSource for ReadChunkSource<R, B>
where R: Read, B: AsMut<[u8]>,

Source§

type Error = Error

Source§

type Chunk<'a> = &'a [u8] where Self: 'a