Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
Fix my own review, update docs and fix a warning with `#[cfg(doc)]`.
  • Loading branch information
Philippe-Cholet committed Mar 1, 2024
1 parent 282e867 commit 97c74c3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
11 changes: 5 additions & 6 deletions src/iter_index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::iter::{Skip, Take};
use core::ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};

#[cfg(doc)]
use crate::Itertools;

mod private_iter_index {
Expand All @@ -16,17 +17,16 @@ mod private_iter_index {
impl Sealed for ops::RangeFull {}
}

/// Used by the ``range`` function to know which iterator
/// Used by [`get`] and [`Itertools::get`] to know which iterator
/// to turn different ranges into.
pub trait IteratorIndex<I>: private_iter_index::Sealed
where
I: Iterator,
{
/// The type that [`get`] or [`Itertools::get`]
/// returns when called with this type of index.
/// The type returned for this type of index.
type Output: Iterator<Item = I::Item>;

/// Returns an iterator(or value) in the specified range.
/// Returns an adapted iterator for the current index.
///
/// Prefer calling [`get`] or [`Itertools::get`] instead
/// of calling this directly.
Expand Down Expand Up @@ -102,8 +102,7 @@ where
}
}

/// Returns an element of the iterator or an iterator
/// over a subsection of the iterator.
/// Returns an iterator over a subsection of the iterator.
///
/// See [`Itertools::get`] for more information.
pub fn get<I, R>(iter: I, index: R) -> R::Output
Expand Down
18 changes: 9 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,13 +505,14 @@ pub trait Itertools: Iterator {
intersperse::intersperse_with(self, element)
}

/// Returns an element at a specific location, or returns an iterator
/// over a subsection of the iterator.
/// Returns an iterator over a subsection of the iterator.
///
/// Works similarly to [`slice::get`](https://doc.rust-lang.org/std/primitive.slice.html#method.get).
///
/// It's a generalisation of [`take`], [`skip`] and [`nth`], and uses these
/// under the hood.
/// It's a generalisation of [`Iterator::take`] and [`Iterator::skip`],
/// and uses these under the hood.
/// Therefore, the resulting iterator is [`DoubleEndedIterator`]
/// and/or [`ExactSizeIterator`] if the adapted iterator is.
///
/// # Unspecified Behavior
/// The result of indexing with an exhausted [`core::ops::RangeInclusive`] is unspecified.
Expand All @@ -524,7 +525,7 @@ pub trait Itertools: Iterator {
/// let vec = vec![3, 1, 4, 1, 5];
///
/// let mut range: Vec<_> =
/// vec.iter().get(1..=3).copied().collect();
/// vec.iter().get(1..=3).copied().collect();
/// assert_eq!(&range, &[1, 4, 1]);
///
/// // It works with other types of ranges, too
Expand All @@ -537,13 +538,12 @@ pub trait Itertools: Iterator {
/// range = vec.iter().get(2..).copied().collect();
/// assert_eq!(&range, &[4, 1, 5]);
///
/// range = vec.iter().get(..=2).copied().collect();
/// assert_eq!(&range, &[3, 1, 4]);
///
/// range = vec.iter().get(..).copied().collect();
/// assert_eq!(range, vec);
/// ```
///
/// [`take`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.take
/// [`skip`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.skip
/// [`nth`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.nth
fn get<R>(self, index: R) -> R::Output
where
Self: Sized,
Expand Down

0 comments on commit 97c74c3

Please sign in to comment.