@@ -819,32 +819,64 @@ impl<Idx: Step + Copy> IntoIterator for &ops::range::Range<Idx> {
819
819
}
820
820
}
821
821
822
+ /// Mutating iterator for `ops::Range`.
822
823
#[ 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 > {
824
831
type Item = Idx ;
825
- type IntoIter = RangeIter < Idx > ;
826
832
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
829
853
}
830
854
}
831
855
832
856
impl < 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()`
834
865
///
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`]
838
867
#[ 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
+ }
843
872
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 > ;
846
877
847
- out
878
+ fn into_iter ( self ) -> Self :: IntoIter {
879
+ self . iter_mut ( )
848
880
}
849
881
}
850
882
@@ -910,31 +942,43 @@ impl<A: Step + Copy> IntoIterator for &ops::range::RangeFrom<A> {
910
942
}
911
943
}
912
944
945
+ /// Mutating iterator for `ops::RangeFrom`.
913
946
#[ 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
+ }
917
951
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
920
963
}
921
964
}
922
965
923
966
impl < 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.
929
969
#[ 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
+ }
934
974
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 > ;
936
979
937
- out
980
+ fn into_iter ( self ) -> Self :: IntoIter {
981
+ self . iter_mut ( )
938
982
}
939
983
}
940
984
@@ -1086,52 +1130,106 @@ impl<A: Step + Copy> IntoIterator for &ops::range::RangeInclusive<A> {
1086
1130
}
1087
1131
}
1088
1132
1133
+ /// Mutating iterator for `ops::RangeInclusive`.
1089
1134
#[ 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 > ,
1097
1138
}
1098
1139
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 ( ) ;
1109
1146
let out = iter. next ( ) ;
1110
1147
1111
1148
if iter. inner . exhausted {
1112
1149
// When exhausted, attempt to put end before start so the range is empty
1113
1150
// If end is the minimum value (`start = end = 0`), set start past end
1114
1151
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 ;
1117
1154
} 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 ;
1120
1157
}
1121
1158
} else {
1122
1159
// 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 ;
1125
1162
}
1126
1163
1127
1164
out
1128
1165
}
1129
1166
}
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
+ }
1130
1219
1131
1220
macro_rules! iter_methods {
1132
1221
( $( $ty: ident) ,* ) => { $(
1133
1222
1134
1223
impl <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
+
1135
1233
/// Shorthand for `.into_iter().size_hint()`.
1136
1234
///
1137
1235
/// See [`Iterator::size_hint`]
@@ -1164,6 +1262,14 @@ impl<Idx: Step> ops::range::$ty<Idx> {
1164
1262
self . into_iter( ) . step_by( step)
1165
1263
}
1166
1264
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
+
1167
1273
/// Shorthand for `.into_iter().chain(...)`
1168
1274
///
1169
1275
/// See [`Iterator::chain`]
@@ -1379,6 +1485,18 @@ impl<Idx: Step> ops::range::$ty<Idx> {
1379
1485
self . into_iter( ) . partition( f)
1380
1486
}
1381
1487
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
+
1382
1500
/// Shorthand for `.into_iter().fold(...)`
1383
1501
///
1384
1502
/// See [`Iterator::fold`]
@@ -1401,49 +1519,40 @@ impl<Idx: Step> ops::range::$ty<Idx> {
1401
1519
self . into_iter( ) . reduce( f)
1402
1520
}
1403
1521
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(...)`
1408
1523
///
1409
1524
/// See [`Iterator::all`]
1410
1525
#[ stable( feature = "new_range" , since = "1.0.0" ) ]
1411
1526
#[ 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
1413
1528
where
1414
1529
F : FnMut ( Idx ) -> bool ,
1415
1530
{
1416
- self . into_iter ( ) . all( f)
1531
+ self . iter_mut ( ) . all( f)
1417
1532
}
1418
1533
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(...)`
1423
1535
///
1424
1536
/// See [`Iterator::any`]
1425
1537
#[ stable( feature = "new_range" , since = "1.0.0" ) ]
1426
1538
#[ 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
1428
1540
where
1429
1541
F : FnMut ( Idx ) -> bool ,
1430
1542
{
1431
- self . into_iter ( ) . any( f)
1543
+ self . iter_mut ( ) . any( f)
1432
1544
}
1433
1545
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(...)`
1438
1547
///
1439
1548
/// See [`Iterator::find`]
1440
1549
#[ stable( feature = "new_range" , since = "1.0.0" ) ]
1441
1550
#[ 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 >
1443
1552
where
1444
1553
P : FnMut ( & Idx ) -> bool ,
1445
1554
{
1446
- self . into_iter ( ) . find( predicate)
1555
+ self . iter_mut ( ) . find( predicate)
1447
1556
}
1448
1557
1449
1558
/// Shorthand for `.into_iter().max()`
0 commit comments