@@ -1752,6 +1752,14 @@ where
1752
1752
}
1753
1753
}
1754
1754
1755
+ #[ stable( feature = "fused" , since = "1.26.0" ) ]
1756
+ impl < I , P > FusedIterator for TakeWhile < I , P >
1757
+ where
1758
+ I : FusedIterator ,
1759
+ P : FnMut ( & I :: Item ) -> bool ,
1760
+ {
1761
+ }
1762
+
1755
1763
/// An iterator that only accepts elements while `predicate` returns `Some(_)`.
1756
1764
///
1757
1765
/// This `struct` is created by the [`map_while`] method on [`Iterator`]. See its
@@ -1764,20 +1772,19 @@ where
1764
1772
#[ derive( Clone ) ]
1765
1773
pub struct MapWhile < I , P > {
1766
1774
iter : I ,
1767
- finished : bool ,
1768
1775
predicate : P ,
1769
1776
}
1770
1777
1771
1778
impl < I , P > MapWhile < I , P > {
1772
1779
pub ( super ) fn new ( iter : I , predicate : P ) -> MapWhile < I , P > {
1773
- MapWhile { iter, finished : false , predicate }
1780
+ MapWhile { iter, predicate }
1774
1781
}
1775
1782
}
1776
1783
1777
1784
#[ unstable( feature = "iter_map_while" , reason = "recently added" , issue = "68537" ) ]
1778
1785
impl < I : fmt:: Debug , P > fmt:: Debug for MapWhile < I , P > {
1779
1786
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1780
- f. debug_struct ( "MapWhile" ) . field ( "iter" , & self . iter ) . field ( "flag" , & self . finished ) . finish ( )
1787
+ f. debug_struct ( "MapWhile" ) . field ( "iter" , & self . iter ) . finish ( )
1781
1788
}
1782
1789
}
1783
1790
@@ -1790,65 +1797,32 @@ where
1790
1797
1791
1798
#[ inline]
1792
1799
fn next ( & mut self ) -> Option < B > {
1793
- if self . finished {
1794
- None
1795
- } else {
1796
- let x = self . iter . next ( ) ?;
1797
- let ret = ( self . predicate ) ( x) ;
1798
- self . finished = ret. is_none ( ) ;
1799
- ret
1800
- }
1800
+ let x = self . iter . next ( ) ?;
1801
+ ( self . predicate ) ( x)
1801
1802
}
1802
1803
1803
1804
#[ inline]
1804
1805
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
1805
- if self . finished {
1806
- ( 0 , Some ( 0 ) )
1807
- } else {
1808
- let ( _, upper) = self . iter . size_hint ( ) ;
1809
- ( 0 , upper) // can't know a lower bound, due to the predicate
1810
- }
1806
+ let ( _, upper) = self . iter . size_hint ( ) ;
1807
+ ( 0 , upper) // can't know a lower bound, due to the predicate
1811
1808
}
1812
1809
1813
1810
#[ inline]
1814
- fn try_fold < Acc , Fold , R > ( & mut self , init : Acc , fold : Fold ) -> R
1811
+ fn try_fold < Acc , Fold , R > ( & mut self , init : Acc , mut fold : Fold ) -> R
1815
1812
where
1816
1813
Self : Sized ,
1817
1814
Fold : FnMut ( Acc , Self :: Item ) -> R ,
1818
1815
R : Try < Ok = Acc > ,
1819
1816
{
1820
- fn check < ' a , B , T , Acc , R : Try < Ok = Acc > > (
1821
- flag : & ' a mut bool ,
1822
- p : & ' a mut impl FnMut ( T ) -> Option < B > ,
1823
- mut fold : impl FnMut ( Acc , B ) -> R + ' a ,
1824
- ) -> impl FnMut ( Acc , T ) -> LoopState < Acc , R > + ' a {
1825
- move |acc, x| match p ( x) {
1826
- Some ( item) => LoopState :: from_try ( fold ( acc, item) ) ,
1827
- None => {
1828
- * flag = true ;
1829
- LoopState :: Break ( Try :: from_ok ( acc) )
1830
- }
1831
- }
1832
- }
1833
-
1834
- if self . finished {
1835
- Try :: from_ok ( init)
1836
- } else {
1837
- let flag = & mut self . finished ;
1838
- let p = & mut self . predicate ;
1839
- self . iter . try_fold ( init, check ( flag, p, fold) ) . into_try ( )
1840
- }
1817
+ let Self { iter, predicate } = self ;
1818
+ iter. try_fold ( init, |acc, x| match predicate ( x) {
1819
+ Some ( item) => LoopState :: from_try ( fold ( acc, item) ) ,
1820
+ None => LoopState :: Break ( Try :: from_ok ( acc) ) ,
1821
+ } )
1822
+ . into_try ( )
1841
1823
}
1842
1824
}
1843
1825
1844
- #[ stable( feature = "fused" , since = "1.26.0" ) ]
1845
- impl < I , P > FusedIterator for TakeWhile < I , P >
1846
- where
1847
- I : FusedIterator ,
1848
- P : FnMut ( & I :: Item ) -> bool ,
1849
- {
1850
- }
1851
-
1852
1826
/// An iterator that skips over `n` elements of `iter`.
1853
1827
///
1854
1828
/// This `struct` is created by the [`skip`] method on [`Iterator`]. See its
0 commit comments