Skip to content

Commit 642cbf0

Browse files
committed
Introduce CoalescePredicate
1 parent 6c6af09 commit 642cbf0

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/adaptors/mod.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,18 @@ pub struct Coalesce<I, F>
656656
f: F,
657657
}
658658

659+
pub trait CoalescePredicate<Item, T> {
660+
fn coalesce_pair(&mut self, t: T, item: Item) -> Result<T, (T, T)>;
661+
}
662+
663+
impl<F, Item, T> CoalescePredicate<Item, T> for F
664+
where F: FnMut(T, Item) -> Result<T, (T, T)>
665+
{
666+
fn coalesce_pair(&mut self, t: T, item: Item) -> Result<T, (T, T)> {
667+
self(t, item)
668+
}
669+
}
670+
659671
impl<I: Clone, F: Clone> Clone for Coalesce<I, F>
660672
where I: Iterator,
661673
I::Item: Clone
@@ -685,12 +697,13 @@ pub fn coalesce<I, F>(mut iter: I, f: F) -> Coalesce<I, F>
685697

686698
impl<I, F> Iterator for Coalesce<I, F>
687699
where I: Iterator,
688-
F: FnMut(I::Item, I::Item) -> Result<I::Item, (I::Item, I::Item)>
700+
F: CoalescePredicate<I::Item, I::Item>
689701
{
690702
type Item = I::Item;
691703

692704
fn next(&mut self) -> Option<Self::Item> {
693-
self.iter.next_with(&mut self.f)
705+
let f = &mut self.f;
706+
self.iter.next_with(|last, item| f.coalesce_pair(last, item))
694707
}
695708

696709
fn size_hint(&self) -> (usize, Option<usize>) {
@@ -703,7 +716,7 @@ impl<I, F> Iterator for Coalesce<I, F>
703716
if let Some(last) = self.iter.last {
704717
let mut f = self.f;
705718
let (last, acc) = self.iter.iter.fold((last, acc), |(last, acc), elt| {
706-
match f(last, elt) {
719+
match f.coalesce_pair(last, elt) {
707720
Ok(joined) => (joined, acc),
708721
Err((last_, next_)) => (next_, fn_acc(acc, last_)),
709722
}

0 commit comments

Comments
 (0)