gix_utils/iter.rs
1//! Iterator utilities.
2/// An iterator over chunks of input, producing `Vec<Item>` with a size of `size`, with the last chunk being the remainder and thus
3/// potentially smaller than `size`.
4pub struct Chunks<I> {
5 /// The inner iterator to ask for items.
6 pub inner: I,
7 /// The size of chunks to produce
8 pub size: usize,
9}
10
11impl<I, Item> Iterator for Chunks<I>
12where
13 I: Iterator<Item = Item>,
14{
15 type Item = Vec<Item>;
16
17 fn next(&mut self) -> Option<Self::Item> {
18 let mut res = Vec::with_capacity(self.size);
19 let mut items_left = self.size;
20 for item in &mut self.inner {
21 res.push(item);
22 items_left -= 1;
23 if items_left == 0 {
24 break;
25 }
26 }
27 (!res.is_empty()).then_some(res)
28 }
29}