Skip to content

Commit f966f9c

Browse files
authored
Try #475:
2 parents 180c4e0 + d03c37b commit f966f9c

File tree

5 files changed

+58
-22
lines changed

5 files changed

+58
-22
lines changed

src/adaptors/mod.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -795,9 +795,30 @@ macro_rules! impl_tuple_combination {
795795
)
796796
}
797797

798-
impl_tuple_combination!(Tuple2Combination Tuple1Combination ; A, A, A ; a);
799-
impl_tuple_combination!(Tuple3Combination Tuple2Combination ; A, A, A, A ; a b);
800-
impl_tuple_combination!(Tuple4Combination Tuple3Combination ; A, A, A, A, A; a b c);
798+
// This snippet generates the twelve `impl_tuple_combination!` invocations:
799+
// use core::iter;
800+
// use itertools::Itertools;
801+
//
802+
// for i in 2..=12 {
803+
// println!("impl_tuple_combination!(Tuple{arity}Combination Tuple{prev}Combination; {tys}; {idents});",
804+
// arity = i,
805+
// prev = i - 1,
806+
// tys = iter::repeat("A").take(i + 1).join(", "),
807+
// idents = ('a'..'z').take(i - 1).join(" "),
808+
// );
809+
// }
810+
// It could probably be replaced by a bit more macro cleverness.
811+
impl_tuple_combination!(Tuple2Combination Tuple1Combination; A, A, A; a);
812+
impl_tuple_combination!(Tuple3Combination Tuple2Combination; A, A, A, A; a b);
813+
impl_tuple_combination!(Tuple4Combination Tuple3Combination; A, A, A, A, A; a b c);
814+
impl_tuple_combination!(Tuple5Combination Tuple4Combination; A, A, A, A, A, A; a b c d);
815+
impl_tuple_combination!(Tuple6Combination Tuple5Combination; A, A, A, A, A, A, A; a b c d e);
816+
impl_tuple_combination!(Tuple7Combination Tuple6Combination; A, A, A, A, A, A, A, A; a b c d e f);
817+
impl_tuple_combination!(Tuple8Combination Tuple7Combination; A, A, A, A, A, A, A, A, A; a b c d e f g);
818+
impl_tuple_combination!(Tuple9Combination Tuple8Combination; A, A, A, A, A, A, A, A, A, A; a b c d e f g h);
819+
impl_tuple_combination!(Tuple10Combination Tuple9Combination; A, A, A, A, A, A, A, A, A, A, A; a b c d e f g h i);
820+
impl_tuple_combination!(Tuple11Combination Tuple10Combination; A, A, A, A, A, A, A, A, A, A, A, A; a b c d e f g h i j);
821+
impl_tuple_combination!(Tuple12Combination Tuple11Combination; A, A, A, A, A, A, A, A, A, A, A, A, A; a b c d e f g h i j k);
801822

802823
/// An iterator adapter to filter values within a nested `Result::Ok`.
803824
///

src/cons_tuples_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ macro_rules! impl_cons_iter(
3535
);
3636
);
3737

38-
impl_cons_iter!(A, B, C, D, E, F, G, H,);
38+
impl_cons_iter!(A, B, C, D, E, F, G, H, I, J, K, L,);
3939

4040
/// An iterator that maps an iterator of tuples like
4141
/// `((A, B), C)` to an iterator of `(A, B, C)`.

