Skip to content

Commit

Permalink
Merge branch 'master' into coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleades authored Jan 15, 2024
2 parents 18811c1 + f60fe7e commit 2f024d7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: taiki-e/install-action@cargo-no-dev-deps
- uses: dtolnay/[email protected]
- uses: dtolnay/rust-toolchain@master
with:
# Here, it does not trigger a PR from dependabot.
toolchain: 1.43.1
- run: cargo no-dev-deps check

test:
Expand Down
22 changes: 22 additions & 0 deletions src/take_while_inclusive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ where
(0, self.iter.size_hint().1)
}
}

fn fold<B, Fold>(mut self, init: B, mut f: Fold) -> B
where
Fold: FnMut(B, Self::Item) -> B,
{
if self.done {
init
} else {
let predicate = &mut self.predicate;
self.iter
.try_fold(init, |mut acc, item| {
let is_ok = predicate(&item);
acc = f(acc, item);
if is_ok {
Ok(acc)
} else {
Err(acc)
}
})
.unwrap_or_else(|err| err)
}
}
}

impl<I, F> FusedIterator for TakeWhileInclusive<I, F>
Expand Down
31 changes: 31 additions & 0 deletions src/zip_longest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,37 @@ where
Less => self.b.next_back().map(EitherOrBoth::Right),
}
}

fn rfold<B, F>(self, mut init: B, mut f: F) -> B
where
F: FnMut(B, Self::Item) -> B,
{
let Self { mut a, mut b } = self;
let a_len = a.len();
let b_len = b.len();
match a_len.cmp(&b_len) {
Equal => {}
Greater => {
init = a
.by_ref()
.rev()
.take(a_len - b_len)
.map(EitherOrBoth::Left)
.fold(init, &mut f)
}
Less => {
init = b
.by_ref()
.rev()
.take(b_len - a_len)
.map(EitherOrBoth::Right)
.fold(init, &mut f)
}
}
a.rfold(init, |acc, item_a| {
f(acc, EitherOrBoth::Both(item_a, b.next_back().unwrap()))
})
}
}

impl<T, U> ExactSizeIterator for ZipLongest<T, U>
Expand Down

0 comments on commit 2f024d7

Please sign in to comment.