@@ -594,6 +594,20 @@ impl<I: fmt::Debug, F> fmt::Debug for Map<I, F> {
594
594
}
595
595
}
596
596
597
+ fn map_fold < T , B , Acc > (
598
+ mut f : impl FnMut ( T ) -> B ,
599
+ mut g : impl FnMut ( Acc , B ) -> Acc ,
600
+ ) -> impl FnMut ( Acc , T ) -> Acc {
601
+ move |acc, elt| g ( acc, f ( elt) )
602
+ }
603
+
604
+ fn map_try_fold < ' a , T , B , Acc , R > (
605
+ f : & ' a mut impl FnMut ( T ) -> B ,
606
+ mut g : impl FnMut ( Acc , B ) -> R + ' a ,
607
+ ) -> impl FnMut ( Acc , T ) -> R + ' a {
608
+ move |acc, elt| g ( acc, f ( elt) )
609
+ }
610
+
597
611
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
598
612
impl < B , I : Iterator , F > Iterator for Map < I , F > where F : FnMut ( I :: Item ) -> B {
599
613
type Item = B ;
@@ -608,18 +622,16 @@ impl<B, I: Iterator, F> Iterator for Map<I, F> where F: FnMut(I::Item) -> B {
608
622
self . iter . size_hint ( )
609
623
}
610
624
611
- fn try_fold < Acc , G , R > ( & mut self , init : Acc , mut g : G ) -> R where
625
+ fn try_fold < Acc , G , R > ( & mut self , init : Acc , g : G ) -> R where
612
626
Self : Sized , G : FnMut ( Acc , Self :: Item ) -> R , R : Try < Ok =Acc >
613
627
{
614
- let f = & mut self . f ;
615
- self . iter . try_fold ( init, move |acc, elt| g ( acc, f ( elt) ) )
628
+ self . iter . try_fold ( init, map_try_fold ( & mut self . f , g) )
616
629
}
617
630
618
- fn fold < Acc , G > ( self , init : Acc , mut g : G ) -> Acc
631
+ fn fold < Acc , G > ( self , init : Acc , g : G ) -> Acc
619
632
where G : FnMut ( Acc , Self :: Item ) -> Acc ,
620
633
{
621
- let mut f = self . f ;
622
- self . iter . fold ( init, move |acc, elt| g ( acc, f ( elt) ) )
634
+ self . iter . fold ( init, map_fold ( self . f , g) )
623
635
}
624
636
}
625
637
@@ -632,18 +644,16 @@ impl<B, I: DoubleEndedIterator, F> DoubleEndedIterator for Map<I, F> where
632
644
self . iter . next_back ( ) . map ( & mut self . f )
633
645
}
634
646
635
- fn try_rfold < Acc , G , R > ( & mut self , init : Acc , mut g : G ) -> R where
647
+ fn try_rfold < Acc , G , R > ( & mut self , init : Acc , g : G ) -> R where
636
648
Self : Sized , G : FnMut ( Acc , Self :: Item ) -> R , R : Try < Ok =Acc >
637
649
{
638
- let f = & mut self . f ;
639
- self . iter . try_rfold ( init, move |acc, elt| g ( acc, f ( elt) ) )
650
+ self . iter . try_rfold ( init, map_try_fold ( & mut self . f , g) )
640
651
}
641
652
642
- fn rfold < Acc , G > ( self , init : Acc , mut g : G ) -> Acc
653
+ fn rfold < Acc , G > ( self , init : Acc , g : G ) -> Acc
643
654
where G : FnMut ( Acc , Self :: Item ) -> Acc ,
644
655
{
645
- let mut f = self . f ;
646
- self . iter . rfold ( init, move |acc, elt| g ( acc, f ( elt) ) )
656
+ self . iter . rfold ( init, map_fold ( self . f , g) )
647
657
}
648
658
}
649
659
0 commit comments