blob: ae0f48f3493d04372cbf5f63e0b56cbd371ccd5f [file] [log] [blame]
Jakub Kotura425e552020-12-21 17:28:15 +01001
2macro_rules! impl_cons_iter(
3 ($_A:ident, $_B:ident, ) => (); // stop
4
5 ($A:ident, $($B:ident,)*) => (
6 impl_cons_iter!($($B,)*);
7 #[allow(non_snake_case)]
8 impl<X, Iter, $($B),*> Iterator for ConsTuples<Iter, (($($B,)*), X)>
9 where Iter: Iterator<Item = (($($B,)*), X)>,
10 {
11 type Item = ($($B,)* X, );
12 fn next(&mut self) -> Option<Self::Item> {
13 self.iter.next().map(|(($($B,)*), x)| ($($B,)* x, ))
14 }
15
16 fn size_hint(&self) -> (usize, Option<usize>) {
17 self.iter.size_hint()
18 }
19 fn fold<Acc, Fold>(self, accum: Acc, mut f: Fold) -> Acc
20 where Fold: FnMut(Acc, Self::Item) -> Acc,
21 {
22 self.iter.fold(accum, move |acc, (($($B,)*), x)| f(acc, ($($B,)* x, )))
23 }
24 }
25
26 #[allow(non_snake_case)]
27 impl<X, Iter, $($B),*> DoubleEndedIterator for ConsTuples<Iter, (($($B,)*), X)>
28 where Iter: DoubleEndedIterator<Item = (($($B,)*), X)>,
29 {
30 fn next_back(&mut self) -> Option<Self::Item> {
31 self.iter.next().map(|(($($B,)*), x)| ($($B,)* x, ))
32 }
33 }
34
35 );
36);
37
Joel Galenson6f798712021-04-01 17:03:06 -070038impl_cons_iter!(A, B, C, D, E, F, G, H, I, J, K, L,);
Jakub Kotura425e552020-12-21 17:28:15 +010039
40/// An iterator that maps an iterator of tuples like
41/// `((A, B), C)` to an iterator of `(A, B, C)`.
42///
43/// Used by the `iproduct!()` macro.
44#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
45#[derive(Debug)]
46pub struct ConsTuples<I, J>
47 where I: Iterator<Item=J>,
48{
49 iter: I,
50}
51
52impl<I, J> Clone for ConsTuples<I, J>
53 where I: Clone + Iterator<Item=J>,
54{
55 clone_fields!(iter);
56}
57
58/// Create an iterator that maps for example iterators of
59/// `((A, B), C)` to `(A, B, C)`.
Joel Galenson6f798712021-04-01 17:03:06 -070060pub fn cons_tuples<I, J>(iterable: I) -> ConsTuples<I::IntoIter, J>
61 where I: IntoIterator<Item=J>
Jakub Kotura425e552020-12-21 17:28:15 +010062{
63 ConsTuples { iter: iterable.into_iter() }
64}