@@ -68,11 +68,9 @@ macro_rules! step_impl_unsigned {
68
68
issue = "42168" ) ]
69
69
impl Step for $t {
70
70
#[ inline]
71
- #[ allow( trivial_numeric_casts) ]
72
71
fn steps_between( start: & $t, end: & $t) -> Option <usize > {
73
72
if * start < * end {
74
- // Note: We assume $t <= usize here
75
- Some ( ( * end - * start) as usize )
73
+ usize :: try_from( * end - * start) . ok( )
76
74
} else {
77
75
Some ( 0 )
78
76
}
@@ -98,13 +96,11 @@ macro_rules! step_impl_signed {
98
96
issue = "42168" ) ]
99
97
impl Step for $t {
100
98
#[ inline]
101
- #[ allow( trivial_numeric_casts) ]
102
99
fn steps_between( start: & $t, end: & $t) -> Option <usize > {
103
100
if * start < * end {
104
- // Note: We assume $t <= isize here
105
- // Use .wrapping_sub and cast to usize to compute the
106
- // difference that may not fit inside the range of isize.
107
- Some ( ( * end as isize ) . wrapping_sub( * start as isize ) as usize )
101
+ // Use .wrapping_sub and cast to unsigned to compute the
102
+ // difference that may not fit inside the range of $t.
103
+ usize :: try_from( end. wrapping_sub( * start) as $unsigned) . ok( )
108
104
} else {
109
105
Some ( 0 )
110
106
}
@@ -134,46 +130,9 @@ macro_rules! step_impl_signed {
134
130
) * )
135
131
}
136
132
137
- macro_rules! step_impl_no_between {
138
- ( $( $t: ty) * ) => ( $(
139
- #[ unstable( feature = "step_trait" ,
140
- reason = "likely to be replaced by finer-grained traits" ,
141
- issue = "42168" ) ]
142
- impl Step for $t {
143
- #[ inline]
144
- fn steps_between( _start: & Self , _end: & Self ) -> Option <usize > {
145
- None
146
- }
147
-
148
- #[ inline]
149
- fn add_usize( & self , n: usize ) -> Option <Self > {
150
- self . checked_add( n as $t)
151
- }
152
-
153
- step_identical_methods!( ) ;
154
- }
155
- ) * )
156
- }
157
-
158
- step_impl_unsigned ! ( usize u8 u16 ) ;
159
- #[ cfg( not( target_pointer_width = "16" ) ) ]
160
- step_impl_unsigned ! ( u32 ) ;
161
- #[ cfg( target_pointer_width = "16" ) ]
162
- step_impl_no_between ! ( u32 ) ;
133
+ step_impl_unsigned ! ( usize u8 u16 u32 u64 u128 ) ;
163
134
step_impl_signed ! ( [ isize : usize ] [ i8 : u8 ] [ i16 : u16 ] ) ;
164
- #[ cfg( not( target_pointer_width = "16" ) ) ]
165
- step_impl_signed ! ( [ i32 : u32 ] ) ;
166
- #[ cfg( target_pointer_width = "16" ) ]
167
- step_impl_no_between ! ( i32 ) ;
168
- #[ cfg( target_pointer_width = "64" ) ]
169
- step_impl_unsigned ! ( u64 ) ;
170
- #[ cfg( target_pointer_width = "64" ) ]
171
- step_impl_signed ! ( [ i64 : u64 ] ) ;
172
- // If the target pointer width is not 64-bits, we
173
- // assume here that it is less than 64-bits.
174
- #[ cfg( not( target_pointer_width = "64" ) ) ]
175
- step_impl_no_between ! ( u64 i64 ) ;
176
- step_impl_no_between ! ( u128 i128 ) ;
135
+ step_impl_signed ! ( [ i32 : u32 ] [ i64 : u64 ] [ i128 : u128 ] ) ;
177
136
178
137
macro_rules! range_exact_iter_impl {
179
138
( $( $t: ty) * ) => ( $(
@@ -229,7 +188,7 @@ impl<A: Step> Iterator for ops::Range<A> {
229
188
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
230
189
match Step :: steps_between ( & self . start , & self . end ) {
231
190
Some ( hint) => ( hint, Some ( hint) ) ,
232
- None => ( 0 , None )
191
+ None => ( usize :: MAX , None )
233
192
}
234
193
}
235
194
@@ -350,7 +309,7 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
350
309
351
310
match Step :: steps_between ( & self . start , & self . end ) {
352
311
Some ( hint) => ( hint. saturating_add ( 1 ) , hint. checked_add ( 1 ) ) ,
353
- None => ( 0 , None ) ,
312
+ None => ( usize :: MAX , None ) ,
354
313
}
355
314
}
356
315
0 commit comments