1 use core::iter::FusedIterator;
2 use core::ptr::{self, NonNull};
5 use crate::alloc::{Allocator, Global};
7 use super::{count, Iter, VecDeque};
9 /// A draining iterator over the elements of a `VecDeque`.
11 /// This `struct` is created by the [`drain`] method on [`VecDeque`]. See its
12 /// documentation for more.
14 /// [`drain`]: VecDeque::drain
15 #[stable(feature = "drain", since = "1.6.0")]
19 #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
24 deque: NonNull<VecDeque<T, A>>,
27 impl<'a, T, A: Allocator> Drain<'a, T, A> {
28 pub(super) unsafe fn new(
32 deque: NonNull<VecDeque<T, A>>,
34 Drain { after_tail, after_head, iter, deque }
38 #[stable(feature = "collection_debug", since = "1.17.0")]
39 impl<T: fmt::Debug, A: Allocator> fmt::Debug for Drain<'_, T, A> {
40 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41 f.debug_tuple("Drain")
42 .field(&self.after_tail)
43 .field(&self.after_head)
49 #[stable(feature = "drain", since = "1.6.0")]
50 unsafe impl<T: Sync, A: Allocator + Sync> Sync for Drain<'_, T, A> {}
51 #[stable(feature = "drain", since = "1.6.0")]
52 unsafe impl<T: Send, A: Allocator + Send> Send for Drain<'_, T, A> {}
54 #[stable(feature = "drain", since = "1.6.0")]
55 impl<T, A: Allocator> Drop for Drain<'_, T, A> {
57 struct DropGuard<'r, 'a, T, A: Allocator>(&'r mut Drain<'a, T, A>);
59 impl<'r, 'a, T, A: Allocator> Drop for DropGuard<'r, 'a, T, A> {
61 self.0.for_each(drop);
63 let source_deque = unsafe { self.0.deque.as_mut() };
65 // T = source_deque_tail; H = source_deque_head; t = drain_tail; h = drain_head
68 // [. . . o o x x o o . . .]
70 let orig_tail = source_deque.tail;
71 let drain_tail = source_deque.head;
72 let drain_head = self.0.after_tail;
73 let orig_head = self.0.after_head;
75 let tail_len = count(orig_tail, drain_tail, source_deque.cap());
76 let head_len = count(drain_head, orig_head, source_deque.cap());
78 // Restore the original head value
79 source_deque.head = orig_head;
81 match (tail_len, head_len) {
83 source_deque.head = 0;
84 source_deque.tail = 0;
87 source_deque.tail = drain_head;
90 source_deque.head = drain_tail;
93 if tail_len <= head_len {
94 source_deque.tail = source_deque.wrap_sub(drain_head, tail_len);
95 source_deque.wrap_copy(source_deque.tail, orig_tail, tail_len);
97 source_deque.head = source_deque.wrap_add(drain_tail, head_len);
98 source_deque.wrap_copy(drain_tail, drain_head, head_len);
105 while let Some(item) = self.next() {
106 let guard = DropGuard(self);
115 #[stable(feature = "drain", since = "1.6.0")]
116 impl<T, A: Allocator> Iterator for Drain<'_, T, A> {
120 fn next(&mut self) -> Option<T> {
121 self.iter.next().map(|elt| unsafe { ptr::read(elt) })
125 fn size_hint(&self) -> (usize, Option<usize>) {
126 self.iter.size_hint()
130 #[stable(feature = "drain", since = "1.6.0")]
131 impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A> {
133 fn next_back(&mut self) -> Option<T> {
134 self.iter.next_back().map(|elt| unsafe { ptr::read(elt) })
138 #[stable(feature = "drain", since = "1.6.0")]
139 impl<T, A: Allocator> ExactSizeIterator for Drain<'_, T, A> {}
141 #[stable(feature = "fused", since = "1.26.0")]
142 impl<T, A: Allocator> FusedIterator for Drain<'_, T, A> {}