Rollup merge of #89685 - DeveloperC286:iter_fields_to_private, r=oli-obk

refactor: VecDeques Iter fields to private

Made the fields of VecDeque's Iter private by creating a Iter::new(...) function to create a new instance of Iter and migrating usage to use Iter::new(...).
This commit is contained in:
Michael Goulet 2022-05-30 15:57:27 -07:00 committed by GitHub
commit 3c0b9d50ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 16 deletions

View File

@ -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")]

View File

@ -1013,7 +1013,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.
@ -1192,12 +1192,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() },
}
Iter::new(unsafe { self.buffer_as_slice() }, tail, head)
}
/// Creates an iterator that covers the specified mutable range in the deque.
@ -1313,16 +1309,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.