ark_std/iterable/rev.rs
1use super::Iterable;
2use crate::iter::Rev;
3
4/// Stream that goes over an `[ExactSizeIterator]` in reverse order.
5///
6/// This stream allows to switch fast from little endian ordering used in
7/// time-efficient algorithms, e.g. in slices `&[T]` into big endia ordering
8/// (used in space-efficient algorithms.
9///
10/// # Examples
11/// ```
12/// use ark_std::iterable::{Iterable, Reverse};
13///
14/// let le_v = &[1, 2, 3];
15/// let be_v = Reverse(le_v);
16/// let mut be_v_iter = be_v.iter();
17/// assert_eq!(be_v_iter.next(), Some(&3));
18/// assert_eq!(be_v_iter.next(), Some(&2));
19/// assert_eq!(be_v_iter.next(), Some(&1));
20/// ```
21#[derive(Clone, Copy)]
22pub struct Reverse<I>(pub I)
23where
24 I: Iterable,
25 I::Iter: DoubleEndedIterator;
26
27impl<I> Iterable for Reverse<I>
28where
29 I: Iterable,
30 I::Iter: DoubleEndedIterator,
31{
32 type Item = I::Item;
33 type Iter = Rev<I::Iter>;
34
35 #[inline]
36 fn iter(&self) -> Self::Iter {
37 self.0.iter().rev()
38 }
39
40 #[inline]
41 fn len(&self) -> usize {
42 self.0.len()
43 }
44}