non_empty_iter/
fuse.rs

1//! Fusing non-empty iterators.
2
3use core::iter;
4
5use crate::non_empty::NonEmptyIterator;
6
7/// Represents non-empty iterators that yield [`None`] forever after the underlying
8/// iterator yields [`None`] once.
9///
10/// This `struct` is created by the [`fuse`] method on [`NonEmptyIterator`].
11/// See its documentation for more.
12///
13/// [`fuse`]: NonEmptyIterator::fuse
14#[derive(Debug, Clone)]
15#[must_use = "non-empty iterators are lazy and do nothing unless consumed"]
16pub struct Fuse<I: NonEmptyIterator> {
17    non_empty: I,
18}
19
20impl<I: NonEmptyIterator> Fuse<I> {
21    /// Constructs [`Self`].
22    pub const fn new(non_empty: I) -> Self {
23        Self { non_empty }
24    }
25}
26
27impl<I: NonEmptyIterator> IntoIterator for Fuse<I> {
28    type Item = I::Item;
29
30    type IntoIter = iter::Fuse<I::IntoIter>;
31
32    fn into_iter(self) -> Self::IntoIter {
33        self.non_empty.into_iter().fuse()
34    }
35}
36
37unsafe impl<I: NonEmptyIterator> NonEmptyIterator for Fuse<I> {}