pub struct ChunksExact<const N: usize, I> {
iter: I,
}
impl<const N: usize, I, T> Iterator for ChunksExact<N, I>
where
I: Iterator<Item = T>,
{
type Item = [T; N];
fn next(&mut self) -> Option<Self::Item> {
assert_ne!(N, 0);
let mut vec: Vec<T> = Vec::with_capacity(N);
for _ in 0..N {
match self.iter.next() {
Some(item) => vec.push(item),
None => return None,
}
}
let ary: [T; N] = vec.try_into().unwrap_or_else(|v: Vec<T>| {
panic!("Expected a Vec of length {} but it was {}", N, v.len())
});
Some(ary)
}
}
pub(crate) trait ChunksExactIterator: Sized {
fn chunks_exact<const N: usize>(self) -> ChunksExact<N, Self> {
assert!(N != 0, "chunk size must be non-zero");
ChunksExact { iter: self }
}
}
impl<I> ChunksExactIterator for I where I: Iterator {}
pub(crate) trait Tap: Sized {
#[inline(always)]
fn tap_mut<F: FnOnce(&mut Self)>(mut self, f: F) -> Self {
f(&mut self);
self
}
#[inline(always)]
fn tap_mut_if<F: FnOnce(&mut Self)>(mut self, cond: bool, f: F) -> Self {
if cond {
f(&mut self);
}
self
}
#[inline(always)]
fn pipe_if<F: FnOnce(Self) -> Self>(self, cond: bool, f: F) -> Self {
if cond {
f(self)
} else {
self
}
}
}
impl<T> Tap for T where T: Sized {}
#[cfg(test)]
#[path = "std_ext.test.rs"]
mod test;