File tree 2 files changed +23
-2
lines changed
2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -425,12 +425,15 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
425
425
426
426
#[ inline]
427
427
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
428
- // NOTE: the safety of `unsafe impl TrustedLen` depends on this being correct!
429
-
428
+ // This seems redundant with a similar comparison that `Step::steps_between`
429
+ // implementations need to do, but it separates the `start > end` case from `start == end`.
430
+ // `steps_between` returns `Some(0)` in both of these cases, but we only want to add 1
431
+ // in the latter.
430
432
if !( self . start <= self . end ) {
431
433
return ( 0 , Some ( 0 ) ) ;
432
434
}
433
435
436
+ // NOTE: the safety of `unsafe impl TrustedLen` depends on this being correct!
434
437
match Step :: steps_between ( & self . start , & self . end ) {
435
438
Some ( hint) => ( hint. saturating_add ( 1 ) , hint. checked_add ( 1 ) ) ,
436
439
None => ( usize:: MAX , None ) ,
Original file line number Diff line number Diff line change @@ -1165,6 +1165,24 @@ fn test_range_inclusive_nth() {
1165
1165
assert_eq ! ( r, 21 ...20 ) ;
1166
1166
}
1167
1167
1168
+ #[ test]
1169
+ fn test_range_len ( ) {
1170
+ assert_eq ! ( ( 0 ..10_u8 ) . len( ) , 10 ) ;
1171
+ assert_eq ! ( ( 9 ..10_u8 ) . len( ) , 1 ) ;
1172
+ assert_eq ! ( ( 10 ..10_u8 ) . len( ) , 0 ) ;
1173
+ assert_eq ! ( ( 11 ..10_u8 ) . len( ) , 0 ) ;
1174
+ assert_eq ! ( ( 100 ..10_u8 ) . len( ) , 0 ) ;
1175
+ }
1176
+
1177
+ #[ test]
1178
+ fn test_range_inclusive_len ( ) {
1179
+ assert_eq ! ( ( 0 ...10_u8 ) . len( ) , 11 ) ;
1180
+ assert_eq ! ( ( 9 ...10_u8 ) . len( ) , 2 ) ;
1181
+ assert_eq ! ( ( 10 ...10_u8 ) . len( ) , 1 ) ;
1182
+ assert_eq ! ( ( 11 ...10_u8 ) . len( ) , 0 ) ;
1183
+ assert_eq ! ( ( 100 ...10_u8 ) . len( ) , 0 ) ;
1184
+ }
1185
+
1168
1186
#[ test]
1169
1187
fn test_range_step ( ) {
1170
1188
#![ allow( deprecated) ]
You can’t perform that action at this time.
0 commit comments