Skip to content

Commit 7d8171f

Browse files
get(s..=usize::MAX) should be fine when s != 0
1 parent a2cab3b commit 7d8171f

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/iter_index.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,17 @@ impl<I> IteratorIndex<I> for RangeInclusive<usize>
4848
where
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

src/lib.rs

+1-1
Original file line numberDiff line numberDiff 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.

0 commit comments

Comments
 (0)