non_empty_iter/
inspect.rs

1//! Inspecting items of non-empty iterators.
2
3use core::iter;
4
5use crate::non_empty::NonEmptyIterator;
6
7/// Represents non-empty iterators that allow inspecting each item before yielding it.
8///
9/// This `struct` is created by the [`inspect`] method on [`NonEmptyIterator`].
10/// See its documentation for more.
11///
12/// [`inspect`]: NonEmptyIterator::inspect
13#[derive(Debug, Clone)]
14#[must_use = "non-empty iterators are lazy and do nothing unless consumed"]
15pub struct Inspect<I: NonEmptyIterator, F> {
16    non_empty: I,
17    function: F,
18}
19
20impl<I: NonEmptyIterator, F> Inspect<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<I: NonEmptyIterator, F: FnMut(&I::Item)> IntoIterator for Inspect<I, F> {
31    type Item = I::Item;
32
33    type IntoIter = iter::Inspect<I::IntoIter, F>;
34
35    fn into_iter(self) -> Self::IntoIter {
36        self.non_empty.into_iter().inspect(self.function)
37    }
38}
39
40unsafe impl<I: NonEmptyIterator, F: FnMut(&I::Item)> NonEmptyIterator for Inspect<I, F> {}