1pub struct Chunk<I, const N: usize> {
9 iter: I,
10}
11
12pub trait ChunkOps: Iterator + Sized {
13 fn chunk<const N: usize>(self) -> Chunk<Self, N>;
14}
15
16impl<I: Iterator> ChunkOps for I {
17 fn chunk<const N: usize>(self) -> Chunk<Self, N> {
18 Chunk { iter: self }
19 }
20}
21
22macro_rules! iterator {
23 ($n:literal $($var:ident)+) => {
24 impl<I: Iterator> Iterator for Chunk<I, $n> {
25 type Item = [I::Item; $n];
26
27 #[inline]
28 fn size_hint(&self) -> (usize, Option<usize>) {
29 let (lo, hi) = self.iter.size_hint();
30 (lo / $n, hi.map(|h| h / $n))
31 }
32
33 #[inline]
34 fn next(&mut self) -> Option<Self::Item> {
35 $(let $var = self.iter.next()?;)+
36 Some([$($var),+])
37 }
38 }
39 };
40}
41
42iterator!(2 a b);
43iterator!(3 a b c);
44iterator!(4 a b c d);
45iterator!(5 a b c d e);
46iterator!(6 a b c d e f);
47iterator!(7 a b c d e f g);
48iterator!(8 a b c d e f g h);
49iterator!(9 a b c d e f g h i);
50iterator!(10 a b c d e f g h i j);
51iterator!(11 a b c d e f g h i j k);
52iterator!(12 a b c d e f g h i j k l);