File tree Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Expand file tree Collapse file tree 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> {
425425
426426 #[ inline]
427427 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.
430432 if !( self . start <= self . end ) {
431433 return ( 0 , Some ( 0 ) ) ;
432434 }
433435
436+ // NOTE: the safety of `unsafe impl TrustedLen` depends on this being correct!
434437 match Step :: steps_between ( & self . start , & self . end ) {
435438 Some ( hint) => ( hint. saturating_add ( 1 ) , hint. checked_add ( 1 ) ) ,
436439 None => ( usize:: MAX , None ) ,
Original file line number Diff line number Diff line change @@ -1165,6 +1165,24 @@ fn test_range_inclusive_nth() {
11651165 assert_eq ! ( r, 21 ...20 ) ;
11661166}
11671167
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+
11681186#[ test]
11691187fn test_range_step ( ) {
11701188 #![ allow( deprecated) ]
You can’t perform that action at this time.
0 commit comments