non_empty_iter/
take.rs

1//! Iterating over only the first provided number of items in non-empty iterators.
2
3use core::iter;
4
5use non_zero_size::Size;
6
7use crate::non_empty::NonEmptyIterator;
8
9/// Represents non-empty iterators that only iterate over the first given number of items
10/// of the underlying iterator.
11#[derive(Debug, Clone)]
12#[must_use = "non-empty iterators are lazy and do nothing unless consumed"]
13pub struct Take<I: NonEmptyIterator> {
14    non_empty: I,
15    count: Size,
16}
17
18impl<I: NonEmptyIterator> Take<I> {
19    /// Constructs [`Self`].
20    pub const fn new(non_empty: I, count: Size) -> Self {
21        Self { non_empty, count }
22    }
23}
24
25impl<I: NonEmptyIterator> IntoIterator for Take<I> {
26    type Item = I::Item;
27
28    type IntoIter = iter::Take<I::IntoIter>;
29
30    fn into_iter(self) -> Self::IntoIter {
31        self.non_empty.into_iter().take(self.count.get())
32    }
33}
34
35unsafe impl<I: NonEmptyIterator> NonEmptyIterator for Take<I> {}