@@ -605,27 +605,19 @@ impl<I, J, F> Iterator for MergeBy<I, J, F>
605
605
}
606
606
}
607
607
608
- #[ derive( Clone , Debug ) ]
609
- pub struct CoalesceCore < I , T >
610
- where I : Iterator
611
- {
612
- iter : I ,
613
- last : Option < T > ,
614
- }
615
-
616
- impl < I , T > CoalesceCore < I , T >
608
+ impl < I , F , T > CoalesceBy < I , F , T >
617
609
where I : Iterator
618
610
{
619
- fn next_with < F > ( & mut self , mut f : F ) -> Option < T >
620
- where F : FnMut ( T , I :: Item ) -> Result < T , ( T , T ) >
611
+ fn next_with ( & mut self ) -> Option < T >
612
+ where F : CoalescePredicate < I :: Item , T >
621
613
{
622
614
// this fuses the iterator
623
615
let mut last = match self . last . take ( ) {
624
616
None => return None ,
625
617
Some ( x) => x,
626
618
} ;
627
619
for next in & mut self . iter {
628
- match f ( last, next) {
620
+ match self . f . coalesce_pair ( last, next) {
629
621
Ok ( joined) => last = joined,
630
622
Err ( ( last_, next_) ) => {
631
623
self . last = Some ( next_) ;
@@ -637,7 +629,7 @@ impl<I, T> CoalesceCore<I, T>
637
629
Some ( last)
638
630
}
639
631
640
- fn size_hint ( & self ) -> ( usize , Option < usize > ) {
632
+ fn size_hint_internal ( & self ) -> ( usize , Option < usize > ) {
641
633
let ( low, hi) = size_hint:: add_scalar ( self . iter . size_hint ( ) ,
642
634
self . last . is_some ( ) as usize ) ;
643
635
( ( low > 0 ) as usize , hi)
@@ -653,7 +645,8 @@ pub type Coalesce<I, F> = CoalesceBy<I, F, <I as Iterator>::Item>;
653
645
pub struct CoalesceBy < I , F , T >
654
646
where I : Iterator
655
647
{
656
- iter : CoalesceCore < I , T > ,
648
+ iter : I ,
649
+ last : Option < T > ,
657
650
f : F ,
658
651
}
659
652
@@ -672,7 +665,7 @@ impl<F, Item, T> CoalescePredicate<Item, T> for F
672
665
impl < I : Clone , F : Clone , T : Clone > Clone for CoalesceBy < I , F , T >
673
666
where I : Iterator ,
674
667
{
675
- clone_fields ! ( iter, f) ;
668
+ clone_fields ! ( last , iter, f) ;
676
669
}
677
670
678
671
impl < I , F , T > fmt:: Debug for CoalesceBy < I , F , T >
@@ -687,10 +680,8 @@ pub fn coalesce<I, F>(mut iter: I, f: F) -> Coalesce<I, F>
687
680
where I : Iterator
688
681
{
689
682
Coalesce {
690
- iter : CoalesceCore {
691
- last : iter. next ( ) ,
692
- iter,
693
- } ,
683
+ last : iter. next ( ) ,
684
+ iter,
694
685
f,
695
686
}
696
687
}
@@ -702,20 +693,19 @@ impl<I, F, T> Iterator for CoalesceBy<I, F, T>
702
693
type Item = T ;
703
694
704
695
fn next ( & mut self ) -> Option < Self :: Item > {
705
- let f = & mut self . f ;
706
- self . iter . next_with ( |last, item| f. coalesce_pair ( last, item) )
696
+ self . next_with ( )
707
697
}
708
698
709
699
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
710
- self . iter . size_hint ( )
700
+ self . size_hint_internal ( )
711
701
}
712
702
713
703
fn fold < Acc , FnAcc > ( self , acc : Acc , mut fn_acc : FnAcc ) -> Acc
714
704
where FnAcc : FnMut ( Acc , Self :: Item ) -> Acc ,
715
705
{
716
- if let Some ( last) = self . iter . last {
706
+ if let Some ( last) = self . last {
717
707
let mut f = self . f ;
718
- let ( last, acc) = self . iter . iter . fold ( ( last, acc) , |( last, acc) , elt| {
708
+ let ( last, acc) = self . iter . fold ( ( last, acc) , |( last, acc) , elt| {
719
709
match f. coalesce_pair ( last, elt) {
720
710
Ok ( joined) => ( joined, acc) ,
721
711
Err ( ( last_, next_) ) => ( next_, fn_acc ( acc, last_) ) ,
@@ -778,10 +768,8 @@ pub fn dedup_by<I, Pred>(mut iter: I, dedup_pred: Pred) -> DedupBy<I, Pred>
778
768
where I : Iterator ,
779
769
{
780
770
DedupBy {
781
- iter : CoalesceCore {
782
- last : iter. next ( ) ,
783
- iter,
784
- } ,
771
+ last : iter. next ( ) ,
772
+ iter,
785
773
f : DedupPred2CoalescePred ( dedup_pred) ,
786
774
}
787
775
}
@@ -827,10 +815,8 @@ pub fn dedup_by_with_count<I, Pred>(mut iter: I, dedup_pred: Pred) -> DedupByWit
827
815
where I : Iterator ,
828
816
{
829
817
DedupByWithCount {
830
- iter : CoalesceCore {
831
- last : iter. next ( ) . map ( |v| ( 1 , v) ) ,
832
- iter,
833
- } ,
818
+ last : iter. next ( ) . map ( |v| ( 1 , v) ) ,
819
+ iter,
834
820
f : DedupPredWithCount2CoalescePred ( dedup_pred) ,
835
821
}
836
822
}
0 commit comments