1use crate::iter::{FusedIterator, TrustedLen};
2use crate::num::NonZeroUsize;
3use crate::ops::Try;
4
5/// A double-ended iterator with the direction inverted.
6///
7/// This `struct` is created by the [`rev`] method on [`Iterator`]. See its
8/// documentation for more.
9///
10/// [`rev`]: Iterator::rev
11/// [`Iterator`]: trait.Iterator.html
12#[derive(Clone, Debug)]
13#[must_use = "iterators are lazy and do nothing unless consumed"]
14#[stable(feature = "rust1", since = "1.0.0")]
15pub struct Rev<T> {
16 iter: T,
17}
18
19impl<T> Rev<T> {
20 pub(in crate::iter) fn new(iter: T) -> Rev<T> {
21 Rev { iter }
22 }
23}
24
25#[stable(feature = "rust1", since = "1.0.0")]
26impl<I> Iterator for Rev<I>
27where
28 I: DoubleEndedIterator,
29{
30 type Item = <I as Iterator>::Item;
31
32 #[inline]
33 fn next(&mut self) -> Option<<I as Iterator>::Item> {
34 self.iter.next_back()
35 }
36 #[inline]
37 fn size_hint(&self) -> (usize, Option<usize>) {
38 self.iter.size_hint()
39 }
40
41 #[inline]
42 fn advance_by(&mut self, n: usize) -> Result<(), NonZeroUsize> {
43 self.iter.advance_back_by(n)
44 }
45
46 #[inline]
47 fn nth(&mut self, n: usize) -> Option<<I as Iterator>::Item> {
48 self.iter.nth_back(n)
49 }
50
51 fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
52 where
53 Self: Sized,
54 F: FnMut(B, Self::Item) -> R,
55 R: Try<Output = B>,
56 {
57 self.iter.try_rfold(init, f)
58 }
59
60 fn fold<Acc, F>(self, init: Acc, f: F) -> Acc
61 where
62 F: FnMut(Acc, Self::Item) -> Acc,
63 {
64 self.iter.rfold(init, f)
65 }
66
67 #[inline]
68 fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
69 where
70 P: FnMut(&Self::Item) -> bool,
71 {
72 self.iter.rfind(predicate)
73 }
74}
75
76#[stable(feature = "rust1", since = "1.0.0")]
77impl<I> DoubleEndedIterator for Rev<I>
78where
79 I: DoubleEndedIterator,
80{
81 #[inline]
82 fn next_back(&mut self) -> Option<<I as Iterator>::Item> {
83 self.iter.next()
84 }
85
86 #[inline]
87 fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> {
88 self.iter.advance_by(n)
89 }
90
91 #[inline]
92 fn nth_back(&mut self, n: usize) -> Option<<I as Iterator>::Item> {
93 self.iter.nth(n)
94 }
95
96 fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R
97 where
98 Self: Sized,
99 F: FnMut(B, Self::Item) -> R,
100 R: Try<Output = B>,
101 {
102 self.iter.try_fold(init, f)
103 }
104
105 fn rfold<Acc, F>(self, init: Acc, f: F) -> Acc
106 where
107 F: FnMut(Acc, Self::Item) -> Acc,
108 {
109 self.iter.fold(init, f)
110 }
111
112 fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
113 where
114 P: FnMut(&Self::Item) -> bool,
115 {
116 self.iter.find(predicate)
117 }
118}
119
120#[stable(feature = "rust1", since = "1.0.0")]
121impl<I> ExactSizeIterator for Rev<I>
122where
123 I: ExactSizeIterator + DoubleEndedIterator,
124{
125 fn len(&self) -> usize {
126 self.iter.len()
127 }
128
129 fn is_empty(&self) -> bool {
130 self.iter.is_empty()
131 }
132}
133
134#[stable(feature = "fused", since = "1.26.0")]
135impl<I> FusedIterator for Rev<I> where I: FusedIterator + DoubleEndedIterator {}
136
137#[unstable(feature = "trusted_len", issue = "37572")]
138unsafe impl<I> TrustedLen for Rev<I> where I: TrustedLen + DoubleEndedIterator {}
139
140#[stable(feature = "default_iters", since = "1.70.0")]
141impl<I: Default> Default for Rev<I> {
142 /// Creates a `Rev` iterator from the default value of `I`
143 /// ```
144 /// # use core::slice;
145 /// # use core::iter::Rev;
146 /// let iter: Rev<slice::Iter<'_, u8>> = Default::default();
147 /// assert_eq!(iter.len(), 0);
148 /// ```
149 fn default() -> Self {
150 Rev::new(iter:Default::default())
151 }
152}
153