Skip to content

Commit d03c37b

Browse files
committed
trait impls for tuples of arity <= 12
following the convention of libcore fixes #428 fixes #398
1 parent 89f3a0d commit d03c37b

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
@@ -806,9 +806,30 @@ macro_rules! impl_tuple_combination {
806806
)
807807
}
808808

809-
impl_tuple_combination!(Tuple2Combination Tuple1Combination ; A, A, A ; a);
810-
impl_tuple_combination!(Tuple3Combination Tuple2Combination ; A, A, A, A ; a b);
811-
impl_tuple_combination!(Tuple4Combination Tuple3Combination ; A, A, A, A, A; a b c);
809+
// This snippet generates the twelve `impl_tuple_combination!` invocations:
810+
// use core::iter;
811+
// use itertools::Itertools;
812+
//
813+
// for i in 2..=12 {
814+
// println!("impl_tuple_combination!(Tuple{arity}Combination Tuple{prev}Combination; {tys}; {idents});",
815+
// arity = i,
816+
// prev = i - 1,
817+
// tys = iter::repeat("A").take(i + 1).join(", "),
818+
// idents = ('a'..'z').take(i - 1).join(" "),
819+
// );
820+
// }
821+
// It could probably be replaced by a bit more macro cleverness.
822+
impl_tuple_combination!(Tuple2Combination Tuple1Combination; A, A, A; a);
823+
impl_tuple_combination!(Tuple3Combination Tuple2Combination; A, A, A, A; a b);
824+
impl_tuple_combination!(Tuple4Combination Tuple3Combination; A, A, A, A, A; a b c);
825+
impl_tuple_combination!(Tuple5Combination Tuple4Combination; A, A, A, A, A, A; a b c d);
826+
impl_tuple_combination!(Tuple6Combination Tuple5Combination; A, A, A, A, A, A, A; a b c d e);
827+
impl_tuple_combination!(Tuple7Combination Tuple6Combination; A, A, A, A, A, A, A, A; a b c d e f);
828+
impl_tuple_combination!(Tuple8Combination Tuple7Combination; A, A, A, A, A, A, A, A, A; a b c d e f g);
829+
impl_tuple_combination!(Tuple9Combination Tuple8Combination; A, A, A, A, A, A, A, A, A, A; a b c d e f g h);
830+
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);
831+
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);
832+
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);
812833

813834
/// An iterator adapter to filter values within a nested `Result::Ok`.
814835
///

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
@@ -109,3 +109,7 @@ impl_zip_iter!(A, B, C, D, E);
109109
impl_zip_iter!(A, B, C, D, E, F);
110110
impl_zip_iter!(A, B, C, D, E, F, G);
111111
impl_zip_iter!(A, B, C, D, E, F, G, H);
112+
impl_zip_iter!(A, B, C, D, E, F, G, H, I);
113+
impl_zip_iter!(A, B, C, D, E, F, G, H, I, J);
114+
impl_zip_iter!(A, B, C, D, E, F, G, H, I, J, K);
115+
impl_zip_iter!(A, B, C, D, E, F, G, H, I, J, K, L);

0 commit comments

Comments
 (0)