Skip to content

Commit

Permalink
get: panics if the range includes usize::MAX
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe-Cholet authored and jswrenn committed May 14, 2024
1 parent 7a9ce56 commit 4dd6ba0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/iter_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ impl<I> IteratorIndex<I> for RangeInclusive<usize>
where
I: Iterator,
{
type Output = Take<Skip<I>>;
type Output = Skip<Take<I>>;

fn index(self, iter: I) -> Self::Output {
iter.skip(*self.start())
.take((1 + *self.end()).saturating_sub(*self.start()))
assert_ne!(*self.end(), usize::MAX);
iter.take(self.end() + 1).skip(*self.start())
}
}

Expand All @@ -74,6 +74,7 @@ where
type Output = Take<I>;

fn index(self, iter: I) -> Self::Output {
assert_ne!(self.end, usize::MAX);
iter.take(self.end + 1)
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,8 @@ 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`.
///
/// It's a generalisation of [`Iterator::take`] and [`Iterator::skip`],
/// and uses these under the hood.
/// Therefore, the resulting iterator is [`DoubleEndedIterator`]
Expand Down

0 comments on commit 4dd6ba0

Please sign in to comment.