Skip to content

Commit 1257f47

Browse files
committed
Avoid two-layer tries, doubling short-circuit logic
1 parent dedb06c commit 1257f47

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

src/iter/mod.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,7 @@ pub trait ParallelIterator: Sized + Send {
370370
where OP: Fn(Self::Item) -> R + Sync + Send,
371371
R: Try<Ok = ()> + Send
372372
{
373-
self.try_fold_with((), move |(), x| op(x))
374-
.try_reduce(|| (), |(), ()| R::from_ok(()))
373+
self.map(op).try_reduce(|| (), |(), ()| R::from_ok(()))
375374
}
376375

377376
/// TODO
@@ -715,13 +714,8 @@ pub trait ParallelIterator: Sized + Send {
715714
<Self::Item as Try>::Ok: Send,
716715
<Self::Item as Try>::Error: Send
717716
{
718-
let result = self.try_fold(
719-
|| None,
720-
|opt_a, try_b| match (opt_a, try_b.into_result()) {
721-
(Some(a), Ok(b)) => op(a, b).into_result().map(Some),
722-
(_, res_b) => res_b.map(Some),
723-
},
724-
).try_reduce(
717+
// Map into `Result<Option<Ok>, Error>`, then reduce it.
718+
let result = self.map(|try_b| try_b.into_result().map(Some)).try_reduce(
725719
|| None,
726720
|opt_a, opt_b| match (opt_a, opt_b) {
727721
(Some(a), Some(b)) => op(a, b).into_result().map(Some),
@@ -730,6 +724,7 @@ pub trait ParallelIterator: Sized + Send {
730724
},
731725
);
732726

727+
// Map `Result<Option<Ok>, Error>` back to `Option<Self::Item>`.
733728
match result {
734729
Ok(None) => None,
735730
Ok(Some(v)) => Some(Self::Item::from_ok(v)),

0 commit comments

Comments
 (0)