Skip to content

Commit 663533e

Browse files
ControlFlow instead of Result
1 parent d8a24d3 commit 663533e

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

src/adaptors/mod.rs

+13-17
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::size_hint::{self, SizeHint};
1616
use std::fmt;
1717
use std::iter::{Enumerate, FromIterator, Fuse, FusedIterator};
1818
use std::marker::PhantomData;
19+
use std::ops::ControlFlow;
1920

2021
/// An iterator adaptor that alternates elements from two iterators until both
2122
/// run out.
@@ -93,13 +94,13 @@ where
9394
let res = i.try_fold(init, |mut acc, x| {
9495
acc = f(acc, x);
9596
match j.next() {
96-
Some(y) => Ok(f(acc, y)),
97-
None => Err(acc),
97+
Some(y) => ControlFlow::Continue(f(acc, y)),
98+
None => ControlFlow::Break(acc),
9899
}
99100
});
100101
match res {
101-
Ok(acc) => j.fold(acc, f),
102-
Err(acc) => i.fold(acc, f),
102+
ControlFlow::Continue(acc) => j.fold(acc, f),
103+
ControlFlow::Break(acc) => i.fold(acc, f),
103104
}
104105
}
105106
}
@@ -216,14 +217,12 @@ where
216217
let res = i.try_fold(init, |mut acc, x| {
217218
acc = f(acc, x);
218219
match j.next() {
219-
Some(y) => Ok(f(acc, y)),
220-
None => Err(acc),
220+
Some(y) => ControlFlow::Continue(f(acc, y)),
221+
None => ControlFlow::Break(acc),
221222
}
222223
});
223-
match res {
224-
Ok(val) => val,
225-
Err(val) => val,
226-
}
224+
let (ControlFlow::Continue(val) | ControlFlow::Break(val)) = res;
225+
val
227226
}
228227
}
229228

@@ -595,14 +594,11 @@ where
595594
F: FnMut(B, Self::Item) -> B,
596595
{
597596
let res = self.iter.try_fold(acc, |acc, item| match item {
598-
Some(item) => Ok(f(acc, item)),
599-
None => Err(acc),
597+
Some(item) => ControlFlow::Continue(f(acc, item)),
598+
None => ControlFlow::Break(acc),
600599
});
601-
602-
match res {
603-
Ok(val) => val,
604-
Err(val) => val,
605-
}
600+
let (ControlFlow::Continue(val) | ControlFlow::Break(val)) = res;
601+
val
606602
}
607603
}
608604

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2712,7 +2712,7 @@ pub trait Itertools: Iterator {
27122712
Self: Sized,
27132713
F: FnMut(B, Self::Item) -> FoldWhile<B>,
27142714
{
2715-
use Result::{Err as Break, Ok as Continue};
2715+
use core::ops::ControlFlow::{Break, Continue};
27162716

27172717
let result = self.try_fold(
27182718
init,

src/zip_longest.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::size_hint;
2+
use core::ops::ControlFlow;
23
use std::cmp::Ordering::{Equal, Greater, Less};
34
use std::iter::{Fuse, FusedIterator};
45

@@ -62,12 +63,12 @@ where
6263
{
6364
let Self { mut a, mut b } = self;
6465
let res = a.try_fold(init, |init, a| match b.next() {
65-
Some(b) => Ok(f(init, EitherOrBoth::Both(a, b))),
66-
None => Err(f(init, EitherOrBoth::Left(a))),
66+
Some(b) => ControlFlow::Continue(f(init, EitherOrBoth::Both(a, b))),
67+
None => ControlFlow::Break(f(init, EitherOrBoth::Left(a))),
6768
});
6869
match res {
69-
Ok(acc) => b.map(EitherOrBoth::Right).fold(acc, f),
70-
Err(acc) => a.map(EitherOrBoth::Left).fold(acc, f),
70+
ControlFlow::Continue(acc) => b.map(EitherOrBoth::Right).fold(acc, f),
71+
ControlFlow::Break(acc) => a.map(EitherOrBoth::Left).fold(acc, f),
7172
}
7273
}
7374
}

0 commit comments

Comments
 (0)