rune_alloc/vec_deque/
raw_iter.rs

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