File tree 3 files changed +23
-4
lines changed
3 files changed +23
-4
lines changed Original file line number Diff line number Diff line change @@ -48,11 +48,17 @@ impl<I> IteratorIndex<I> for RangeInclusive<usize>
48
48
where
49
49
I : Iterator ,
50
50
{
51
- type Output = Skip < Take < I > > ;
51
+ type Output = Take < Skip < I > > ;
52
52
53
53
fn index ( self , iter : I ) -> Self :: Output {
54
- assert_ne ! ( * self . end( ) , usize :: MAX ) ;
55
- iter. take ( self . end ( ) + 1 ) . skip ( * self . start ( ) )
54
+ // end - start + 1 without overflowing if possible
55
+ let length = if * self . end ( ) == usize:: MAX {
56
+ assert_ne ! ( * self . start( ) , 0 ) ;
57
+ self . end ( ) - self . start ( ) + 1
58
+ } else {
59
+ ( self . end ( ) + 1 ) . saturating_sub ( * self . start ( ) )
60
+ } ;
61
+ iter. skip ( * self . start ( ) ) . take ( length)
56
62
}
57
63
}
58
64
Original file line number Diff line number Diff line change @@ -511,7 +511,7 @@ pub trait Itertools: Iterator {
511
511
///
512
512
/// Works similarly to [`slice::get`](https://doc.rust-lang.org/std/primitive.slice.html#method.get).
513
513
///
514
- /// **Panics** if the range includes ` usize::MAX`.
514
+ /// **Panics** for ranges `..=usize::MAX` and `0..= usize::MAX`.
515
515
///
516
516
/// It's a generalisation of [`Iterator::take`] and [`Iterator::skip`],
517
517
/// and uses these under the hood.
Original file line number Diff line number Diff line change @@ -40,6 +40,19 @@ fn get_dei_esi_then_dei_esi<I: DoubleEndedIterator + ExactSizeIterator + Clone>(
40
40
is_dei_esi ( it. get ( ..) ) ;
41
41
}
42
42
43
+ #[ test]
44
+ fn get_1_max ( ) {
45
+ let mut it = ( 0 ..5 ) . get ( 1 ..=usize:: MAX ) ;
46
+ assert_eq ! ( it. next( ) , Some ( 1 ) ) ;
47
+ assert_eq ! ( it. next_back( ) , Some ( 4 ) ) ;
48
+ }
49
+
50
+ #[ test]
51
+ #[ should_panic]
52
+ fn get_full_range_inclusive ( ) {
53
+ let _it = ( 0 ..5 ) . get ( 0 ..=usize:: MAX ) ;
54
+ }
55
+
43
56
#[ test]
44
57
fn product0 ( ) {
45
58
let mut prod = iproduct ! ( ) ;
You can’t perform that action at this time.
0 commit comments