rune_alloc/vec_deque/
iter.rs

1use core::fmt;
2use core::iter::FusedIterator;
3use core::mem;
4use core::slice;
5
6/// An iterator over the elements of a `VecDeque`.
7///
8/// This `struct` is created by the [`iter`] method on [`super::VecDeque`]. See its
9/// documentation for more.
10///
11/// [`iter`]: super::VecDeque::iter
12pub struct Iter<'a, T: 'a> {
13    i1: slice::Iter<'a, T>,
14    i2: slice::Iter<'a, T>,
15}
16
17impl<'a, T> Iter<'a, T> {
18    pub(super) fn new(i1: slice::Iter<'a, T>, i2: slice::Iter<'a, T>) -> Self {
19        Self { i1, i2 }
20    }
21}
22
23impl<T: fmt::Debug> fmt::Debug for Iter<'_, T> {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        f.debug_tuple("Iter")
26            .field(&self.i1.as_slice())
27            .field(&self.i2.as_slice())
28            .finish()
29    }
30}
31
32// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
33impl<T> Clone for Iter<'_, T> {
34    fn clone(&self) -> Self {
35        Iter {
36            i1: self.i1.clone(),
37            i2: self.i2.clone(),
38        }
39    }
40}
41
42impl<'a, T> Iterator for Iter<'a, T> {
43    type Item = &'a T;
44
45    #[inline]
46    fn next(&mut self) -> Option<&'a T> {
47        match self.i1.next() {
48            Some(val) => Some(val),
49            None => {
50                // most of the time, the iterator will either always
51                // call next(), or always call next_back(). By swapping
52                // the iterators once the first one is empty, we ensure
53                // that the first branch is taken as often as possible,
54                // without sacrificing correctness, as i1 is empty anyways
55                mem::swap(&mut self.i1, &mut self.i2);
56                self.i1.next()
57            }
58        }
59    }
60
61    #[inline]
62    fn size_hint(&self) -> (usize, Option<usize>) {
63        let len = self.len();
64        (len, Some(len))
65    }
66
67    fn fold<Acc, F>(self, accum: Acc, mut f: F) -> Acc
68    where
69        F: FnMut(Acc, Self::Item) -> Acc,
70    {
71        let accum = self.i1.fold(accum, &mut f);
72        self.i2.fold(accum, &mut f)
73    }
74
75    #[inline]
76    fn last(mut self) -> Option<&'a T> {
77        self.next_back()
78    }
79}
80
81impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
82    #[inline]
83    fn next_back(&mut self) -> Option<&'a T> {
84        match self.i2.next_back() {
85            Some(val) => Some(val),
86            None => {
87                // most of the time, the iterator will either always
88                // call next(), or always call next_back(). By swapping
89                // the iterators once the second one is empty, we ensure
90                // that the first branch is taken as often as possible,
91                // without sacrificing correctness, as i2 is empty anyways
92                mem::swap(&mut self.i1, &mut self.i2);
93                self.i2.next_back()
94            }
95        }
96    }
97
98    fn rfold<Acc, F>(self, accum: Acc, mut f: F) -> Acc
99    where
100        F: FnMut(Acc, Self::Item) -> Acc,
101    {
102        let accum = self.i2.rfold(accum, &mut f);
103        self.i1.rfold(accum, &mut f)
104    }
105}
106
107impl<T> ExactSizeIterator for Iter<'_, T> {
108    fn len(&self) -> usize {
109        self.i1.len() + self.i2.len()
110    }
111}
112
113impl<T> FusedIterator for Iter<'_, T> {}