Skip to content

Commit 54684c4

Browse files
committed
Alternate approach; just modify Drain
1 parent 25f4cb5 commit 54684c4

File tree

3 files changed

+59
-52
lines changed

3 files changed

+59
-52
lines changed

library/alloc/src/collections/vec_deque/drain.rs

+35-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use core::fmt;
12
use core::iter::FusedIterator;
3+
use core::marker::PhantomData;
4+
use core::mem::{self, MaybeUninit};
25
use core::ptr::{self, NonNull};
3-
use core::{fmt, mem};
46

57
use crate::alloc::{Allocator, Global};
68

7-
use super::{count, Iter, VecDeque};
9+
use super::{count, wrap_index, VecDeque};
810

911
/// A draining iterator over the elements of a `VecDeque`.
1012
///
@@ -20,18 +22,24 @@ pub struct Drain<
2022
> {
2123
after_tail: usize,
2224
after_head: usize,
23-
iter: Iter<'a, T>,
25+
ring: NonNull<[T]>,
26+
tail: usize,
27+
head: usize,
2428
deque: NonNull<VecDeque<T, A>>,
29+
_phantom: PhantomData<&'a T>,
2530
}
2631

2732
impl<'a, T, A: Allocator> Drain<'a, T, A> {
2833
pub(super) unsafe fn new(
2934
after_tail: usize,
3035
after_head: usize,
31-
iter: Iter<'a, T>,
36+
ring: &'a [MaybeUninit<T>],
37+
tail: usize,
38+
head: usize,
3239
deque: NonNull<VecDeque<T, A>>,
3340
) -> Self {
34-
Drain { after_tail, after_head, iter, deque }
41+
let ring = unsafe { NonNull::new_unchecked(ring as *const [MaybeUninit<T>] as *mut _) };
42+
Drain { after_tail, after_head, ring, tail, head, deque, _phantom: PhantomData }
3543
}
3644
}
3745

@@ -41,7 +49,9 @@ impl<T: fmt::Debug, A: Allocator> fmt::Debug for Drain<'_, T, A> {
4149
f.debug_tuple("Drain")
4250
.field(&self.after_tail)
4351
.field(&self.after_head)
44-
.field(&self.iter)
52+
.field(&self.ring)
53+
.field(&self.tail)
54+
.field(&self.head)
4555
.finish()
4656
}
4757
}
@@ -118,20 +128,36 @@ impl<T, A: Allocator> Iterator for Drain<'_, T, A> {
118128

119129
#[inline]
120130
fn next(&mut self) -> Option<T> {
121-
self.iter.next().map(|elt| unsafe { ptr::read(elt) })
131+
if self.tail == self.head {
132+
return None;
133+
}
134+
let tail = self.tail;
135+
self.tail = wrap_index(self.tail.wrapping_add(1), self.ring.len());
136+
// Safety:
137+
// - `self.tail` in a ring buffer is always a valid index.
138+
// - `self.head` and `self.tail` equality is checked above.
139+
unsafe { Some(ptr::read(self.ring.as_ptr().get_unchecked_mut(tail))) }
122140
}
123141

124142
#[inline]
125143
fn size_hint(&self) -> (usize, Option<usize>) {
126-
self.iter.size_hint()
144+
let len = count(self.tail, self.head, self.ring.len());
145+
(len, Some(len))
127146
}
128147
}
129148

130149
#[stable(feature = "drain", since = "1.6.0")]
131150
impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A> {
132151
#[inline]
133152
fn next_back(&mut self) -> Option<T> {
134-
self.iter.next_back().map(|elt| unsafe { ptr::read(elt) })
153+
if self.tail == self.head {
154+
return None;
155+
}
156+
self.head = wrap_index(self.head.wrapping_sub(1), self.ring.len());
157+
// Safety:
158+
// - `self.head` in a ring buffer is always a valid index.
159+
// - `self.head` and `self.tail` equality is checked above.
160+
unsafe { Some(ptr::read(self.ring.as_ptr().get_unchecked_mut(self.head))) }
135161
}
136162
}
137163

library/alloc/src/collections/vec_deque/iter.rs

+23-41
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use core::fmt;
22
use core::iter::{FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce};
3-
use core::marker::PhantomData;
43
use core::mem::MaybeUninit;
54
use core::ops::Try;
6-
use core::ptr::NonNull;
75

