Skip to content

Commit b2e4e59

Browse files
committed
refactor: VecDeques Drain fields to private
1 parent 25ec827 commit b2e4e59

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

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

+15-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,21 @@ pub struct Drain<
1818
T: 'a,
1919
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
2020
> {
21-
pub(crate) after_tail: usize,
22-
pub(crate) after_head: usize,
23-
pub(crate) iter: Iter<'a, T>,
24-
pub(crate) deque: NonNull<VecDeque<T, A>>,
21+
after_tail: usize,
22+
after_head: usize,
23+
iter: Iter<'a, T>,
24+
deque: NonNull<VecDeque<T, A>>,
25+
}
26+
27+
impl<'a, T, A: Allocator> Drain<'a, T, A> {
28+
pub(super) unsafe fn new(
29+
after_tail: usize,
30+
after_head: usize,
31+
iter: Iter<'a, T>,
32+
deque: NonNull<VecDeque<T, A>>,
33+
) -> Self {
34+
Drain { after_tail, after_head, iter, deque }
35+
}
2536
}
2637

2738
#[stable(feature = "collection_debug", since = "1.17.0")]

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

+11-13
Original file line numberDiff line numberDiff line change
@@ -1269,19 +1269,17 @@ impl<T, A: Allocator> VecDeque<T, A> {
12691269
// the drain is complete and the Drain destructor is run.
12701270
self.head = drain_tail;
12711271

1272-
Drain {
1273-
deque: NonNull::from(&mut *self),
1274-
after_tail: drain_head,
1275-
after_head: head,
1276-
iter: Iter {
1277-
tail: drain_tail,
1278-
head: drain_head,
1279-
// Crucially, we only create shared references from `self` here and read from
1280-
// it. We do not write to `self` nor reborrow to a mutable reference.
1281-
// Hence the raw pointer we created above, for `deque`, remains valid.
1282-
ring: unsafe { self.buffer_as_slice() },
1283-
},
1284-
}
1272+
let deque = NonNull::from(&mut *self);
1273+
let iter = Iter {
1274+
tail: drain_tail,
1275+
head: drain_head,
1276+
// Crucially, we only create shared references from `self` here and read from
1277+
// it. We do not write to `self` nor reborrow to a mutable reference.
1278+
// Hence the raw pointer we created above, for `deque`, remains valid.
1279+
ring: unsafe { self.buffer_as_slice() },
1280+
};
1281+
1282+
unsafe { Drain::new(drain_head, head, iter, deque) }
12851283
}
12861284

12871285
/// Clears the `VecDeque`, removing all values.

0 commit comments

Comments
 (0)