@@ -1768,6 +1768,14 @@ where
1768
1768
}
1769
1769
}
1770
1770
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
+
1771
1779
/// An iterator that only accepts elements while `predicate` returns `Some(_)`.
1772
1780
///
1773
1781
/// This `struct` is created by the [`map_while`] method on [`Iterator`]. See its
@@ -1780,20 +1788,19 @@ where
1780
1788
#[ derive( Clone ) ]
1781
1789
pub struct MapWhile < I , P > {
1782
1790
iter : I ,
1783
- finished : bool ,
1784
1791
predicate : P ,
1785
1792
}
1786
1793
1787
1794
impl < I , P > MapWhile < I , P > {
1788
1795
pub ( super ) fn new ( iter : I , predicate : P ) -> MapWhile < I , P > {
1789
- MapWhile { iter, finished : false , predicate }
1796
+ MapWhile { iter, predicate }
1790
1797
}
1791
1798
}
1792
1799
1793
1800
#[ unstable( feature = "iter_map_while" , reason = "recently added" , issue = "68537" ) ]
1794
1801
impl < I : fmt:: Debug , P > fmt:: Debug for MapWhile < I , P > {
1795
1802
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 ( )
1797
1804
}
1798
1805
}
1799
1806
@@ -1806,65 +1813,32 @@ where
1806
1813
1807
1814
#[ inline]
1808
1815
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)
1817
1818
}
1818
1819
1819
1820
#[ inline]
1820
1821
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
1827
1824
}
1828
1825
1829
1826
#[ 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
1831
1828
where
1832
1829
Self : Sized ,
1833
1830
Fold : FnMut ( Acc , Self :: Item ) -> R ,
1834
1831
R : Try < Ok = Acc > ,
1835
1832
{
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 ( )
1857
1839
}
1858
1840
}
1859
1841
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
-
1868
1842
/// An iterator that skips over `n` elements of `iter`.
1869
1843
///
1870
1844
/// This `struct` is created by the [`skip`] method on [`Iterator`]. See its
0 commit comments