src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1250,7 +1250,7 @@ pub trait Itertools : Iterator {
12501250
/// elements from an iterator.
12511251
///
12521252
/// Iterator element can be any homogeneous tuple of type `Self::Item` with
1253-
/// size up to 4.
1253+
/// size up to 12.
12541254
///
12551255
/// ```
12561256
/// use itertools::Itertools;
@@ -1485,7 +1485,7 @@ pub trait Itertools : Iterator {
14851485

14861486
// non-adaptor methods
14871487
/// Advances the iterator and returns the next items grouped in a tuple of
1488-
/// a specific size (up to 4).
1488+
/// a specific size (up to 12).
14891489
///
14901490
/// If there are enough elements to be grouped in a tuple, then the tuple is
14911491
/// returned inside `Some`, otherwise `None` is returned.
@@ -1505,7 +1505,7 @@ pub trait Itertools : Iterator {
15051505
}
15061506

15071507
/// Collects all items from the iterator into a tuple of a specific size
1508-
/// (up to 4).
1508+
/// (up to 12).
15091509
///
15101510
/// If the number of elements inside the iterator is **exactly** equal to
15111511
/// the tuple size, then the tuple is returned inside `Some`, otherwise

src/tuple_impl.rs

+26-15
Original file line numberDiff line numberDiff line change
@@ -280,23 +280,14 @@ macro_rules! impl_tuple_collect {
280280
return None;
281281
}
282282

283-
#[allow(unused_assignments)]
284283
fn collect_from_iter_no_buf<I>(iter: I) -> Option<Self>
285284
where I: IntoIterator<Item = $A>
286285
{
287286
let mut iter = iter.into_iter();
288-
loop {
289-
$(
290-
let $Y = if let Some($Y) = iter.next() {
291-
$Y
292-
} else {
293-
break;
294-
};
295-
)*
296-
return Some(($($Y),*,))
297-
}
298287

299-
return None;
288+
Some(($(
289+
{ let $Y = iter.next()?; $Y },
290+
)*))
300291
}
301292

302293
fn num_items() -> usize {
@@ -307,17 +298,37 @@ macro_rules! impl_tuple_collect {
307298
use std::mem::replace;
308299

309300
let &mut ($(ref mut $Y),*,) = self;
310-
let tmp = item;
311301
$(
312-
let tmp = replace($Y_rev, tmp);
302+
let item = replace($Y_rev, item);
313303
)*
314-
drop(tmp);
304+
drop(item);
315305
}
316306
}
317307
)
318308
}
319309

310+
// This snippet generates the twelve `impl_tuple_collect!` invocations:
311+
// use core::iter;
312+
// use itertools::Itertools;
313+
//
314+
// for i in 1..=12 {
315+
// println!("impl_tuple_collect!({arity}; A; {ty}; {idents}; {rev_idents});",
316+
// arity=i,
317+
// ty=iter::repeat("A").take(i).join(", "),
318+
// idents=('a'..='z').take(i).join(", "),
319+
// rev_idents=('a'..='z').take(i).collect_vec().into_iter().rev().join(", ")
320+
// );
321+
// }
322+
// It could probably be replaced by a bit more macro cleverness.
320323
impl_tuple_collect!(1; A; A; a; a);
321324
impl_tuple_collect!(2; A; A, A; a, b; b, a);
322325
impl_tuple_collect!(3; A; A, A, A; a, b, c; c, b, a);
323326
impl_tuple_collect!(4; A; A, A, A, A; a, b, c, d; d, c, b, a);
327+
impl_tuple_collect!(5; A; A, A, A, A, A; a, b, c, d, e; e, d, c, b, a);
328+
impl_tuple_collect!(6; A; A, A, A, A, A, A; a, b, c, d, e, f; f, e, d, c, b, a);
329+
impl_tuple_collect!(7; A; A, A, A, A, A, A, A; a, b, c, d, e, f, g; g, f, e, d, c, b, a);
330+
impl_tuple_collect!(8; A; A, A, A, A, A, A, A, A; a, b, c, d, e, f, g, h; h, g, f, e, d, c, b, a);
331+
impl_tuple_collect!(9; A; A, A, A, A, A, A, A, A, A; a, b, c, d, e, f, g, h, i; i, h, g, f, e, d, c, b, a);
332+
impl_tuple_collect!(10; A; A, A, A, A, A, A, A, A, A, A; a, b, c, d, e, f, g, h, i, j; j, i, h, g, f, e, d, c, b, a);
333+
impl_tuple_collect!(11; A; A, A, A, A, A, A, A, A, A, A, A; a, b, c, d, e, f, g, h, i, j, k; k, j, i, h, g, f, e, d, c, b, a);
334+
impl_tuple_collect!(12; A; A, A, A, A, A, A, A, A, A, A, A, A; a, b, c, d, e, f, g, h, i, j, k, l; l, k, j, i, h, g, f, e, d, c, b, a);

src/ziptuple.rs

+4
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,7 @@ impl_zip_iter!(A, B, C, D, E);
133133
impl_zip_iter!(A, B, C, D, E, F);
134134
impl_zip_iter!(A, B, C, D, E, F, G);
135135
impl_zip_iter!(A, B, C, D, E, F, G, H);
136+
impl_zip_iter!(A, B, C, D, E, F, G, H, I);
137+
impl_zip_iter!(A, B, C, D, E, F, G, H, I, J);
138+
impl_zip_iter!(A, B, C, D, E, F, G, H, I, J, K);
139+
impl_zip_iter!(A, B, C, D, E, F, G, H, I, J, K, L);

0 commit comments

Comments
 (0)