@@ -68,11 +68,9 @@ macro_rules! step_impl_unsigned {
6868 issue = "42168" ) ]
6969 impl Step for $t {
7070 #[ inline]
71- #[ allow( trivial_numeric_casts) ]
7271 fn steps_between( start: & $t, end: & $t) -> Option <usize > {
7372 if * start < * end {
74- // Note: We assume $t <= usize here
75- Some ( ( * end - * start) as usize )
73+ usize :: try_from( * end - * start) . ok( )
7674 } else {
7775 Some ( 0 )
7876 }
@@ -98,13 +96,11 @@ macro_rules! step_impl_signed {
9896 issue = "42168" ) ]
9997 impl Step for $t {
10098 #[ inline]
101- #[ allow( trivial_numeric_casts) ]
10299 fn steps_between( start: & $t, end: & $t) -> Option <usize > {
103100 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( )
108104 } else {
109105 Some ( 0 )
110106 }
@@ -134,46 +130,9 @@ macro_rules! step_impl_signed {
134130 ) * )
135131}
136132
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 ) ;
163134step_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 ] ) ;
177136
178137macro_rules! range_exact_iter_impl {
179138 ( $( $t: ty) * ) => ( $(
@@ -229,7 +188,7 @@ impl<A: Step> Iterator for ops::Range<A> {
229188 fn size_hint ( & self ) -> ( usize , Option < usize > ) {
230189 match Step :: steps_between ( & self . start , & self . end ) {
231190 Some ( hint) => ( hint, Some ( hint) ) ,
232- None => ( 0 , None )
191+ None => ( usize :: MAX , None )
233192 }
234193 }
235194
@@ -350,7 +309,7 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
350309
351310 match Step :: steps_between ( & self . start , & self . end ) {
352311 Some ( hint) => ( hint. saturating_add ( 1 ) , hint. checked_add ( 1 ) ) ,
353- None => ( 0 , None ) ,
312+ None => ( usize :: MAX , None ) ,
354313 }
355314 }
356315
0 commit comments