diff --git a/src/adaptors/mod.rs b/src/adaptors/mod.rs index d717e5408..dc6736e31 100644 --- a/src/adaptors/mod.rs +++ b/src/adaptors/mod.rs @@ -16,6 +16,7 @@ use crate::size_hint::{self, SizeHint}; use std::fmt; use std::iter::{Enumerate, FromIterator, Fuse, FusedIterator}; use std::marker::PhantomData; +use std::ops::ControlFlow; /// An iterator adaptor that alternates elements from two iterators until both /// run out. @@ -93,13 +94,13 @@ where let res = i.try_fold(init, |mut acc, x| { acc = f(acc, x); match j.next() { - Some(y) => Ok(f(acc, y)), - None => Err(acc), + Some(y) => ControlFlow::Continue(f(acc, y)), + None => ControlFlow::Break(acc), } }); match res { - Ok(acc) => j.fold(acc, f), - Err(acc) => i.fold(acc, f), + ControlFlow::Continue(acc) => j.fold(acc, f), + ControlFlow::Break(acc) => i.fold(acc, f), } } } @@ -216,14 +217,12 @@ where let res = i.try_fold(init, |mut acc, x| { acc = f(acc, x); match j.next() { - Some(y) => Ok(f(acc, y)), - None => Err(acc), + Some(y) => ControlFlow::Continue(f(acc, y)), + None => ControlFlow::Break(acc), } }); - match res { - Ok(val) => val, - Err(val) => val, - } + let (ControlFlow::Continue(val) | ControlFlow::Break(val)) = res; + val } } @@ -595,14 +594,11 @@ where F: FnMut(B, Self::Item) -> B, { let res = self.iter.try_fold(acc, |acc, item| match item { - Some(item) => Ok(f(acc, item)), - None => Err(acc), + Some(item) => ControlFlow::Continue(f(acc, item)), + None => ControlFlow::Break(acc), }); - - match res { - Ok(val) => val, - Err(val) => val, - } + let (ControlFlow::Continue(val) | ControlFlow::Break(val)) = res; + val } } diff --git a/src/lib.rs b/src/lib.rs index fbff95ba8..f4799f4ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2714,7 +2714,7 @@ pub trait Itertools: Iterator { Self: Sized, F: FnMut(B, Self::Item) -> FoldWhile, { - use Result::{Err as Break, Ok as Continue}; + use std::ops::ControlFlow::{Break, Continue}; let result = self.try_fold( init, diff --git a/src/zip_longest.rs b/src/zip_longest.rs index d4eb9a882..8b3a47c46 100644 --- a/src/zip_longest.rs +++ b/src/zip_longest.rs @@ -1,6 +1,7 @@ use super::size_hint; use std::cmp::Ordering::{Equal, Greater, Less}; use std::iter::{Fuse, FusedIterator}; +use std::ops::ControlFlow; use crate::either_or_both::EitherOrBoth; @@ -62,12 +63,12 @@ where { let Self { mut a, mut b } = self; let res = a.try_fold(init, |init, a| match b.next() { - Some(b) => Ok(f(init, EitherOrBoth::Both(a, b))), - None => Err(f(init, EitherOrBoth::Left(a))), + Some(b) => ControlFlow::Continue(f(init, EitherOrBoth::Both(a, b))), + None => ControlFlow::Break(f(init, EitherOrBoth::Left(a))), }); match res { - Ok(acc) => b.map(EitherOrBoth::Right).fold(acc, f), - Err(acc) => a.map(EitherOrBoth::Left).fold(acc, f), + ControlFlow::Continue(acc) => b.map(EitherOrBoth::Right).fold(acc, f), + ControlFlow::Break(acc) => a.map(EitherOrBoth::Left).fold(acc, f), } } }