Skip to content

Commit

Permalink
Merge #475 #476 #479
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
3 people committed Sep 22, 2020
4 parents f65c0fd + d03c37b + be90cf3 + bc38214 commit af54286
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 30 deletions.
27 changes: 24 additions & 3 deletions src/adaptors/mod.rs
Expand Up @@ -795,9 +795,30 @@ macro_rules! impl_tuple_combination {
)
}

impl_tuple_combination!(Tuple2Combination Tuple1Combination ; A, A, A ; a);
impl_tuple_combination!(Tuple3Combination Tuple2Combination ; A, A, A, A ; a b);
impl_tuple_combination!(Tuple4Combination Tuple3Combination ; A, A, A, A, A; a b c);
// This snippet generates the twelve `impl_tuple_combination!` invocations:
// use core::iter;
// use itertools::Itertools;
//
// for i in 2..=12 {
// println!("impl_tuple_combination!(Tuple{arity}Combination Tuple{prev}Combination; {tys}; {idents});",
// arity = i,
// prev = i - 1,
// tys = iter::repeat("A").take(i + 1).join(", "),
// idents = ('a'..'z').take(i - 1).join(" "),
// );
// }
// It could probably be replaced by a bit more macro cleverness.
impl_tuple_combination!(Tuple2Combination Tuple1Combination; A, A, A; a);
impl_tuple_combination!(Tuple3Combination Tuple2Combination; A, A, A, A; a b);
impl_tuple_combination!(Tuple4Combination Tuple3Combination; A, A, A, A, A; a b c);
impl_tuple_combination!(Tuple5Combination Tuple4Combination; A, A, A, A, A, A; a b c d);
impl_tuple_combination!(Tuple6Combination Tuple5Combination; A, A, A, A, A, A, A; a b c d e);
impl_tuple_combination!(Tuple7Combination Tuple6Combination; A, A, A, A, A, A, A, A; a b c d e f);
impl_tuple_combination!(Tuple8Combination Tuple7Combination; A, A, A, A, A, A, A, A, A; a b c d e f g);
impl_tuple_combination!(Tuple9Combination Tuple8Combination; A, A, A, A, A, A, A, A, A, A; a b c d e f g h);
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);
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);
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);

/// An iterator adapter to filter values within a nested `Result::Ok`.
///
Expand Down
2 changes: 1 addition & 1 deletion src/cons_tuples_impl.rs
Expand Up @@ -35,7 +35,7 @@ macro_rules! impl_cons_iter(
);
);

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

/// An iterator that maps an iterator of tuples like
/// `((A, B), C)` to an iterator of `(A, B, C)`.
Expand Down
27 changes: 17 additions & 10 deletions src/lib.rs
Expand Up @@ -1259,7 +1259,7 @@ pub trait Itertools : Iterator {
/// elements from an iterator.
///
/// Iterator element can be any homogeneous tuple of type `Self::Item` with
/// size up to 4.
/// size up to 12.
///
/// ```
/// use itertools::Itertools;
Expand Down Expand Up @@ -1494,7 +1494,7 @@ pub trait Itertools : Iterator {

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

/// Collects all items from the iterator into a tuple of a specific size
/// (up to 4).
/// (up to 12).
///
/// If the number of elements inside the iterator is **exactly** equal to
/// the tuple size, then the tuple is returned inside `Some`, otherwise
Expand Down Expand Up @@ -2108,19 +2108,26 @@ pub trait Itertools : Iterator {
/// The big difference between the computations of `result2` and `result3` is that while
/// `fold()` called the provided closure for every item of the callee iterator,
/// `fold_while()` actually stopped iterating as soon as it encountered `Fold::Done(_)`.
#[deprecated(note="Use .try_fold() instead", since="0.8.0")]
fn fold_while<B, F>(&mut self, init: B, mut f: F) -> FoldWhile<B>
where Self: Sized,
F: FnMut(B, Self::Item) -> FoldWhile<B>
{
let mut acc = init;
for item in self {
match f(acc, item) {
FoldWhile::Continue(res) => acc = res,
res @ FoldWhile::Done(_) => return res,
use Result::{
Ok as Continue,
Err as Break,
};

let result = self.try_fold(init, #[inline(always)] |acc, v|
match f(acc, v) {
FoldWhile::Continue(acc) => Continue(acc),
FoldWhile::Done(acc) => Break(acc),
}
);

match result {
Continue(acc) => FoldWhile::Continue(acc),
Break(acc) => FoldWhile::Done(acc),
}
FoldWhile::Continue(acc)
}

/// Iterate over the entire iterator and add all the elements.
Expand Down
41 changes: 26 additions & 15 deletions src/tuple_impl.rs
Expand Up @@ -280,23 +280,14 @@ macro_rules! impl_tuple_collect {
return None;
}

#[allow(unused_assignments)]
fn collect_from_iter_no_buf<I>(iter: I) -> Option<Self>
where I: IntoIterator<Item = $A>
{
let mut iter = iter.into_iter();
loop {
$(
let $Y = if let Some($Y) = iter.next() {
$Y
} else {
break;
};
)*
return Some(($($Y),*,))
}

return None;
Some(($(
{ let $Y = iter.next()?; $Y },
)*))
}

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

let &mut ($(ref mut $Y),*,) = self;
let tmp = item;
$(
let tmp = replace($Y_rev, tmp);
let item = replace($Y_rev, item);
)*
drop(tmp);
drop(item);
}
}
)
}

// This snippet generates the twelve `impl_tuple_collect!` invocations:
// use core::iter;
// use itertools::Itertools;
//
// for i in 1..=12 {
// println!("impl_tuple_collect!({arity}; A; {ty}; {idents}; {rev_idents});",
// arity=i,
// ty=iter::repeat("A").take(i).join(", "),
// idents=('a'..='z').take(i).join(", "),
// rev_idents=('a'..='z').take(i).collect_vec().into_iter().rev().join(", ")
// );
// }
// It could probably be replaced by a bit more macro cleverness.
impl_tuple_collect!(1; A; A; a; a);
impl_tuple_collect!(2; A; A, A; a, b; b, a);
impl_tuple_collect!(3; A; A, A, A; a, b, c; c, b, a);
impl_tuple_collect!(4; A; A, A, A, A; a, b, c, d; d, c, b, a);
impl_tuple_collect!(5; A; A, A, A, A, A; a, b, c, d, e; e, d, c, b, a);
impl_tuple_collect!(6; A; A, A, A, A, A, A; a, b, c, d, e, f; f, e, d, c, b, a);
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);
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);
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);
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);
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);
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);
6 changes: 5 additions & 1 deletion src/ziptuple.rs
Expand Up @@ -108,7 +108,7 @@ macro_rules! impl_zip_iter {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
let ($(ref mut $B,)*) = self.t;
let size = *[$( $B.len(), )*].into_iter().min().unwrap();
let size = *[$( $B.len(), )*].iter().min().unwrap();

$(
if $B.len() != size {
Expand All @@ -133,3 +133,7 @@ impl_zip_iter!(A, B, C, D, E);
impl_zip_iter!(A, B, C, D, E, F);
impl_zip_iter!(A, B, C, D, E, F, G);
impl_zip_iter!(A, B, C, D, E, F, G, H);
impl_zip_iter!(A, B, C, D, E, F, G, H, I);
impl_zip_iter!(A, B, C, D, E, F, G, H, I, J);
impl_zip_iter!(A, B, C, D, E, F, G, H, I, J, K);
impl_zip_iter!(A, B, C, D, E, F, G, H, I, J, K, L);

0 comments on commit af54286

Please sign in to comment.