Skip to content

Commit

Permalink
refactor: VecDeques Drain fields to private
Browse files Browse the repository at this point in the history
  • Loading branch information
DeveloperC286 committed Oct 5, 2021
1 parent 25ec827 commit b2e4e59
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
19 changes: 15 additions & 4 deletions library/alloc/src/collections/vec_deque/drain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,21 @@ pub struct Drain<
T: 'a,
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
> {
pub(crate) after_tail: usize,
pub(crate) after_head: usize,
pub(crate) iter: Iter<'a, T>,
pub(crate) deque: NonNull<VecDeque<T, A>>,
after_tail: usize,
after_head: usize,
iter: Iter<'a, T>,
deque: NonNull<VecDeque<T, A>>,
}

impl<'a, T, A: Allocator> Drain<'a, T, A> {
pub(super) unsafe fn new(
after_tail: usize,
after_head: usize,
iter: Iter<'a, T>,
deque: NonNull<VecDeque<T, A>>,
) -> Self {
Drain { after_tail, after_head, iter, deque }
}
}

#[stable(feature = "collection_debug", since = "1.17.0")]
Expand Down
24 changes: 11 additions & 13 deletions library/alloc/src/collections/vec_deque/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1269,19 +1269,17 @@ impl<T, A: Allocator> VecDeque<T, A> {
// the drain is complete and the Drain destructor is run.
self.head = drain_tail;

Drain {
deque: NonNull::from(&mut *self),
after_tail: drain_head,
after_head: head,
iter: Iter {
tail: drain_tail,
head: drain_head,
// Crucially, we only create shared references from `self` here and read from
// it. We do not write to `self` nor reborrow to a mutable reference.
// Hence the raw pointer we created above, for `deque`, remains valid.
ring: unsafe { self.buffer_as_slice() },
},
}
let deque = NonNull::from(&mut *self);
let iter = Iter {
tail: drain_tail,
head: drain_head,
// Crucially, we only create shared references from `self` here and read from
// it. We do not write to `self` nor reborrow to a mutable reference.
// Hence the raw pointer we created above, for `deque`, remains valid.
ring: unsafe { self.buffer_as_slice() },
};

unsafe { Drain::new(drain_head, head, iter, deque) }
}

/// Clears the `VecDeque`, removing all values.
Expand Down

0 comments on commit b2e4e59

Please sign in to comment.