bounded_vec_deque/
iter.rs

1use ::std::collections::vec_deque;
2#[cfg(feature = "fused")]
3use ::std::iter::FusedIterator;
4
5use ::BoundedVecDeque;
6
7macro_rules! forward_iterator_impls {
8    ( impl($($param:tt)*), $impl_type:ty, $item_type:ty ) => {
9        impl<$($param)*> Iterator for $impl_type {
10            type Item = $item_type;
11
12            fn next(&mut self) -> Option<Self::Item> {
13                self.iter.next()
14            }
15
16            fn size_hint(&self) -> (usize, Option<usize>) {
17                self.iter.size_hint()
18            }
19
20            fn count(self) -> usize {
21                self.iter.count()
22            }
23
24            fn last(self) -> Option<Self::Item> {
25                self.iter.last()
26            }
27
28            fn nth(&mut self, n: usize) -> Option<Self::Item> {
29                self.iter.nth(n)
30            }
31
32            fn fold<A, F>(self, init: A, function: F) -> A
33            where F: FnMut(A, Self::Item) -> A {
34                self.iter.fold(init, function)
35            }
36        }
37
38        impl<$($param)*> DoubleEndedIterator for $impl_type {
39            fn next_back(&mut self) -> Option<Self::Item> {
40                self.iter.next_back()
41            }
42        }
43
44        impl<$($param)*> ExactSizeIterator for $impl_type {
45            fn len(&self) -> usize {
46                self.iter.len()
47            }
48        }
49
50        /// This iterator's `next()` will continue to return `None` when exhausted.
51        ///
52        /// # Availability
53        ///
54        /// This trait impl requires [the `fused` feature]. However, the guarantee exists even when
55        /// the trait impl is not present.
56        ///
57        /// [the `fused` feature]: index.html#features
58        #[cfg(feature = "fused")]
59        impl<$($param)*> FusedIterator for $impl_type {}
60    };
61}
62
63/// An iterator over references to elements in a [`BoundedVecDeque`].
64///
65/// This type is returned by [`BoundedVecDeque::iter()`]. See its documentation.
66///
67/// [`BoundedVecDeque`]: struct.BoundedVecDeque.html
68/// [`BoundedVecDeque::iter()`]: struct.BoundedVecDeque.html#method.iter
69#[derive(Debug)]
70pub struct Iter<'a, T: 'a> {
71    pub(crate) iter: vec_deque::Iter<'a, T>,
72}
73
74forward_iterator_impls!(impl('a, T), Iter<'a, T>, &'a T);
75
76// `vec_deque::Iter` is `Clone` even when `T` isn't, so implement `Clone` manually instead of
77// deriving.
78impl<'a, T> Clone for Iter<'a, T> {
79    fn clone(&self) -> Self {
80        Iter {
81            iter: self.iter.clone(),
82        }
83    }
84}
85
86/// An iterator over mutable references to elements in a [`BoundedVecDeque`].
87///
88/// This type is returned by [`BoundedVecDeque::iter_mut()`]. See its documentation.
89///
90/// [`BoundedVecDeque`]: struct.BoundedVecDeque.html
91/// [`BoundedVecDeque::iter_mut()`]: struct.BoundedVecDeque.html#method.iter_mut
92#[derive(Debug)]
93pub struct IterMut<'a, T: 'a> {
94    pub(crate) iter: vec_deque::IterMut<'a, T>,
95}
96
97forward_iterator_impls!(impl('a, T: 'a), IterMut<'a, T>, &'a mut T);
98
99/// An owning iterator over elements from a [`BoundedVecDeque`].
100///
101/// This type is returned by [`BoundedVecDeque::into_iter()`]. See its documentation.
102///
103/// [`BoundedVecDeque`]: struct.BoundedVecDeque.html
104/// [`BoundedVecDeque::into_iter()`]: struct.BoundedVecDeque.html#method.into_iter
105#[derive(Debug, Clone)]
106pub struct IntoIter<T> {
107    pub(crate) iter: vec_deque::IntoIter<T>,
108}
109
110forward_iterator_impls!(impl(T), IntoIter<T>, T);
111
112/// A draining iterator over elements from a [`BoundedVecDeque`].
113///
114/// This type is returned by [`BoundedVecDeque::drain()`]. See its documentation.
115///
116/// [`BoundedVecDeque`]: struct.BoundedVecDeque.html
117/// [`BoundedVecDeque::drain()`]: struct.BoundedVecDeque.html#method.drain
118#[derive(Debug)]
119pub struct Drain<'a, T: 'a> {
120    pub(crate) iter: vec_deque::Drain<'a, T>,
121}
122
123forward_iterator_impls!(impl('a, T: 'a), Drain<'a, T>, T);
124
125/// A draining iterator over elements from a [`BoundedVecDeque`].
126///
127/// This type is returned by [`BoundedVecDeque::append()`]. See its documentation.
128///
129/// [`BoundedVecDeque`]: struct.BoundedVecDeque.html
130/// [`BoundedVecDeque::append()`]: struct.BoundedVecDeque.html#method.append
131#[derive(Debug)]
132pub struct Append<'a, T: 'a> {
133    pub(crate) source: &'a mut BoundedVecDeque<T>,
134    pub(crate) destination: &'a mut BoundedVecDeque<T>,
135}
136
137impl<'a, T: 'a> Drop for Append<'a, T> {
138    fn drop(&mut self) {
139        // Run self until the end to make sure all the values make it from source to destination.
140        while let Some(_) = self.next() {
141            continue
142        }
143    }
144}
145
146impl<'a, T: 'a> Iterator for Append<'a, T> {
147    type Item = T;
148
149    fn next(&mut self) -> Option<Self::Item> {
150        loop {
151            match self.source.pop_front() {
152                Some(value) => match self.destination.push_back(value) {
153                    Some(displaced_value) => return Some(displaced_value),
154                    None => continue,
155                },
156                None => return None,
157            }
158        }
159    }
160
161    fn size_hint(&self) -> (usize, Option<usize>) {
162        (self.len(), Some(self.len()))
163    }
164}
165
166impl<'a, T: 'a> ExactSizeIterator for Append<'a, T> {
167    fn len(&self) -> usize {
168        self.destination
169            .max_len()
170            .saturating_sub(self.destination.len())
171            .saturating_sub(self.source.len())
172    }
173}
174
175/// This iterator's `next()` will continue to return `None` when exhausted.
176///
177/// # Availability
178///
179/// This trait impl requires [the `fused` feature]. However, the guarantee exists even when the
180/// trait impl is not present.
181///
182/// [the `fused` feature]: index.html#features
183#[cfg(feature = "fused")]
184impl<'a, T: 'a> FusedIterator for Append<'a, T> {}