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>
impl<R: Read + ?Sized> ReadChunks<'_, R>
Sourcepub fn next_chunk(&mut self) -> Option<Result<&mut [u8]>>
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());