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