Skip to content

Commit af54286

Browse files
bors[bot]jswrenndmitris
authored
Merge #475 #476 #479
475: Trait impls for tuples of arity <= 12 r=jswrenn a=jswrenn I stopped at 12, because that's where libcore stops. fixes #428 fixes #398 476: Undeprecate and optimize `fold_while` r=jswrenn a=jswrenn Prompted by #469. 479: fix compiler warning on array.into_iter() r=jswrenn a=dmitris Fix the compile warnings listed below by changing `into_iter()` invocation to `iter()` in the `impl_zip_iter` macro as recommended in rust-lang/rust#66145. For additional background, see also rust-lang/rust#65819 and rust-lang/rust#66017 (the latter is the linter change producing the warning). ``` $ cargo build Compiling itertools v0.9.0 (/Users/dsavints/dev/hack/github.com/rust-itertools/itertools) warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. --> src/ziptuple.rs:111:47 | 111 | let size = *[$( $B.len(), )*].into_iter().min().unwrap(); | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` ... 128 | impl_zip_iter!(A); | ------------------ in this macro invocation | = note: `#[warn(array_into_iter)]` on by default = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #66145 <rust-lang/rust#66145> = note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. --> src/ziptuple.rs:111:47 | 111 | let size = *[$( $B.len(), )*].into_iter().min().unwrap(); | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` ... 129 | impl_zip_iter!(A, B); | --------------------- in this macro invocation | = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #66145 <rust-lang/rust#66145> = note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) ``` Co-authored-by: Jack Wrenn <me@jswrenn.com> Co-authored-by: Dmitry Savintsev <dsavints@verizonmedia.com>
4 parents f65c0fd + d03c37b + be90cf3 + bc38214 commit af54286

File tree

5 files changed

+73
-30
lines changed

5 files changed

+73
-30
lines changed

Diff for: 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
///

Diff for: 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)`.

Diff for: src/lib.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,7 @@ pub trait Itertools : Iterator {
12591259
/// elements from an iterator.
12601260
///
12611261
/// Iterator element can be any homogeneous tuple of type `Self::Item` with
1262-
/// size up to 4.
1262+
/// size up to 12.
12631263
///
12641264
/// ```
12651265
/// use itertools::Itertools;
@@ -1494,7 +1494,7 @@ pub trait Itertools : Iterator {
14941494

14951495
// non-adaptor methods
14961496
/// Advances the iterator and returns the next items grouped in a tuple of
1497-
/// a specific size (up to 4).
1497+
/// a specific size (up to 12).
14981498
///
14991499
/// If there are enough elements to be grouped in a tuple, then the tuple is
15001500
/// returned inside `Some`, otherwise `None` is returned.
@@ -1514,7 +1514,7 @@ pub trait Itertools : Iterator {
15141514
}
15151515

15161516
/// Collects all items from the iterator into a tuple of a specific size
1517-
/// (up to 4).
1517+
/// (up to 12).
15181518
///
15191519
/// If the number of elements inside the iterator is **exactly** equal to
15201520
/// the tuple size, then the tuple is returned inside `Some`, otherwise
@@ -2108,19 +2108,26 @@ pub trait Itertools : Iterator {
21082108
/// The big difference between the computations of `result2` and `result3` is that while
21092109
/// `fold()` called the provided closure for every item of the callee iterator,
21102110
/// `fold_while()` actually stopped iterating as soon as it encountered `Fold::Done(_)`.
2111-
#[deprecated(note="Use .try_fold() instead", since="0.8.0")]
21122111
fn fold_while<B, F>(&mut self, init: B, mut f: F) -> FoldWhile<B>
21132112
where Self: Sized,
21142113
F: FnMut(B, Self::Item) -> FoldWhile<B>
21152114
{
2116-
let mut acc = init;
2117-
for item in self {
2118-
match f(acc, item) {
2119-
FoldWhile::Continue(res) => acc = res,
2120-
res @ FoldWhile::Done(_) => return res,
2115+
use Result::{
2116+
Ok as Continue,
2117+
Err as Break,
2118+
};
2119+
2120+
let result = self.try_fold(init, #[inline(always)] |acc, v|
2121+
match f(acc, v) {
2122+
FoldWhile::Continue(acc) => Continue(acc),
2123+
FoldWhile::Done(acc) => Break(acc),
21212124
}
2125+
);
2126+
2127+
match result {
2128+
Continue(acc) => FoldWhile::Continue(acc),
2129+
Break(acc) => FoldWhile::Done(acc),
21222130
}
2123-
FoldWhile::Continue(acc)
21242131
}
21252132

21262133
/// Iterate over the entire iterator and add all the elements.

Diff for: 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);

Diff for: src/ziptuple.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ macro_rules! impl_zip_iter {
108108
#[inline]
109109
fn next_back(&mut self) -> Option<Self::Item> {
110110
let ($(ref mut $B,)*) = self.t;
111-
let size = *[$( $B.len(), )*].into_iter().min().unwrap();
111+
let size = *[$( $B.len(), )*].iter().min().unwrap();
112112

113113
$(
114114
if $B.len() != size {
@@ -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)