Skip to content

Commit

Permalink
get(s..=usize::MAX) should be fine when s != 0
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe-Cholet committed May 2, 2024
1 parent a2cab3b commit 7d8171f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/iter_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ impl<I> IteratorIndex<I> for RangeInclusive<usize>
where
I: Iterator,
{
type Output = Skip<Take<I>>;
type Output = Take<Skip<I>>;

fn index(self, iter: I) -> Self::Output {
assert_ne!(*self.end(), usize::MAX);
iter.take(self.end() + 1).skip(*self.start())
// end - start + 1 without overflowing if possible
let length = if *self.end() == usize::MAX {
assert_ne!(*self.start(), 0);
self.end() - self.start() + 1
} else {
(self.end() + 1).saturating_sub(*self.start())
};
iter.skip(*self.start()).take(length)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ pub trait Itertools: Iterator {
///
/// Works similarly to [`slice::get`](https://doc.rust-lang.org/std/primitive.slice.html#method.get).
///
/// **Panics** if the range includes `usize::MAX`.
/// **Panics** for ranges `..=usize::MAX` and `0..=usize::MAX`.
///
/// It's a generalisation of [`Iterator::take`] and [`Iterator::skip`],
/// and uses these under the hood.
Expand Down

0 comments on commit 7d8171f

Please sign in to comment.