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