@@ -1768,6 +1768,14 @@ where
17681768 }
17691769}
17701770
1771+ #[ stable( feature = "fused" , since = "1.26.0" ) ]
1772+ impl < I , P > FusedIterator for TakeWhile < I , P >
1773+ where
1774+ I : FusedIterator ,
1775+ P : FnMut ( & I :: Item ) -> bool ,
1776+ {
1777+ }
1778+
17711779/// An iterator that only accepts elements while `predicate` returns `Some(_)`.
17721780///
17731781/// This `struct` is created by the [`map_while`] method on [`Iterator`]. See its
@@ -1780,20 +1788,19 @@ where
17801788#[ derive( Clone ) ]
17811789pub struct MapWhile < I , P > {
17821790 iter : I ,
1783- finished : bool ,
17841791 predicate : P ,
17851792}
17861793
17871794impl < I , P > MapWhile < I , P > {
17881795 pub ( super ) fn new ( iter : I , predicate : P ) -> MapWhile < I , P > {
1789- MapWhile { iter, finished : false , predicate }
1796+ MapWhile { iter, predicate }
17901797 }
17911798}
17921799
17931800#[ unstable( feature = "iter_map_while" , reason = "recently added" , issue = "68537" ) ]
17941801impl < I : fmt:: Debug , P > fmt:: Debug for MapWhile < I , P > {
17951802 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1796- f. debug_struct ( "MapWhile" ) . field ( "iter" , & self . iter ) . field ( "flag" , & self . finished ) . finish ( )
1803+ f. debug_struct ( "MapWhile" ) . field ( "iter" , & self . iter ) . finish ( )
17971804 }
17981805}
17991806
@@ -1806,65 +1813,32 @@ where
18061813
18071814 #[ inline]
18081815 fn next ( & mut self ) -> Option < B > {
1809- if self . finished {
1810- None
1811- } else {
1812- let x = self . iter . next ( ) ?;
1813- let ret = ( self . predicate ) ( x) ;
1814- self . finished = ret. is_none ( ) ;
1815- ret
1816- }
1816+ let x = self . iter . next ( ) ?;
1817+ ( self . predicate ) ( x)
18171818 }
18181819
18191820 #[ inline]
18201821 fn size_hint ( & self ) -> ( usize , Option < usize > ) {
1821- if self . finished {
1822- ( 0 , Some ( 0 ) )
1823- } else {
1824- let ( _, upper) = self . iter . size_hint ( ) ;
1825- ( 0 , upper) // can't know a lower bound, due to the predicate
1826- }
1822+ let ( _, upper) = self . iter . size_hint ( ) ;
1823+ ( 0 , upper) // can't know a lower bound, due to the predicate
18271824 }
18281825
18291826 #[ inline]
1830- fn try_fold < Acc , Fold , R > ( & mut self , init : Acc , fold : Fold ) -> R
1827+ fn try_fold < Acc , Fold , R > ( & mut self , init : Acc , mut fold : Fold ) -> R
18311828 where
18321829 Self : Sized ,
18331830 Fold : FnMut ( Acc , Self :: Item ) -> R ,
18341831 R : Try < Ok = Acc > ,
18351832 {
1836- fn check < ' a , B , T , Acc , R : Try < Ok = Acc > > (
1837- flag : & ' a mut bool ,
1838- p : & ' a mut impl FnMut ( T ) -> Option < B > ,
1839- mut fold : impl FnMut ( Acc , B ) -> R + ' a ,
1840- ) -> impl FnMut ( Acc , T ) -> LoopState < Acc , R > + ' a {
1841- move |acc, x| match p ( x) {
1842- Some ( item) => LoopState :: from_try ( fold ( acc, item) ) ,
1843- None => {
1844- * flag = true ;
1845- LoopState :: Break ( Try :: from_ok ( acc) )
1846- }
1847- }
1848- }
1849-
1850- if self . finished {
1851- Try :: from_ok ( init)
1852- } else {
1853- let flag = & mut self . finished ;
1854- let p = & mut self . predicate ;
1855- self . iter . try_fold ( init, check ( flag, p, fold) ) . into_try ( )
1856- }
1833+ let Self { iter, predicate } = self ;
1834+ iter. try_fold ( init, |acc, x| match predicate ( x) {
1835+ Some ( item) => LoopState :: from_try ( fold ( acc, item) ) ,
1836+ None => LoopState :: Break ( Try :: from_ok ( acc) ) ,
1837+ } )
1838+ . into_try ( )
18571839 }
18581840}
18591841
1860- #[ stable( feature = "fused" , since = "1.26.0" ) ]
1861- impl < I , P > FusedIterator for TakeWhile < I , P >
1862- where
1863- I : FusedIterator ,
1864- P : FnMut ( & I :: Item ) -> bool ,
1865- {
1866- }
1867-
18681842/// An iterator that skips over `n` elements of `iter`.
18691843///
18701844/// This `struct` is created by the [`skip`] method on [`Iterator`]. See its
0 commit comments