@@ -656,6 +656,18 @@ pub struct Coalesce<I, F>
656
656
f : F ,
657
657
}
658
658
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
+
659
671
impl < I : Clone , F : Clone > Clone for Coalesce < I , F >
660
672
where I : Iterator ,
661
673
I :: Item : Clone
@@ -685,12 +697,13 @@ pub fn coalesce<I, F>(mut iter: I, f: F) -> Coalesce<I, F>
685
697
686
698
impl < I , F > Iterator for Coalesce < I , F >
687
699
where I : Iterator ,
688
- F : FnMut ( I :: Item , I :: Item ) -> Result < I :: Item , ( I :: Item , I :: Item ) >
700
+ F : CoalescePredicate < I :: Item , I :: Item >
689
701
{
690
702
type Item = I :: Item ;
691
703
692
704
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) )
694
707
}
695
708
696
709
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
@@ -703,7 +716,7 @@ impl<I, F> Iterator for Coalesce<I, F>
703
716
if let Some ( last) = self . iter . last {
704
717
let mut f = self . f ;
705
718
let ( last, acc) = self . iter . iter . fold ( ( last, acc) , |( last, acc) , elt| {
706
- match f ( last, elt) {
719
+ match f. coalesce_pair ( last, elt) {
707
720
Ok ( joined) => ( joined, acc) ,
708
721
Err ( ( last_, next_) ) => ( next_, fn_acc ( acc, last_) ) ,
709
722
}
0 commit comments