non_empty_iter/
flatten.rs

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