86
use super::{count, wrap_index, RingSlices};
97

@@ -15,53 +13,38 @@ use super::{count, wrap_index, RingSlices};
1513
/// [`iter`]: super::VecDeque::iter
1614
#[stable(feature = "rust1", since = "1.0.0")]
1715
pub struct Iter<'a, T: 'a> {
18-
ring: NonNull<[T]>,
16+
ring: &'a [MaybeUninit<T>],
1917
tail: usize,
2018
head: usize,
21-
_marker: PhantomData<&'a T>,
2219
}
2320

2421
impl<'a, T> Iter<'a, T> {
2522
pub(super) fn new(ring: &'a [MaybeUninit<T>], tail: usize, head: usize) -> Self {
26-
Iter {
27-
ring: unsafe { NonNull::new_unchecked(ring as *const [MaybeUninit<T>] as *mut _) },
28-
tail,
29-
head,
30-
_marker: PhantomData,
31-
}
32-
}
33-
34-
unsafe fn ring(&self) -> &'a [MaybeUninit<T>] {
35-
unsafe {
36-
core::slice::from_raw_parts(
37-
self.ring.as_ptr() as *const MaybeUninit<T>,
38-
self.ring.len(),
39-
)
40-
}
23+
Iter { ring, tail, head }
4124
}
4225
}
4326

44-
#[stable(feature = "rust1", since = "1.0.0")]
45-
unsafe impl<T: Sync> Sync for Iter<'_, T> {}
46-
#[stable(feature = "rust1", since = "1.0.0")]
47-
unsafe impl<T: Sync> Send for Iter<'_, T> {}
48-
4927
#[stable(feature = "collection_debug", since = "1.17.0")]
5028
impl<T: fmt::Debug> fmt::Debug for Iter<'_, T> {
5129
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52-
let (front, back) = unsafe { RingSlices::ring_slices(self.ring(), self.head, self.tail) };
30+
let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail);
5331
// Safety:
5432
// - `self.head` and `self.tail` in a ring buffer are always valid indices.
5533
// - `RingSlices::ring_slices` guarantees that the slices split according to `self.head` and `self.tail` are initialized.
56-
f.debug_tuple("Iter").field(&front).field(&back).finish()
34+
unsafe {
35+
f.debug_tuple("Iter")
36+
.field(&MaybeUninit::slice_assume_init_ref(front))
37+
.field(&MaybeUninit::slice_assume_init_ref(back))
38+
.finish()
39+
}
5740
}
5841
}
5942

6043
// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
6144
#[stable(feature = "rust1", since = "1.0.0")]
6245
impl<T> Clone for Iter<'_, T> {
6346
fn clone(&self) -> Self {
64-
Iter { ring: self.ring, tail: self.tail, head: self.head, _marker: PhantomData }
47+
Iter { ring: self.ring, tail: self.tail, head: self.head }
6548
}
6649
}
6750

@@ -79,7 +62,7 @@ impl<'a, T> Iterator for Iter<'a, T> {
7962
// Safety:
8063
// - `self.tail` in a ring buffer is always a valid index.
8164
// - `self.head` and `self.tail` equality is checked above.
82-
unsafe { Some(self.ring().get_unchecked(tail).assume_init_ref()) }
65+
unsafe { Some(self.ring.get_unchecked(tail).assume_init_ref()) }
8366
}
8467

