non_empty_iter/
cloned.rs

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