ReadChunks

Struct ReadChunks 

Source
pub struct ReadChunks<'a, R: ?Sized + Read> { /* private fields */ }
Expand description

A lending iterator that allows reading chunks of n bytes at a time from a reader.

Implementations§

Source§

impl<R: Read + ?Sized> ReadChunks<'_, R>

Source

pub fn next_chunk(&mut self) -> Option<Result<&mut [u8]>>

Reads the next chunk of bytes from R with a size n specified by ReadExt::read_chunks.

This method is meant to be called repeatedly until None is returned, at which EOF is assumed to have occurred (when Read::read returns Ok(0)).

§Errors

If this function encounters ErrorKind::Interrupted it will continue to attempt to fill the buffer until a different error is encountered, the buffer is filled, or R returns Ok(0).

If a different read error occurs, this function will return the error, and the amount that was read into the internal buffer before the error is unspecified (another call to next_chunk will clobber this data). In the future, a way to extract the leftover buffer after an error may be added. This method was chosen as it mirrors what Read::read_exact does on an error.

§Examples
let mut slice: &[u8] = &[0u8, 1, 2, 3];

let mut it = slice.read_chunks(2);

while let Some(chunk) = it.next_chunk() {
    // unwrap the io error, real code should handle this
    let chunk = chunk.unwrap();

    assert!(chunk == &[0, 1] || chunk == &[2, 3]);
}

// The slice implementation of `Read` will empty the slice.
// It is notable that read_chunks did not take ownership of
// slice however, only an exclusive borrow.
assert!(slice.is_empty());

Auto Trait Implementations§

§

impl<'a, R> Freeze for ReadChunks<'a, R>
where R: ?Sized,

§

impl<'a, R> RefUnwindSafe for ReadChunks<'a, R>
where R: RefUnwindSafe + ?Sized,

§

impl<'a, R> Send for ReadChunks<'a, R>
where R: Send + ?Sized,

§

impl<'a, R> Sync for ReadChunks<'a, R>
where R: Sync + ?Sized,

§

impl<'a, R> Unpin for ReadChunks<'a, R>
where R: ?Sized,

§

impl<'a, R> !UnwindSafe for ReadChunks<'a, R>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.