File tree Expand file tree Collapse file tree 3 files changed +23
-4
lines changed Expand file tree Collapse file tree 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>
4848where
4949 I : Iterator ,
5050{
51- type Output = Skip < Take < I > > ;
51+ type Output = Take < Skip < I > > ;
5252
5353 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)
5662 }
5763}
5864
Original file line number Diff line number Diff line change @@ -511,7 +511,7 @@ pub trait Itertools: Iterator {
511511 ///
512512 /// Works similarly to [`slice::get`](https://doc.rust-lang.org/std/primitive.slice.html#method.get).
513513 ///
514- /// **Panics** if the range includes ` usize::MAX`.
514+ /// **Panics** for ranges `..=usize::MAX` and `0..= usize::MAX`.
515515 ///
516516 /// It's a generalisation of [`Iterator::take`] and [`Iterator::skip`],
517517 /// 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>(
4040 is_dei_esi ( it. get ( ..) ) ;
4141}
4242
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+
4356#[ test]
4457fn product0 ( ) {
4558 let mut prod = iproduct ! ( ) ;
You can’t perform that action at this time.
0 commit comments