non_empty_iter/
adapter.rs

1//! Non-empty adapter.
2
3use crate::non_empty::NonEmptyIterator;
4
5/// Adapts [`IntoIterator`] values that are known to be non-empty to implement [`NonEmptyIterator`].
6///
7/// This adapter is primarily used in the [`TryIntoNonEmptyIterator`] trait implementation.
8///
9/// [`TryIntoNonEmptyIterator`]: crate::non_empty::TryIntoNonEmptyIterator
10#[derive(Debug, Clone)]
11#[must_use = "non-empty iterators are lazy and do nothing unless consumed"]
12pub struct NonEmptyAdapter<I: IntoIterator> {
13    iterable: I,
14}
15
16impl<I: IntoIterator> NonEmptyAdapter<I> {
17    /// Constructs [`Self`].
18    ///
19    /// # Safety
20    ///
21    /// The caller must guarantee that the provided iterable is non-empty.
22    pub const unsafe fn new(iterable: I) -> Self {
23        Self { iterable }
24    }
25}
26
27impl<I: IntoIterator> IntoIterator for NonEmptyAdapter<I> {
28    type Item = I::Item;
29
30    type IntoIter = I::IntoIter;
31
32    fn into_iter(self) -> Self::IntoIter {
33        self.iterable.into_iter()
34    }
35}
36
37unsafe impl<I: IntoIterator> NonEmptyIterator for NonEmptyAdapter<I> {}