@@ -15,32 +15,42 @@ use usize;
15
15
16
16
use super :: { FusedIterator , TrustedLen } ;
17
17
18
- /// Objects that can be stepped over in both directions.
19
- ///
20
- /// The `steps_between` function provides a way to efficiently compare
21
- /// two `Step` objects.
18
+ /// Objects that have a notion of *successor* and *predecessor*
19
+ /// for the purpose of range iterators.
22
20
#[ unstable( feature = "step_trait" ,
23
- reason = "likely to be replaced by finer-grained traits " ,
21
+ reason = "recently redesigned " ,
24
22
issue = "42168" ) ]
25
23
pub trait Step : Clone + PartialOrd + Sized {
26
- /// Returns the number of steps between two step objects. The count is
27
- /// inclusive of `start` and exclusive of `end`.
24
+ /// Returns the number of *successor* steps needed to get from `start` to `end`.
28
25
///
29
- /// Returns `None` if it is not possible to calculate `steps_between`
30
- /// without overflow.
26
+ /// Returns `None` if that number would overflow `usize`
27
+ /// (or is infinite, if `end` would never be reached).
28
+ /// Returns `Some(0)` if `start` comes after (is greater than) or equals `end`.
31
29
fn steps_between ( start : & Self , end : & Self ) -> Option < usize > ;
32
30
33
- /// Add an usize, returning None on overflow
34
- fn add_usize ( & self , n : usize ) -> Option < Self > ;
31
+ /// Returns the value that would be obtained by taking the *successor* of `self`,
32
+ /// `step_count` times.
33
+ ///
34
+ /// Returns `None` if this would overflow the range of values supported by the type `Self`.
35
+ ///
36
+ /// Note: `step_count == 1` is a common case,
37
+ /// used for example in `Iterator::next` for ranges.
38
+ fn forward ( & self , step_count : usize ) -> Option < Self > ;
35
39
36
- /// Subtracts an usize, returning None on overflow
37
- fn sub_usize ( & self , n : usize ) -> Option < Self > ;
40
+ /// Returns the value that would be obtained by taking the *predecessor* of `self`,
41
+ /// `step_count` times.
42
+ ///
43
+ /// Returns `None` if this would overflow the range of values supported by the type `Self`.
44
+ ///
45
+ /// Note: `step_count == 1` is a common case,
46
+ /// used for example in `Iterator::next_back` for ranges.
47
+ fn backward ( & self , step_count : usize ) -> Option < Self > ;
38
48
}
39
49
40
50
macro_rules! step_impl_unsigned {
41
51
( $( $t: ty) * ) => ( $(
42
52
#[ unstable( feature = "step_trait" ,
43
- reason = "likely to be replaced by finer-grained traits " ,
53
+ reason = "recently redesigned " ,
44
54
issue = "42168" ) ]
45
55
impl Step for $t {
46
56
#[ inline]
@@ -55,15 +65,15 @@ macro_rules! step_impl_unsigned {
55
65
}
56
66
57
67
#[ inline]
58
- fn add_usize ( & self , n: usize ) -> Option <Self > {
68
+ fn forward ( & self , n: usize ) -> Option <Self > {
59
69
match <$t>:: try_from( n) {
60
70
Ok ( n_as_t) => self . checked_add( n_as_t) ,
61
71
Err ( _) => None ,
62
72
}
63
73
}
64
74
65
75
#[ inline]
66
- fn sub_usize ( & self , n: usize ) -> Option <Self > {
76
+ fn backward ( & self , n: usize ) -> Option <Self > {
67
77
match <$t>:: try_from( n) {
68
78
Ok ( n_as_t) => self . checked_sub( n_as_t) ,
69
79
Err ( _) => None ,
@@ -75,7 +85,7 @@ macro_rules! step_impl_unsigned {
75
85
macro_rules! step_impl_signed {
76
86
( $( [ $t: ty : $unsigned: ty] ) * ) => ( $(
77
87
#[ unstable( feature = "step_trait" ,
78
- reason = "likely to be replaced by finer-grained traits " ,
88
+ reason = "recently redesigned " ,
79
89
issue = "42168" ) ]
80
90
impl Step for $t {
81
91
#[ inline]
@@ -92,11 +102,11 @@ macro_rules! step_impl_signed {
92
102
}
93
103
94
104
#[ inline]
95
- fn add_usize ( & self , n: usize ) -> Option <Self > {
105
+ fn forward ( & self , n: usize ) -> Option <Self > {
96
106
match <$unsigned>:: try_from( n) {
97
107
Ok ( n_as_unsigned) => {
98
108
// Wrapping in unsigned space handles cases like
99
- // `-120_i8.add_usize (200) == Some(80_i8)`,
109
+ // `-120_i8.forward (200) == Some(80_i8)`,
100
110
// even though 200_usize is out of range for i8.
101
111
let wrapped = ( * self as $unsigned) . wrapping_add( n_as_unsigned) as $t;
102
112
if wrapped >= * self {
@@ -109,11 +119,11 @@ macro_rules! step_impl_signed {
109
119
}
110
120
}
111
121
#[ inline]
112
- fn sub_usize ( & self , n: usize ) -> Option <Self > {
122
+ fn backward ( & self , n: usize ) -> Option <Self > {
113
123
match <$unsigned>:: try_from( n) {
114
124
Ok ( n_as_unsigned) => {
115
125
// Wrapping in unsigned space handles cases like
116
- // `-120_i8.add_usize (200) == Some(80_i8)`,
126
+ // `-120_i8.forward (200) == Some(80_i8)`,
117
127
// even though 200_usize is out of range for i8.
118
128
let wrapped = ( * self as $unsigned) . wrapping_sub( n_as_unsigned) as $t;
119
129
if wrapped <= * self {
@@ -132,7 +142,7 @@ macro_rules! step_impl_signed {
132
142
macro_rules! step_impl_no_between {
133
143
( $( $t: ty) * ) => ( $(
134
144
#[ unstable( feature = "step_trait" ,
135
- reason = "likely to be replaced by finer-grained traits " ,
145
+ reason = "recently redesigned " ,
136
146
issue = "42168" ) ]
137
147
impl Step for $t {
138
148
#[ inline]
@@ -141,12 +151,12 @@ macro_rules! step_impl_no_between {
141
151
}
142
152
143
153
#[ inline]
144
- fn add_usize ( & self , n: usize ) -> Option <Self > {
154
+ fn forward ( & self , n: usize ) -> Option <Self > {
145
155
self . checked_add( n as $t)
146
156
}
147
157
148
158
#[ inline]
149
- fn sub_usize ( & self , n: usize ) -> Option <Self > {
159
+ fn backward ( & self , n: usize ) -> Option <Self > {
150
160
self . checked_sub( n as $t)
151
161
}
152
162
}
@@ -205,7 +215,7 @@ impl<A: Step> Iterator for ops::Range<A> {
205
215
fn next ( & mut self ) -> Option < A > {
206
216
if self . start < self . end {
207
217
// `start + 1` should not overflow since `end` exists such that `start < end`
208
- let mut n = self . start . add_usize ( 1 ) . expect ( "overflow in Range::next" ) ;
218
+ let mut n = self . start . forward ( 1 ) . expect ( "overflow in Range::next" ) ;
209
219
mem:: swap ( & mut n, & mut self . start ) ;
210
220
Some ( n)
211
221
} else {
@@ -223,10 +233,10 @@ impl<A: Step> Iterator for ops::Range<A> {
223
233
224
234
#[ inline]
225
235
fn nth ( & mut self , n : usize ) -> Option < A > {
226
- if let Some ( plus_n) = self . start . add_usize ( n) {
236
+ if let Some ( plus_n) = self . start . forward ( n) {
227
237
if plus_n < self . end {
228
238
// `plus_n + 1` should not overflow since `end` exists such that `plus_n < end`
229
- self . start = plus_n. add_usize ( 1 ) . expect ( "overflow in Range::nth" ) ;
239
+ self . start = plus_n. forward ( 1 ) . expect ( "overflow in Range::nth" ) ;
230
240
return Some ( plus_n)
231
241
}
232
242
}
@@ -256,7 +266,7 @@ impl<A: Step> DoubleEndedIterator for ops::Range<A> {
256
266
fn next_back ( & mut self ) -> Option < A > {
257
267
if self . start < self . end {
258
268
// `end - 1` should not overflow since `start` exists such that `start < end`
259
- self . end = self . end . sub_usize ( 1 ) . expect ( "overflow in Range::nth_back" ) ;
269
+ self . end = self . end . backward ( 1 ) . expect ( "overflow in Range::nth_back" ) ;
260
270
Some ( self . end . clone ( ) )
261
271
} else {
262
272
None
@@ -274,7 +284,7 @@ impl<A: Step> Iterator for ops::RangeFrom<A> {
274
284
#[ inline]
275
285
fn next ( & mut self ) -> Option < A > {
276
286
// Overflow can happen here. Panic when it does.
277
- let mut n = self . start . add_usize ( 1 ) . expect ( "overflow in RangeFrom::next" ) ;
287
+ let mut n = self . start . forward ( 1 ) . expect ( "overflow in RangeFrom::next" ) ;
278
288
mem:: swap ( & mut n, & mut self . start ) ;
279
289
Some ( n)
280
290
}
@@ -287,8 +297,8 @@ impl<A: Step> Iterator for ops::RangeFrom<A> {
287
297
#[ inline]
288
298
fn nth ( & mut self , n : usize ) -> Option < A > {
289
299
// Overflow can happen here. Panic when it does.
290
- let plus_n = self . start . add_usize ( n) . expect ( "overflow in RangeFrom::nth" ) ;
291
- self . start = plus_n. add_usize ( 1 ) . expect ( "overflow in RangeFrom::nth" ) ;
300
+ let plus_n = self . start . forward ( n) . expect ( "overflow in RangeFrom::nth" ) ;
301
+ self . start = plus_n. forward ( 1 ) . expect ( "overflow in RangeFrom::nth" ) ;
292
302
Some ( plus_n)
293
303
}
294
304
}
@@ -307,18 +317,18 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
307
317
match self . start . partial_cmp ( & self . end ) {
308
318
Some ( Less ) => {
309
319
// `start + 1` should not overflow since `end` exists such that `start < end`
310
- let n = self . start . add_usize ( 1 ) . expect ( "overflow in RangeInclusive::next" ) ;
320
+ let n = self . start . forward ( 1 ) . expect ( "overflow in RangeInclusive::next" ) ;
311
321
Some ( mem:: replace ( & mut self . start , n) )
312
322
} ,
313
323
Some ( Equal ) => {
314
324
let last;
315
- if let Some ( end_plus_one) = self . end . add_usize ( 1 ) {
325
+ if let Some ( end_plus_one) = self . end . forward ( 1 ) {
316
326
last = mem:: replace ( & mut self . start , end_plus_one) ;
317
327
} else {
318
328
last = self . start . clone ( ) ;
319
329
// `start == end`, and `end + 1` underflowed.
320
330
// `start - 1` overflowing would imply a type with only one valid value?
321
- self . end = self . start . sub_usize ( 1 ) . expect ( "overflow in RangeInclusive::next" ) ;
331
+ self . end = self . start . backward ( 1 ) . expect ( "overflow in RangeInclusive::next" ) ;
322
332
}
323
333
Some ( last)
324
334
} ,
@@ -340,35 +350,35 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
340
350
341
351
#[ inline]
342
352
fn nth ( & mut self , n : usize ) -> Option < A > {
343
- if let Some ( plus_n) = self . start . add_usize ( n) {
353
+ if let Some ( plus_n) = self . start . forward ( n) {
344
354
use cmp:: Ordering :: * ;
345
355
346
356
match plus_n. partial_cmp ( & self . end ) {
347
357
Some ( Less ) => {
348
358
// `plus_n + 1` should not overflow since `end` exists such that `plus_n < end`
349
- self . start = plus_n. add_usize ( 1 ) . expect ( "overflow in RangeInclusive::nth" ) ;
359
+ self . start = plus_n. forward ( 1 ) . expect ( "overflow in RangeInclusive::nth" ) ;
350
360
return Some ( plus_n)
351
361
}
352
362
Some ( Equal ) => {
353
- if let Some ( end_plus_one) = self . end . add_usize ( 1 ) {
363
+ if let Some ( end_plus_one) = self . end . forward ( 1 ) {
354
364
self . start = end_plus_one
355
365
} else {
356
366
// `start == end`, and `end + 1` underflowed.
357
367
// `start - 1` overflowing would imply a type with only one valid value?
358
- self . end = self . start . sub_usize ( 1 ) . expect ( "overflow in RangeInclusive::nth" )
368
+ self . end = self . start . backward ( 1 ) . expect ( "overflow in RangeInclusive::nth" )
359
369
}
360
370
return Some ( plus_n)
361
371
}
362
372
_ => { }
363
373
}
364
374
}
365
375
366
- if let Some ( end_plus_one) = self . end . add_usize ( 1 ) {
376
+ if let Some ( end_plus_one) = self . end . forward ( 1 ) {
367
377
self . start = end_plus_one
368
378
} else {
369
379
// `start == end`, and `end + 1` underflowed.
370
380
// `start - 1` overflowing would imply a type with only one valid value?
371
- self . end = self . start . sub_usize ( 1 ) . expect ( "overflow in RangeInclusive::nth" )
381
+ self . end = self . start . backward ( 1 ) . expect ( "overflow in RangeInclusive::nth" )
372
382
}
373
383
None
374
384
}
@@ -383,18 +393,18 @@ impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {
383
393
match self . start . partial_cmp ( & self . end ) {
384
394
Some ( Less ) => {
385
395
// `end - 1` should not overflow since `start` exists such that `start < end`
386
- let n = self . end . sub_usize ( 1 ) . expect ( "overflow in RangeInclusive::next_back" ) ;
396
+ let n = self . end . backward ( 1 ) . expect ( "overflow in RangeInclusive::next_back" ) ;
387
397
Some ( mem:: replace ( & mut self . end , n) )
388
398
} ,
389
399
Some ( Equal ) => {
390
400
let last;
391
- if let Some ( start_minus_one) = self . start . sub_usize ( 1 ) {
401
+ if let Some ( start_minus_one) = self . start . backward ( 1 ) {
392
402
last = mem:: replace ( & mut self . end , start_minus_one) ;
393
403
} else {
394
404
last = self . end . clone ( ) ;
395
405
// `start == end`, and `start - 1` underflowed.
396
406
// `end + 1` overflowing would imply a type with only one valid value?
397
- self . start = self . start . add_usize ( 1 ) . expect ( "overflow in RangeInclusive::next_back" ) ;
407
+ self . start = self . start . forward ( 1 ) . expect ( "overflow in RangeInclusive::next_back" ) ;
398
408
}
399
409
Some ( last)
400
410
} ,
0 commit comments