non_empty_iter/
enumerate.rs

1//! Enumerating items in non-empty iterators.
2
3use core::iter;
4
5use crate::non_empty::NonEmptyIterator;
6
7/// Represents non-empty iterators that yield the current count and the item during iteration.
8///
9/// This `struct` is created by the [`enumerate`] method on [`NonEmptyIterator`].
10/// See its documentation for more.
11///
12/// [`enumerate`]: NonEmptyIterator::enumerate
13#[derive(Debug, Clone)]
14#[must_use = "non-empty iterators are lazy and do nothing unless consumed"]
15pub struct Enumerate<I: NonEmptyIterator> {
16    non_empty: I,
17}
18
19impl<I: NonEmptyIterator> Enumerate<I> {
20    /// Constructs [`Self`].
21    pub const fn new(non_empty: I) -> Self {
22        Self { non_empty }
23    }
24}
25
26impl<I: NonEmptyIterator> IntoIterator for Enumerate<I> {
27    type Item = (usize, I::Item);
28
29    type IntoIter = iter::Enumerate<I::IntoIter>;
30
31    fn into_iter(self) -> Self::IntoIter {
32        self.non_empty.into_iter().enumerate()
33    }
34}
35
36unsafe impl<I: NonEmptyIterator> NonEmptyIterator for Enumerate<I> {}