non_empty_iter/
copied.rs

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