8568
#[inline]
@@ -92,11 +75,11 @@ impl<'a, T> Iterator for Iter<'a, T> {
9275
where
9376
F: FnMut(Acc, Self::Item) -> Acc,
9477
{
78+
let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail);
9579
// Safety:
9680
// - `self.head` and `self.tail` in a ring buffer are always valid indices.
9781
// - `RingSlices::ring_slices` guarantees that the slices split according to `self.head` and `self.tail` are initialized.
9882
unsafe {
99-
let (front, back) = RingSlices::ring_slices(self.ring(), self.head, self.tail);
10083
accum = MaybeUninit::slice_assume_init_ref(front).iter().fold(accum, &mut f);
10184
MaybeUninit::slice_assume_init_ref(back).iter().fold(accum, &mut f)
10285
}
@@ -111,13 +94,12 @@ impl<'a, T> Iterator for Iter<'a, T> {
11194
let (mut iter, final_res);
11295
if self.tail <= self.head {
11396
// Safety: single slice self.ring[self.tail..self.head] is initialized.
114-
iter =
115-
unsafe { MaybeUninit::slice_assume_init_ref(&self.ring()[self.tail..self.head]) }
116-
.iter();
97+
iter = unsafe { MaybeUninit::slice_assume_init_ref(&self.ring[self.tail..self.head]) }
98+
.iter();
11799
final_res = iter.try_fold(init, &mut f);
118100
} else {
119-
// Safety: two slices: self.ring()[self.tail..], self.ring()[..self.head] both are initialized.
120-
let (front, back) = unsafe { self.ring().split_at(self.tail) };
101+
// Safety: two slices: self.ring[self.tail..], self.ring[..self.head] both are initialized.
102+
let (front, back) = self.ring.split_at(self.tail);
121103

122104
let mut back_iter = unsafe { MaybeUninit::slice_assume_init_ref(back).iter() };
123105
let res = back_iter.try_fold(init, &mut f);
@@ -151,7 +133,7 @@ impl<'a, T> Iterator for Iter<'a, T> {
151133
// that is in bounds.
152134
unsafe {
153135
let idx = wrap_index(self.tail.wrapping_add(idx), self.ring.len());
154-
self.ring().get_unchecked(idx).assume_init_ref()
136+
self.ring.get_unchecked(idx).assume_init_ref()
155137
}
156138
}
157139
}
@@ -167,18 +149,18 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
167149
// Safety:
168150
// - `self.head` in a ring buffer is always a valid index.
169151
// - `self.head` and `self.tail` equality is checked above.
170-
unsafe { Some(self.ring().get_unchecked(self.head).assume_init_ref()) }
152+
unsafe { Some(self.ring.get_unchecked(self.head).assume_init_ref()) }
171153
}
172154

173155
fn rfold<Acc, F>(self, mut accum: Acc, mut f: F) -> Acc
174156
where
175157
F: FnMut(Acc, Self::Item) -> Acc,
176158
{
159+
let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail);
177160
// Safety:
178161
// - `self.head` and `self.tail` in a ring buffer are always valid indices.
179162
// - `RingSlices::ring_slices` guarantees that the slices split according to `self.head` and `self.tail` are initialized.
180163
unsafe {
181-
let (front, back) = RingSlices::ring_slices(self.ring(), self.head, self.tail);
182164
accum = MaybeUninit::slice_assume_init_ref(back).iter().rfold(accum, &mut f);
183165
MaybeUninit::slice_assume_init_ref(front).iter().rfold(accum, &mut f)
184166
}
@@ -192,14 +174,14 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
192174
{
193175
let (mut iter, final_res);
194176
if self.tail <= self.head {
195-
// Safety: single slice self.ring()[self.tail..self.head] is initialized.
177+
// Safety: single slice self.ring[self.tail..self.head] is initialized.
196178
iter = unsafe {
197-
MaybeUninit::slice_assume_init_ref(&self.ring()[self.tail..self.head]).iter()
179+
MaybeUninit::slice_assume_init_ref(&self.ring[self.tail..self.head]).iter()
198180
};
199181
final_res = iter.try_rfold(init, &mut f);
200182
} else {
201-
// Safety: two slices: self.ring()[self.tail..], self.ring()[..self.head] both are initialized.
202-
let (front, back) = unsafe { self.ring().split_at(self.tail) };
183+
// Safety: two slices: self.ring[self.tail..], self.ring[..self.head] both are initialized.
184+
let (front, back) = self.ring.split_at(self.tail);
203185

204186
let mut front_iter =
205187
unsafe { MaybeUninit::slice_assume_init_ref(&front[..self.head]).iter() };

library/alloc/src/collections/vec_deque/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1334,9 +1334,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
13341334
// it. We do not write to `self` nor reborrow to a mutable reference.
13351335
// Hence the raw pointer we created above, for `deque`, remains valid.
13361336
let ring = self.buffer_as_slice();
1337-
let iter = Iter::new(ring, drain_tail, drain_head);
13381337

1339-
Drain::new(drain_head, head, iter, deque)
1338+
Drain::new(drain_head, head, ring, drain_tail, drain_head, deque)
13401339
}
13411340
}
13421341

0 commit comments

Comments
 (0)