pub struct BytesChunkIter<'b> {
pub bytes: &'b [u8],
pub index: usize,
pub length: usize,
pub chunk_size: usize,
}
impl<'b> BytesChunkIter<'b> {
pub fn new(bytes: &'b [u8], chunk_size: usize) -> BytesChunkIter<'b> {
Self {
bytes,
index: 0,
length: bytes.len(),
chunk_size,
}
}
pub fn next(&mut self) -> Option<(&[u8], Option<usize>)> {
let remaining = self.length - self.index;
if remaining == 0 {
return None;
}
let (chunk, padding) = if remaining < self.chunk_size {
(&self.bytes[self.index..], Some(self.chunk_size - remaining))
} else {
(
&self.bytes[self.index..(self.index + self.chunk_size)],
None,
)
};
self.index = (self.index + self.chunk_size).min(self.length);
Some((chunk, padding))
}
}