use crate::cell::MelinoeCell;
use super::WriterShard;
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct ShardChunks<'a, 'brand, T> {
pub(super) rest: Option<&'a mut [MelinoeCell<'brand, T>]>,
pub(super) chunk: usize,
}
impl<'a, 'brand, T> Iterator for ShardChunks<'a, 'brand, T> {
type Item = WriterShard<'a, 'brand, T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let slice = self.rest.take()?;
if slice.is_empty() {
return None;
}
let mid = self.chunk.min(slice.len());
let (head, tail) = slice.split_at_mut(mid);
self.rest = Some(tail);
Some(WriterShard::new(head))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.rest.as_ref().map_or(0, |s| s.len());
let shards = if remaining == 0 {
0
} else {
1 + (remaining - 1) / self.chunk
};
(shards, Some(shards))
}
}
impl<'a, 'brand, T> ExactSizeIterator for ShardChunks<'a, 'brand, T> {}
impl<'a, 'brand, T> core::fmt::Debug for ShardChunks<'a, 'brand, T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let remaining = self.rest.as_ref().map_or(0, |s| s.len());
f.debug_struct("ShardChunks")
.field("remaining", &remaining)
.field("chunk", &self.chunk)
.finish()
}
}