Skip to content

Commit

Permalink
ControlFlow instead of Result
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe-Cholet committed Jul 4, 2024
1 parent df00c92 commit c9d0aef
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
30 changes: 13 additions & 17 deletions src/adaptors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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),
}
}
}
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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),

Check warning on line 598 in src/adaptors/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/adaptors/mod.rs#L598

Added line #L598 was not covered by tests
});

match res {
Ok(val) => val,
Err(val) => val,
}
let (ControlFlow::Continue(val) | ControlFlow::Break(val)) = res;
val
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2714,7 +2714,7 @@ pub trait Itertools: Iterator {
Self: Sized,
F: FnMut(B, Self::Item) -> FoldWhile<B>,
{
use Result::{Err as Break, Ok as Continue};
use std::ops::ControlFlow::{Break, Continue};

let result = self.try_fold(
init,
Expand Down
9 changes: 5 additions & 4 deletions src/zip_longest.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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),
}
}
}
Expand Down

0 comments on commit c9d0aef

Please sign in to comment.