rune_alloc/vec_deque/
into_iter.rs

1use core::fmt;
2use core::iter::FusedIterator;
3use core::ptr;
4
5use crate::alloc::{Allocator, Global};
6use crate::clone::TryClone;
7use crate::error::Error;
8
9use super::VecDeque;
10
11/// An owning iterator over the elements of a `VecDeque`.
12///
13/// This `struct` is created by the [`into_iter`] method on [`VecDeque`]
14/// (provided by the [`IntoIterator`] trait). See its documentation for more.
15///
16/// [`into_iter`]: VecDeque::into_iter
17pub struct IntoIter<T, A: Allocator = Global> {
18    inner: VecDeque<T, A>,
19}
20
21impl<T, A: Allocator + Clone> TryClone for IntoIter<T, A>
22where
23    T: TryClone,
24{
25    #[inline]
26    fn try_clone(&self) -> Result<Self, Error> {
27        Ok(IntoIter {
28            inner: self.inner.try_clone()?,
29        })
30    }
31
32    #[inline]
33    fn try_clone_from(&mut self, source: &Self) -> Result<(), Error> {
34        self.inner.try_clone_from(&source.inner)
35    }
36}
37
38impl<T, A: Allocator> IntoIter<T, A> {
39    pub(super) fn new(inner: VecDeque<T, A>) -> Self {
40        IntoIter { inner }
41    }
42
43    pub(super) fn into_vecdeque(self) -> VecDeque<T, A> {
44        self.inner
45    }
46}
47
48impl<T: fmt::Debug, A: Allocator> fmt::Debug for IntoIter<T, A> {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        f.debug_tuple("IntoIter").field(&self.inner).finish()
51    }
52}
53
54impl<T, A: Allocator> Iterator for IntoIter<T, A> {
55    type Item = T;
56
57    #[inline]
58    fn next(&mut self) -> Option<T> {
59        self.inner.pop_front()
60    }
61
62    #[inline]
63    fn size_hint(&self) -> (usize, Option<usize>) {
64        let len = self.inner.len();
65        (len, Some(len))
66    }
67
68    #[inline]
69    fn count(self) -> usize {
70        self.inner.len
71    }
72
73    #[inline]
74    fn fold<B, F>(mut self, mut init: B, mut f: F) -> B
75    where
76        F: FnMut(B, Self::Item) -> B,
77    {
78        struct Guard<'a, T, A: Allocator> {
79            deque: &'a mut VecDeque<T, A>,
80            // `consumed <= deque.len` always holds.
81            consumed: usize,
82        }
83
84        impl<'a, T, A: Allocator> Drop for Guard<'a, T, A> {
85            fn drop(&mut self) {
86                self.deque.len -= self.consumed;
87                self.deque.head = self.deque.to_physical_idx(self.consumed);
88            }
89        }
90
91        let mut guard = Guard {
92            deque: &mut self.inner,
93            consumed: 0,
94        };
95
96        let (head, tail) = guard.deque.as_slices();
97
98        init = head
99            .iter()
100            .map(|elem| {
101                guard.consumed += 1;
102                // SAFETY: Because we incremented `guard.consumed`, the
103                // deque effectively forgot the element, so we can take
104                // ownership
105                unsafe { ptr::read(elem) }
106            })
107            .fold(init, &mut f);
108
109        tail.iter()
110            .map(|elem| {
111                guard.consumed += 1;
112                // SAFETY: Same as above.
113                unsafe { ptr::read(elem) }
114            })
115            .fold(init, &mut f)
116    }
117
118    #[inline]
119    fn last(mut self) -> Option<Self::Item> {
120        self.inner.pop_back()
121    }
122}
123
124impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> {
125    #[inline]
126    fn next_back(&mut self) -> Option<T> {
127        self.inner.pop_back()
128    }
129
130    #[inline]
131    fn rfold<B, F>(mut self, mut init: B, mut f: F) -> B
132    where
133        F: FnMut(B, Self::Item) -> B,
134    {
135        struct Guard<'a, T, A: Allocator> {
136            deque: &'a mut VecDeque<T, A>,
137            // `consumed <= deque.len` always holds.
138            consumed: usize,
139        }
140
141        impl<'a, T, A: Allocator> Drop for Guard<'a, T, A> {
142            fn drop(&mut self) {
143                self.deque.len -= self.consumed;
144            }
145        }
146
147        let mut guard = Guard {
148            deque: &mut self.inner,
149            consumed: 0,
150        };
151
152        let (head, tail) = guard.deque.as_slices();
153
154        init = tail
155            .iter()
156            .map(|elem| {
157                guard.consumed += 1;
158                // SAFETY: See `try_fold`'s safety comment.
159                unsafe { ptr::read(elem) }
160            })
161            .fold(init, &mut f);
162
163        head.iter()
164            .map(|elem| {
165                guard.consumed += 1;
166                // SAFETY: Same as above.
167                unsafe { ptr::read(elem) }
168            })
169            .fold(init, &mut f)
170    }
171}
172
173impl<T, A: Allocator> ExactSizeIterator for IntoIter<T, A> {
174    #[inline]
175    fn len(&self) -> usize {
176        self.inner.len()
177    }
178}
179
180impl<T, A: Allocator> FusedIterator for IntoIter<T, A> {}