@@ -30,34 +30,13 @@ pub trait Step: Clone + PartialOrd + Sized {
30
30
/// without overflow.
31
31
fn steps_between ( start : & Self , end : & Self ) -> Option < usize > ;
32
32
33
- /// Replaces this step with `1`, returning itself
34
- fn replace_one ( & mut self ) -> Self ;
35
-
36
- /// Replaces this step with `0`, returning itself
37
- fn replace_zero ( & mut self ) -> Self ;
38
-
39
33
/// Add an usize, returning None on overflow
40
34
fn add_usize ( & self , n : usize ) -> Option < Self > ;
41
35
42
36
/// Subtracts an usize, returning None on overflow
43
37
fn sub_usize ( & self , n : usize ) -> Option < Self > ;
44
38
}
45
39
46
- // These are still macro-generated because the integer literals resolve to different types.
47
- macro_rules! step_identical_methods {
48
- ( ) => {
49
- #[ inline]
50
- fn replace_one( & mut self ) -> Self {
51
- mem:: replace( self , 1 )
52
- }
53
-
54
- #[ inline]
55
- fn replace_zero( & mut self ) -> Self {
56
- mem:: replace( self , 0 )
57
- }
58
- }
59
- }
60
-
61
40
macro_rules! step_impl_unsigned {
62
41
( $( $t: ty) * ) => ( $(
63
42
#[ unstable( feature = "step_trait" ,
@@ -90,8 +69,6 @@ macro_rules! step_impl_unsigned {
90
69
Err ( _) => None ,
91
70
}
92
71
}
93
-
94
- step_identical_methods!( ) ;
95
72
}
96
73
) * )
97
74
}
@@ -148,8 +125,6 @@ macro_rules! step_impl_signed {
148
125
Err ( _) => None ,
149
126
}
150
127
}
151
-
152
- step_identical_methods!( ) ;
153
128
}
154
129
) * )
155
130
}
@@ -174,8 +149,6 @@ macro_rules! step_impl_no_between {
174
149
fn sub_usize( & self , n: usize ) -> Option <Self > {
175
150
self . checked_sub( n as $t)
176
151
}
177
-
178
- step_identical_methods!( ) ;
179
152
}
180
153
) * )
181
154
}
@@ -338,8 +311,15 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
338
311
Some ( mem:: replace ( & mut self . start , n) )
339
312
} ,
340
313
Some ( Equal ) => {
341
- let last = self . start . replace_one ( ) ;
342
- self . end . replace_zero ( ) ;
314
+ let last;
315
+ if let Some ( end_plus_one) = self . end . add_usize ( 1 ) {
316
+ last = mem:: replace ( & mut self . start , end_plus_one) ;
317
+ } else {
318
+ last = self . start . clone ( ) ;
319
+ // `start == end`, and `end + 1` underflowed.
320
+ // `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" ) ;
322
+ }
343
323
Some ( last)
344
324
} ,
345
325
_ => None ,
@@ -370,16 +350,26 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
370
350
return Some ( plus_n)
371
351
}
372
352
Some ( Equal ) => {
373
- self . start . replace_one ( ) ;
374
- self . end . replace_zero ( ) ;
353
+ if let Some ( end_plus_one) = self . end . add_usize ( 1 ) {
354
+ self . start = end_plus_one
355
+ } else {
356
+ // `start == end`, and `end + 1` underflowed.
357
+ // `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" )
359
+ }
375
360
return Some ( plus_n)
376
361
}
377
362
_ => { }
378
363
}
379
364
}
380
365
381
- self . start . replace_one ( ) ;
382
- self . end . replace_zero ( ) ;
366
+ if let Some ( end_plus_one) = self . end . add_usize ( 1 ) {
367
+ self . start = end_plus_one
368
+ } else {
369
+ // `start == end`, and `end + 1` underflowed.
370
+ // `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" )
372
+ }
383
373
None
384
374
}
385
375
}
@@ -397,8 +387,15 @@ impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {
397
387
Some ( mem:: replace ( & mut self . end , n) )
398
388
} ,
399
389
Some ( Equal ) => {
400
- let last = self . end . replace_zero ( ) ;
401
- self . start . replace_one ( ) ;
390
+ let last;
391
+ if let Some ( start_minus_one) = self . start . sub_usize ( 1 ) {
392
+ last = mem:: replace ( & mut self . end , start_minus_one) ;
393
+ } else {
394
+ last = self . end . clone ( ) ;
395
+ // `start == end`, and `start - 1` underflowed.
396
+ // `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" ) ;
398
+ }
402
399
Some ( last)
403
400
} ,
404
401
_ => None ,
0 commit comments