Skip to content

Commit 75f721d

Browse files
Move VecDeque Drain iterator to new file
1 parent 1f373f4 commit 75f721d

File tree

2 files changed

+131
-121
lines changed

2 files changed

+131
-121
lines changed

src/liballoc/collections/vec_deque.rs

Lines changed: 5 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ use crate::collections::TryReserveError;
2222
use crate::raw_vec::RawVec;
2323
use crate::vec::Vec;
2424

25+
#[stable(feature = "drain", since = "1.6.0")]
26+
pub use self::drain::Drain;
27+
28+
mod drain;
29+
2530
#[cfg(test)]
2631
mod tests;
2732

@@ -2541,127 +2546,6 @@ impl<T> ExactSizeIterator for IntoIter<T> {
25412546
#[stable(feature = "fused", since = "1.26.0")]
25422547
impl<T> FusedIterator for IntoIter<T> {}
25432548

2544-
/// A draining iterator over the elements of a `VecDeque`.
2545-
///
2546-
/// This `struct` is created by the [`drain`] method on [`VecDeque`]. See its
2547-
/// documentation for more.
2548-
///
2549-
/// [`drain`]: struct.VecDeque.html#method.drain
2550-
/// [`VecDeque`]: struct.VecDeque.html
2551-
#[stable(feature = "drain", since = "1.6.0")]
2552-
pub struct Drain<'a, T: 'a> {
2553-
after_tail: usize,
2554-
after_head: usize,
2555-
iter: Iter<'a, T>,
2556-
deque: NonNull<VecDeque<T>>,
2557-
}
2558-
2559-
#[stable(feature = "collection_debug", since = "1.17.0")]
2560-
impl<T: fmt::Debug> fmt::Debug for Drain<'_, T> {
2561-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2562-
f.debug_tuple("Drain")
2563-
.field(&self.after_tail)
2564-
.field(&self.after_head)
2565-
.field(&self.iter)
2566-
.finish()
2567-
}
2568-
}
2569-
2570-
#[stable(feature = "drain", since = "1.6.0")]
2571-
unsafe impl<T: Sync> Sync for Drain<'_, T> {}
2572-
#[stable(feature = "drain", since = "1.6.0")]
2573-
unsafe impl<T: Send> Send for Drain<'_, T> {}
2574-
2575-
#[stable(feature = "drain", since = "1.6.0")]
2576-
impl<T> Drop for Drain<'_, T> {
2577-
fn drop(&mut self) {
2578-
struct DropGuard<'r, 'a, T>(&'r mut Drain<'a, T>);
2579-
2580-
impl<'r, 'a, T> Drop for DropGuard<'r, 'a, T> {
2581-
fn drop(&mut self) {
2582-
self.0.for_each(drop);
2583-
2584-
let source_deque = unsafe { self.0.deque.as_mut() };
2585-
2586-
// T = source_deque_tail; H = source_deque_head; t = drain_tail; h = drain_head
2587-
//
2588-
// T t h H
2589-
// [. . . o o x x o o . . .]
2590-
//
2591-
let orig_tail = source_deque.tail;
2592-
let drain_tail = source_deque.head;
2593-
let drain_head = self.0.after_tail;
2594-
let orig_head = self.0.after_head;
2595-
2596-
let tail_len = count(orig_tail, drain_tail, source_deque.cap());
2597-
let head_len = count(drain_head, orig_head, source_deque.cap());
2598-
2599-
// Restore the original head value
2600-
source_deque.head = orig_head;
2601-
2602-
match (tail_len, head_len) {
2603-
(0, 0) => {
2604-
source_deque.head = 0;
2605-
source_deque.tail = 0;
2606-
}
2607-
(0, _) => {
2608-
source_deque.tail = drain_head;
2609-
}
2610-
(_, 0) => {
2611-
source_deque.head = drain_tail;
2612-
}
2613-
_ => unsafe {
2614-
if tail_len <= head_len {
2615-
source_deque.tail = source_deque.wrap_sub(drain_head, tail_len);
2616-
source_deque.wrap_copy(source_deque.tail, orig_tail, tail_len);
2617-
} else {
2618-
source_deque.head = source_deque.wrap_add(drain_tail, head_len);
2619-
source_deque.wrap_copy(drain_tail, drain_head, head_len);
2620-
}
2621-
},
2622-
}
2623-
}
2624-
}
2625-
2626-
while let Some(item) = self.next() {
2627-
let guard = DropGuard(self);
2628-
drop(item);
2629-
mem::forget(guard);
2630-
}
2631-
2632-
DropGuard(self);
2633-
}
2634-
}
2635-
2636-
#[stable(feature = "drain", since = "1.6.0")]
2637-
impl<T> Iterator for Drain<'_, T> {
2638-
type Item = T;
2639-
2640-
#[inline]
2641-
fn next(&mut self) -> Option<T> {
2642-
self.iter.next().map(|elt| unsafe { ptr::read(elt) })
2643-
}
2644-
2645-
#[inline]
2646-
fn size_hint(&self) -> (usize, Option<usize>) {
2647-
self.iter.size_hint()
2648-
}
2649-
}
2650-
2651-
#[stable(feature = "drain", since = "1.6.0")]
2652-
impl<T> DoubleEndedIterator for Drain<'_, T> {
2653-
#[inline]
2654-
fn next_back(&mut self) -> Option<T> {
2655-
self.iter.next_back().map(|elt| unsafe { ptr::read(elt) })
2656-
}
2657-
}
2658-
2659-
#[stable(feature = "drain", since = "1.6.0")]
2660-
impl<T> ExactSizeIterator for Drain<'_, T> {}
2661-
2662-
#[stable(feature = "fused", since = "1.26.0")]
2663-
impl<T> FusedIterator for Drain<'_, T> {}
2664-
26652549
#[stable(feature = "rust1", since = "1.0.0")]
26662550
impl<A: PartialEq> PartialEq for VecDeque<A> {
26672551
fn eq(&self, other: &VecDeque<A>) -> bool {
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
use core::iter::FusedIterator;
2+
use core::ptr::{self, NonNull};
3+
use core::{fmt, mem};
4+
5+
use super::{count, Iter, VecDeque};
6+
7+
/// A draining iterator over the elements of a `VecDeque`.
8+
///
9+
/// This `struct` is created by the [`drain`] method on [`VecDeque`]. See its
10+
/// documentation for more.
11+
///
12+
/// [`drain`]: struct.VecDeque.html#method.drain
13+
/// [`VecDeque`]: struct.VecDeque.html
14+
#[stable(feature = "drain", since = "1.6.0")]
15+
pub struct Drain<'a, T: 'a> {
16+
pub(crate) after_tail: usize,
17+
pub(crate) after_head: usize,
18+
pub(crate) iter: Iter<'a, T>,
19+
pub(crate) deque: NonNull<VecDeque<T>>,
20+
}
21+
22+
#[stable(feature = "collection_debug", since = "1.17.0")]
23+
impl<T: fmt::Debug> fmt::Debug for Drain<'_, T> {
24+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25+
f.debug_tuple("Drain")
26+
.field(&self.after_tail)
27+
.field(&self.after_head)
28+
.field(&self.iter)
29+
.finish()
30+
}
31+
}
32+
33+
#[stable(feature = "drain", since = "1.6.0")]
34+
unsafe impl<T: Sync> Sync for Drain<'_, T> {}
35+
#[stable(feature = "drain", since = "1.6.0")]
36+
unsafe impl<T: Send> Send for Drain<'_, T> {}
37+
38+
#[stable(feature = "drain", since = "1.6.0")]
39+
impl<T> Drop for Drain<'_, T> {
40+
fn drop(&mut self) {
41+
struct DropGuard<'r, 'a, T>(&'r mut Drain<'a, T>);
42+
43+
impl<'r, 'a, T> Drop for DropGuard<'r, 'a, T> {
44+
fn drop(&mut self) {
45+
self.0.for_each(drop);
46+
47+
let source_deque = unsafe { self.0.deque.as_mut() };
48+
49+
// T = source_deque_tail; H = source_deque_head; t = drain_tail; h = drain_head
50+
//
51+
// T t h H
52+
// [. . . o o x x o o . . .]
53+
//
54+
let orig_tail = source_deque.tail;
55+
let drain_tail = source_deque.head;
56+
let drain_head = self.0.after_tail;
57+
let orig_head = self.0.after_head;
58+
59+
let tail_len = count(orig_tail, drain_tail, source_deque.cap());
60+
let head_len = count(drain_head, orig_head, source_deque.cap());
61+
62+
// Restore the original head value
63+
source_deque.head = orig_head;
64+
65+
match (tail_len, head_len) {
66+
(0, 0) => {
67+
source_deque.head = 0;
68+
source_deque.tail = 0;
69+
}
70+
(0, _) => {
71+
source_deque.tail = drain_head;
72+
}
73+
(_, 0) => {
74+
source_deque.head = drain_tail;
75+
}
76+
_ => unsafe {
77+
if tail_len <= head_len {
78+
source_deque.tail = source_deque.wrap_sub(drain_head, tail_len);
79+
source_deque.wrap_copy(source_deque.tail, orig_tail, tail_len);
80+
} else {
81+
source_deque.head = source_deque.wrap_add(drain_tail, head_len);
82+
source_deque.wrap_copy(drain_tail, drain_head, head_len);
83+
}
84+
},
85+
}
86+
}
87+
}
88+
89+
while let Some(item) = self.next() {
90+
let guard = DropGuard(self);
91+
drop(item);
92+
mem::forget(guard);
93+
}
94+
95+
DropGuard(self);
96+
}
97+
}
98+
99+
#[stable(feature = "drain", since = "1.6.0")]
100+
impl<T> Iterator for Drain<'_, T> {
101+
type Item = T;
102+
103+
#[inline]
104+
fn next(&mut self) -> Option<T> {
105+
self.iter.next().map(|elt| unsafe { ptr::read(elt) })
106+
}
107+
108+
#[inline]
109+
fn size_hint(&self) -> (usize, Option<usize>) {
110+
self.iter.size_hint()
111+
}
112+
}
113+
114+
#[stable(feature = "drain", since = "1.6.0")]
115+
impl<T> DoubleEndedIterator for Drain<'_, T> {
116+
#[inline]
117+
fn next_back(&mut self) -> Option<T> {
118+
self.iter.next_back().map(|elt| unsafe { ptr::read(elt) })
119+
}
120+
}
121+
122+
#[stable(feature = "drain", since = "1.6.0")]
123+
impl<T> ExactSizeIterator for Drain<'_, T> {}
124+
125+
#[stable(feature = "fused", since = "1.26.0")]
126+
impl<T> FusedIterator for Drain<'_, T> {}

0 commit comments

Comments
 (0)