@@ -605,27 +605,18 @@ 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 >
617
- where I : Iterator
608
+ impl < I , F , T > Coalesce < I , F , T >
609
+ where I : Iterator ,
610
+ F : CoalescePredicate < I :: Item , T > ,
618
611
{
619
- fn next_with < F > ( & mut self , mut f : F ) -> Option < T >
620
- where F : FnMut ( T , I :: Item ) -> Result < T , ( T , T ) >
621
- {
612
+ fn next_with ( & mut self ) -> Option < T > {
622
613
// this fuses the iterator
623
614
let mut last = match self . last . take ( ) {
624
615
None => return None ,
625
616
Some ( x) => x,
626
617
} ;
627
618
for next in & mut self . iter {
628
- match f ( last, next) {
619
+ match self . f . coalesce_pair ( last, next) {
629
620
Ok ( joined) => last = joined,
630
621
Err ( ( last_, next_) ) => {
631
622
self . last = Some ( next_) ;
@@ -637,11 +628,6 @@ impl<I, T> CoalesceCore<I, T>
637
628
Some ( last)
638
629
}
639
630
640
- fn size_hint ( & self ) -> ( usize , Option < usize > ) {
641
- let ( low, hi) = size_hint:: add_scalar ( self . iter . size_hint ( ) ,
642
- self . last . is_some ( ) as usize ) ;
643
- ( ( low > 0 ) as usize , hi)
644
- }
645
631
}
646
632
647
633
/// An iterator adaptor that may join together adjacent elements.
@@ -651,7 +637,8 @@ impl<I, T> CoalesceCore<I, T>
651
637
pub struct Coalesce < I , F , T >
652
638
where I : Iterator
653
639
{
654
- iter : CoalesceCore < I , T > ,
640
+ iter : I ,
641
+ last : Option < T > ,
655
642
f : F ,
656
643
}
657
644
@@ -670,25 +657,23 @@ impl<F, Item, T> CoalescePredicate<Item, T> for F
670
657
impl < I : Clone , F : Clone , T : Clone > Clone for Coalesce < I , F , T >
671
658
where I : Iterator ,
672
659
{
673
- clone_fields ! ( iter, f) ;
660
+ clone_fields ! ( iter, last , f) ;
674
661
}
675
662
676
663
impl < I , F , T > fmt:: Debug for Coalesce < I , F , T >
677
664
where I : Iterator + fmt:: Debug ,
678
665
T : fmt:: Debug ,
679
666
{
680
- debug_fmt_fields ! ( Coalesce , iter) ;
667
+ debug_fmt_fields ! ( Coalesce , iter, last ) ;
681
668
}
682
669
683
670
/// Create a new `Coalesce`.
684
671
pub fn coalesce < I , F > ( mut iter : I , f : F ) -> Coalesce < I , F , I :: Item >
685
672
where I : Iterator
686
673
{
687
674
Coalesce {
688
- iter : CoalesceCore {
689
- last : iter. next ( ) ,
690
- iter,
691
- } ,
675
+ last : iter. next ( ) ,
676
+ iter,
692
677
f,
693
678
}
694
679
}
@@ -700,20 +685,21 @@ impl<I, F, T> Iterator for Coalesce<I, F, T>
700
685
type Item = T ;
701
686
702
687
fn next ( & mut self ) -> Option < Self :: Item > {
703
- let f = & mut self . f ;
704
- self . iter . next_with ( |last, item| f. coalesce_pair ( last, item) )
688
+ self . next_with ( )
705
689
}
706
690
707
691
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
708
- self . iter . size_hint ( )
692
+ let ( low, hi) = size_hint:: add_scalar ( self . iter . size_hint ( ) ,
693
+ self . last . is_some ( ) as usize ) ;
694
+ ( ( low > 0 ) as usize , hi)
709
695
}
710
696
711
697
fn fold < Acc , FnAcc > ( self , acc : Acc , mut fn_acc : FnAcc ) -> Acc
712
698
where FnAcc : FnMut ( Acc , Self :: Item ) -> Acc ,
713
699
{
714
- if let Some ( last) = self . iter . last {
700
+ if let Some ( last) = self . last {
715
701
let mut f = self . f ;
716
- let ( last, acc) = self . iter . iter . fold ( ( last, acc) , |( last, acc) , elt| {
702
+ let ( last, acc) = self . iter . fold ( ( last, acc) , |( last, acc) , elt| {
717
703
match f. coalesce_pair ( last, elt) {
718
704
Ok ( joined) => ( joined, acc) ,
719
705
Err ( ( last_, next_) ) => ( next_, fn_acc ( acc, last_) ) ,
@@ -776,10 +762,8 @@ pub fn dedup_by<I, Pred>(mut iter: I, dedup_pred: Pred) -> DedupBy<I, Pred>
776
762
where I : Iterator ,
777
763
{
778
764
Coalesce {
779
- iter : CoalesceCore {
780
- last : iter. next ( ) ,
781
- iter,
782
- } ,
765
+ last : iter. next ( ) ,
766
+ iter,
783
767
f : DedupPred2CoalescePred ( dedup_pred) ,
784
768
}
785
769
}
0 commit comments