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
11pub 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
44impl<T: fmt::Debug, A: Allocator> fmt::Debug for IntoIter<T, A> {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 f.debug_tuple("IntoIter").field(&self.inner).finish()
47 }
48}
49
50impl<T, A: Allocator> Iterator for IntoIter<T, A> {
51 type Item = T;
52
53 #[inline]
54 fn next(&mut self) -> Option<T> {
55 self.inner.pop_front()
56 }
57
58 #[inline]
59 fn size_hint(&self) -> (usize, Option<usize>) {
60 let len = self.inner.len();
61 (len, Some(len))
62 }
63
64 #[inline]
65 fn count(self) -> usize {
66 self.inner.len
67 }
68
69 #[inline]
70 fn fold<B, F>(mut self, mut init: B, mut f: F) -> B
71 where
72 F: FnMut(B, Self::Item) -> B,
73 {
74 struct Guard<'a, T, A: Allocator> {
75 deque: &'a mut VecDeque<T, A>,
76 consumed: usize,
78 }
79
80 impl<T, A: Allocator> Drop for Guard<'_, T, A> {
81 fn drop(&mut self) {
82 self.deque.len -= self.consumed;
83 self.deque.head = self.deque.to_physical_idx(self.consumed);
84 }
85 }
86
87 let mut guard = Guard {
88 deque: &mut self.inner,
89 consumed: 0,
90 };
91
92 let (head, tail) = guard.deque.as_slices();
93
94 init = head
95 .iter()
96 .map(|elem| {
97 guard.consumed += 1;
98 unsafe { ptr::read(elem) }
102 })
103 .fold(init, &mut f);
104
105 tail.iter()
106 .map(|elem| {
107 guard.consumed += 1;
108 unsafe { ptr::read(elem) }
110 })
111 .fold(init, &mut f)
112 }
113
114 #[inline]
115 fn last(mut self) -> Option<Self::Item> {
116 self.inner.pop_back()
117 }
118}
119
120impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> {
121 #[inline]
122 fn next_back(&mut self) -> Option<T> {
123 self.inner.pop_back()
124 }
125
126 #[inline]
127 fn rfold<B, F>(mut self, mut init: B, mut f: F) -> B
128 where
129 F: FnMut(B, Self::Item) -> B,
130 {
131 struct Guard<'a, T, A: Allocator> {
132 deque: &'a mut VecDeque<T, A>,
133 consumed: usize,
135 }
136
137 impl<T, A: Allocator> Drop for Guard<'_, T, A> {
138 fn drop(&mut self) {
139 self.deque.len -= self.consumed;
140 }
141 }
142
143 let mut guard = Guard {
144 deque: &mut self.inner,
145 consumed: 0,
146 };
147
148 let (head, tail) = guard.deque.as_slices();
149
150 init = tail
151 .iter()
152 .map(|elem| {
153 guard.consumed += 1;
154 unsafe { ptr::read(elem) }
156 })
157 .fold(init, &mut f);
158
159 head.iter()
160 .map(|elem| {
161 guard.consumed += 1;
162 unsafe { ptr::read(elem) }
164 })
165 .fold(init, &mut f)
166 }
167}
168
169impl<T, A: Allocator> ExactSizeIterator for IntoIter<T, A> {
170 #[inline]
171 fn len(&self) -> usize {
172 self.inner.len()
173 }
174}
175
176impl<T, A: Allocator> FusedIterator for IntoIter<T, A> {}