non_empty_iter/
rev.rs

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