@@ -819,32 +819,64 @@ impl<Idx: Step + Copy> IntoIterator for &ops::range::Range<Idx> {
819819 }
820820}
821821
822+ /// Mutating iterator for `ops::Range`.
822823#[ stable( feature = "new_range" , since = "1.0.0" ) ]
823- impl < Idx : Step + Copy > IntoIterator for & mut ops:: range:: Range < Idx > {
824+ #[ derive( Debug ) ]
825+ pub struct RangeIterMut < ' a , Idx > {
826+ range : & ' a mut ops:: range:: Range < Idx > ,
827+ }
828+
829+ #[ stable( feature = "new_range" , since = "1.0.0" ) ]
830+ impl < ' a , Idx : Step > Iterator for RangeIterMut < ' a , Idx > {
824831 type Item = Idx ;
825- type IntoIter = RangeIter < Idx > ;
826832
827- fn into_iter ( self ) -> Self :: IntoIter {
828- ( * self ) . into_iter ( )
833+ fn next ( & mut self ) -> Option < Self :: Item > {
834+ let mut iter = self . range . clone ( ) . into_iter ( ) ;
835+ let out = iter. next ( ) ;
836+
837+ self . range . start = iter. inner . start ;
838+ self . range . end = iter. inner . end ;
839+
840+ out
841+ }
842+ }
843+ #[ stable( feature = "new_range" , since = "1.0.0" ) ]
844+ impl < ' a , Idx : Step > DoubleEndedIterator for RangeIterMut < ' a , Idx > {
845+ fn next_back ( & mut self ) -> Option < Self :: Item > {
846+ let mut iter = self . range . clone ( ) . into_iter ( ) ;
847+ let out = iter. next_back ( ) ;
848+
849+ self . range . start = iter. inner . start ;
850+ self . range . end = iter. inner . end ;
851+
852+ out
829853 }
830854}
831855
832856impl < Idx : Step > ops:: range:: Range < Idx > {
833- /// Returns and advances `start` unless the range is empty.
857+ /// Returns an iterator which mutates this range in place,
858+ /// rather than taking the range by value.
859+ #[ stable( feature = "new_range" , since = "1.0.0" ) ]
860+ pub fn iter_mut ( & mut self ) -> RangeIterMut < ' _ , Idx > {
861+ RangeIterMut { range : self }
862+ }
863+
864+ /// Shorthand for `.iter_mut().next_back()`
834865 ///
835- /// This differs from `.into_iter().next()` because
836- /// that copies the range before advancing the iterator
837- /// but this modifies the range in place.
866+ /// See [`DoubleEndedIterator::next_back`]
838867 #[ stable( feature = "new_range" , since = "1.0.0" ) ]
839- # [ deprecated ( since = "1.0.0" , note = "can cause subtle bugs" ) ]
840- pub fn next ( & mut self ) -> Option < Idx > {
841- let mut iter = self . clone ( ) . into_iter ( ) ;
842- let out = iter . next ( ) ;
868+ pub fn next_back ( & mut self ) -> Option < Idx > {
869+ self . iter_mut ( ) . next_back ( )
870+ }
871+ }
843872
844- self . start = iter. inner . start ;
845- self . end = iter. inner . end ;
873+ #[ stable( feature = "new_range" , since = "1.0.0" ) ]
874+ impl < ' a , Idx : Step > IntoIterator for & ' a mut ops:: range:: Range < Idx > {
875+ type Item = Idx ;
876+ type IntoIter = RangeIterMut < ' a , Idx > ;
846877
847- out
878+ fn into_iter ( self ) -> Self :: IntoIter {
879+ self . iter_mut ( )
848880 }
849881}
850882
@@ -910,31 +942,43 @@ impl<A: Step + Copy> IntoIterator for &ops::range::RangeFrom<A> {
910942 }
911943}
912944
945+ /// Mutating iterator for `ops::RangeFrom`.
913946#[ stable( feature = "new_range" , since = "1.0.0" ) ]
914- impl < A : Step + Copy > IntoIterator for & mut ops:: range:: RangeFrom < A > {
915- type Item = A ;
916- type IntoIter = RangeFromIter < A > ;
947+ #[ derive( Debug ) ]
948+ pub struct RangeFromIterMut < ' a , Idx > {
949+ range : & ' a mut ops:: range:: RangeFrom < Idx > ,
950+ }
917951
918- fn into_iter ( self ) -> Self :: IntoIter {
919- ( * self ) . into_iter ( )
952+ #[ stable( feature = "new_range" , since = "1.0.0" ) ]
953+ impl < ' a , Idx : Step > Iterator for RangeFromIterMut < ' a , Idx > {
954+ type Item = Idx ;
955+
956+ fn next ( & mut self ) -> Option < Self :: Item > {
957+ let mut iter = self . range . clone ( ) . into_iter ( ) ;
958+ let out = iter. next ( ) ;
959+
960+ self . range . start = iter. inner . start ;
961+
962+ out
920963 }
921964}
922965
923966impl < Idx : Step > ops:: range:: RangeFrom < Idx > {
924- /// Returns and advances `start` unless the range is empty.
925- ///
926- /// This differs from `.into_iter().next()` because
927- /// that copies the range before advancing the iterator
928- /// but this modifies the range in place.
967+ /// Returns an iterator which mutates this range in place,
968+ /// rather than taking the range by value.
929969 #[ stable( feature = "new_range" , since = "1.0.0" ) ]
930- # [ deprecated ( since = "1.0.0" , note = "can cause subtle bugs" ) ]
931- pub fn next ( & mut self ) -> Option < Idx > {
932- let mut iter = self . clone ( ) . into_iter ( ) ;
933- let out = iter . next ( ) ;
970+ pub fn iter_mut ( & mut self ) -> RangeFromIterMut < ' _ , Idx > {
971+ RangeFromIterMut { range : self }
972+ }
973+ }
934974
935- self . start = iter. inner . start ;
975+ #[ stable( feature = "new_range" , since = "1.0.0" ) ]
976+ impl < ' a , Idx : Step > IntoIterator for & ' a mut ops:: range:: RangeFrom < Idx > {
977+ type Item = Idx ;
978+ type IntoIter = RangeFromIterMut < ' a , Idx > ;
936979
937- out
980+ fn into_iter ( self ) -> Self :: IntoIter {
981+ self . iter_mut ( )
938982 }
939983}
940984
@@ -1086,52 +1130,106 @@ impl<A: Step + Copy> IntoIterator for &ops::range::RangeInclusive<A> {
10861130 }
10871131}
10881132
1133+ /// Mutating iterator for `ops::RangeInclusive`.
10891134#[ stable( feature = "new_range" , since = "1.0.0" ) ]
1090- impl < A : Step + Copy > IntoIterator for & mut ops:: range:: RangeInclusive < A > {
1091- type Item = A ;
1092- type IntoIter = RangeInclusiveIter < A > ;
1093-
1094- fn into_iter ( self ) -> Self :: IntoIter {
1095- ( * self ) . into_iter ( )
1096- }
1135+ #[ derive( Debug ) ]
1136+ pub struct RangeInclusiveIterMut < ' a , Idx > {
1137+ range : & ' a mut ops:: range:: RangeInclusive < Idx > ,
10971138}
10981139
1099- impl < Idx : Step > ops:: range:: RangeInclusive < Idx > {
1100- /// Returns and advances `start` unless the range is empty.
1101- ///
1102- /// This differs from `.into_iter().next()` because
1103- /// that copies the range before advancing the iterator
1104- /// but this modifies the range in place.
1105- #[ stable( feature = "new_range" , since = "1.0.0" ) ]
1106- #[ deprecated( since = "1.0.0" , note = "can cause subtle bugs" ) ]
1107- pub fn next ( & mut self ) -> Option < Idx > {
1108- let mut iter = self . clone ( ) . into_iter ( ) ;
1140+ #[ stable( feature = "new_range" , since = "1.0.0" ) ]
1141+ impl < ' a , Idx : Step > Iterator for RangeInclusiveIterMut < ' a , Idx > {
1142+ type Item = Idx ;
1143+
1144+ fn next ( & mut self ) -> Option < Self :: Item > {
1145+ let mut iter = self . range . clone ( ) . into_iter ( ) ;
11091146 let out = iter. next ( ) ;
11101147
11111148 if iter. inner . exhausted {
11121149 // When exhausted, attempt to put end before start so the range is empty
11131150 // If end is the minimum value (`start = end = 0`), set start past end
11141151 if let Some ( n) = Step :: backward_checked ( iter. inner . start . clone ( ) , 1 ) {
1115- self . end = n;
1116- self . start = iter. inner . start ;
1152+ self . range . end = n;
1153+ self . range . start = iter. inner . start ;
11171154 } else {
1118- self . start = Step :: forward ( iter. inner . end . clone ( ) , 1 ) ;
1119- self . end = iter. inner . end ;
1155+ self . range . start = Step :: forward ( iter. inner . end . clone ( ) , 1 ) ;
1156+ self . range . end = iter. inner . end ;
11201157 }
11211158 } else {
11221159 // Not exhausted, so just set new start and end
1123- self . start = iter. inner . start ;
1124- self . end = iter. inner . end ;
1160+ self . range . start = iter. inner . start ;
1161+ self . range . end = iter. inner . end ;
11251162 }
11261163
11271164 out
11281165 }
11291166}
1167+ #[ stable( feature = "new_range" , since = "1.0.0" ) ]
1168+ impl < ' a , Idx : Step > DoubleEndedIterator for RangeInclusiveIterMut < ' a , Idx > {
1169+ fn next_back ( & mut self ) -> Option < Self :: Item > {
1170+ let mut iter = self . range . clone ( ) . into_iter ( ) ;
1171+ let out = iter. next_back ( ) ;
1172+
1173+ if iter. inner . exhausted {
1174+ // When exhausted, attempt to put end before start so the range is empty
1175+ // If end is the minimum value (`start = end = 0`), set start past end
1176+ if let Some ( n) = Step :: backward_checked ( iter. inner . start . clone ( ) , 1 ) {
1177+ self . range . end = n;
1178+ self . range . start = iter. inner . start ;
1179+ } else {
1180+ self . range . start = Step :: forward ( iter. inner . end . clone ( ) , 1 ) ;
1181+ self . range . end = iter. inner . end ;
1182+ }
1183+ } else {
1184+ // Not exhausted, so just set new start and end
1185+ self . range . start = iter. inner . start ;
1186+ self . range . end = iter. inner . end ;
1187+ }
1188+
1189+ out
1190+ }
1191+ }
1192+
1193+ impl < Idx : Step > ops:: range:: RangeInclusive < Idx > {
1194+ /// Returns an iterator which mutates this range in place,
1195+ /// rather than taking the range by value.
1196+ #[ stable( feature = "new_range" , since = "1.0.0" ) ]
1197+ pub fn iter_mut ( & mut self ) -> RangeInclusiveIterMut < ' _ , Idx > {
1198+ RangeInclusiveIterMut { range : self }
1199+ }
1200+
1201+ /// Shorthand for `.iter_mut().next_back()`
1202+ ///
1203+ /// See [`DoubleEndedIterator::next_back`]
1204+ #[ stable( feature = "new_range" , since = "1.0.0" ) ]
1205+ pub fn next_back ( & mut self ) -> Option < Idx > {
1206+ self . iter_mut ( ) . next_back ( )
1207+ }
1208+ }
1209+
1210+ #[ stable( feature = "new_range" , since = "1.0.0" ) ]
1211+ impl < ' a , Idx : Step > IntoIterator for & ' a mut ops:: range:: RangeInclusive < Idx > {
1212+ type Item = Idx ;
1213+ type IntoIter = RangeInclusiveIterMut < ' a , Idx > ;
1214+
1215+ fn into_iter ( self ) -> Self :: IntoIter {
1216+ self . iter_mut ( )
1217+ }
1218+ }
11301219
11311220macro_rules! iter_methods {
11321221 ( $( $ty: ident) ,* ) => { $(
11331222
11341223impl <Idx : Step > ops:: range:: $ty<Idx > {
1224+ /// Shorthand for `.iter_mut().next()`.
1225+ ///
1226+ /// See [`Iterator::next`]
1227+ #[ stable( feature = "new_range" , since = "1.0.0" ) ]
1228+ #[ deprecated( since = "1.0.0" , note = "can cause subtle bugs" ) ]
1229+ pub fn next( & mut self ) -> Option <Idx > {
1230+ self . iter_mut( ) . next( )
1231+ }
1232+
11351233 /// Shorthand for `.into_iter().size_hint()`.
11361234 ///
11371235 /// See [`Iterator::size_hint`]
@@ -1164,6 +1262,14 @@ impl<Idx: Step> ops::range::$ty<Idx> {
11641262 self . into_iter( ) . step_by( step)
11651263 }
11661264
1265+ /// Shorthand for `.iter_mut().nth(...)`.
1266+ ///
1267+ /// See [`Iterator::nth`]
1268+ #[ stable( feature = "new_range" , since = "1.0.0" ) ]
1269+ pub fn nth( & mut self , n: usize ) -> Option <Idx > {
1270+ self . iter_mut( ) . nth( n)
1271+ }
1272+
11671273 /// Shorthand for `.into_iter().chain(...)`
11681274 ///
11691275 /// See [`Iterator::chain`]
@@ -1379,6 +1485,18 @@ impl<Idx: Step> ops::range::$ty<Idx> {
13791485 self . into_iter( ) . partition( f)
13801486 }
13811487
1488+ /// Shorthand for `.into_iter().try_fold(...)`
1489+ ///
1490+ /// See [`Iterator::try_fold`]
1491+ #[ stable( feature = "new_range" , since = "1.0.0" ) ]
1492+ pub fn try_fold<B , F , R >( & mut self , init: B , f: F ) -> R
1493+ where
1494+ F : FnMut ( B , Idx ) -> R ,
1495+ R : Try <Output = B >,
1496+ {
1497+ self . iter_mut( ) . try_fold( init, f)
1498+ }
1499+
13821500 /// Shorthand for `.into_iter().fold(...)`
13831501 ///
13841502 /// See [`Iterator::fold`]
@@ -1401,49 +1519,40 @@ impl<Idx: Step> ops::range::$ty<Idx> {
14011519 self . into_iter( ) . reduce( f)
14021520 }
14031521
1404- /// Shorthand for `.into_iter().all(...)`
1405- ///
1406- /// One noticeable difference is that this takes the
1407- /// range by copy, rather than mutating it in place.
1522+ /// Shorthand for `.iter_mut().all(...)`
14081523 ///
14091524 /// See [`Iterator::all`]
14101525 #[ stable( feature = "new_range" , since = "1.0.0" ) ]
14111526 #[ deprecated( since = "1.0.0" , note = "can cause subtle bugs" ) ]
1412- pub fn all<F >( self , f: F ) -> bool
1527+ pub fn all<F >( & mut self , f: F ) -> bool
14131528 where
14141529 F : FnMut ( Idx ) -> bool ,
14151530 {
1416- self . into_iter ( ) . all( f)
1531+ self . iter_mut ( ) . all( f)
14171532 }
14181533
1419- /// Shorthand for `.into_iter().any(...)`
1420- ///
1421- /// One noticeable difference is that this takes the
1422- /// range by copy, rather than mutating it in place.
1534+ /// Shorthand for `.iter_mut().any(...)`
14231535 ///
14241536 /// See [`Iterator::any`]
14251537 #[ stable( feature = "new_range" , since = "1.0.0" ) ]
14261538 #[ deprecated( since = "1.0.0" , note = "can cause subtle bugs" ) ]
1427- pub fn any<F >( self , f: F ) -> bool
1539+ pub fn any<F >( & mut self , f: F ) -> bool
14281540 where
14291541 F : FnMut ( Idx ) -> bool ,
14301542 {
1431- self . into_iter ( ) . any( f)
1543+ self . iter_mut ( ) . any( f)
14321544 }
14331545
1434- /// Shorthand for `.into_iter().find(...)`
1435- ///
1436- /// One noticeable difference is that this takes the
1437- /// range by copy, rather than mutating it in place.
1546+ /// Shorthand for `.iter_mut().find(...)`
14381547 ///
14391548 /// See [`Iterator::find`]
14401549 #[ stable( feature = "new_range" , since = "1.0.0" ) ]
14411550 #[ deprecated( since = "1.0.0" , note = "can cause subtle bugs" ) ]
1442- pub fn find<P >( self , predicate: P ) -> Option <Idx >
1551+ pub fn find<P >( & mut self , predicate: P ) -> Option <Idx >
14431552 where
14441553 P : FnMut ( & Idx ) -> bool ,
14451554 {
1446- self . into_iter ( ) . find( predicate)
1555+ self . iter_mut ( ) . find( predicate)
14471556 }
14481557
14491558 /// Shorthand for `.into_iter().max()`
0 commit comments