rune_alloc/vec_deque/
iter_mut.rs

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