Skip to content

refactor: VecDeques Iter fields to private #89685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions library/alloc/src/collections/vec_deque/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ use super::{count, wrap_index, RingSlices};
/// [`iter`]: super::VecDeque::iter
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Iter<'a, T: 'a> {
pub(crate) ring: &'a [MaybeUninit<T>],
pub(crate) tail: usize,
pub(crate) head: usize,
ring: &'a [MaybeUninit<T>],
tail: usize,
head: usize,
}

impl<'a, T> Iter<'a, T> {
pub(super) fn new(ring: &'a [MaybeUninit<T>], tail: usize, head: usize) -> Self {
Iter { ring, tail, head }
}
}

#[stable(feature = "collection_debug", since = "1.17.0")]
Expand Down
21 changes: 8 additions & 13 deletions library/alloc/src/collections/vec_deque/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn iter(&self) -> Iter<'_, T> {
Iter { tail: self.tail, head: self.head, ring: unsafe { self.buffer_as_slice() } }
Iter::new(unsafe { self.buffer_as_slice() }, self.tail, self.head)
}

/// Returns a front-to-back iterator that returns mutable references.
Expand Down Expand Up @@ -1188,12 +1188,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
R: RangeBounds<usize>,
{
let (tail, head) = self.range_tail_head(range);
Iter {
tail,
head,
// The shared reference we have in &self is maintained in the '_ of Iter.
ring: unsafe { self.buffer_as_slice() },
}
// The shared reference we have in &self is maintained in the '_ of Iter.
Iter::new(unsafe { self.buffer_as_slice() }, tail, head)
}

/// Creates an iterator that covers the specified mutable range in the deque.
Expand Down Expand Up @@ -1309,16 +1305,15 @@ impl<T, A: Allocator> VecDeque<T, A> {
self.head = drain_tail;

let deque = NonNull::from(&mut *self);
let iter = Iter {
tail: drain_tail,
head: drain_head,
unsafe {
// 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 ring = self.buffer_as_slice();
let iter = Iter::new(ring, drain_tail, drain_head);

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

/// Clears the deque, removing all values.
Expand Down