Skip to content

Commit e2a0a6e

Browse files
committed
ConsTuples is a MapSpecialCase
1 parent 4f49f26 commit e2a0a6e

File tree

1 file changed

+13
-32
lines changed

1 file changed

+13
-32
lines changed

src/cons_tuples_impl.rs

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,39 @@
1+
use crate::adaptors::map::{MapSpecialCase, MapSpecialCaseFn};
2+
13
macro_rules! impl_cons_iter(
24
($_A:ident, $_B:ident, ) => (); // stop
35

46
($A:ident, $($B:ident,)*) => (
57
impl_cons_iter!($($B,)*);
68
#[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, )
2213
}
2314
}
2415
);
2516
);
2617

2718
impl_cons_iter!(A, B, C, D, E, F, G, H, I, J, K, L,);
2819

20+
#[derive(Debug, Clone)]
21+
pub struct ConsTuplesFn;
22+
2923
/// An iterator that maps an iterator of tuples like
3024
/// `((A, B), C)` to an iterator of `(A, B, C)`.
3125
///
3226
/// 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>;
4828

4929
/// Create an iterator that maps for example iterators of
5030
/// `((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>
5232
where
53-
I: IntoIterator<Item = J>,
33+
I: IntoIterator,
5434
{
5535
ConsTuples {
5636
iter: iterable.into_iter(),
37+
f: ConsTuplesFn,
5738
}
5839
}

0 commit comments

Comments
 (0)