non_empty_iter/
map.rs

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