Skip to content

Commit 3c0b9d5

Browse files
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(...).
2 parents c35035c + cf30ac8 commit 3c0b9d5

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@ use super::{count, wrap_index, RingSlices};
1313
/// [`iter`]: super::VecDeque::iter
1414
#[stable(feature = "rust1", since = "1.0.0")]
1515
pub struct Iter<'a, T: 'a> {
16-
pub(crate) ring: &'a [MaybeUninit<T>],
17-
pub(crate) tail: usize,
18-
pub(crate) head: usize,
16+
ring: &'a [MaybeUninit<T>],
17+
tail: usize,
18+
head: usize,
19+
}
20+
21+
impl<'a, T> Iter<'a, T> {
22+
pub(super) fn new(ring: &'a [MaybeUninit<T>], tail: usize, head: usize) -> Self {
23+
Iter { ring, tail, head }
24+
}
1925
}
2026

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

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

+8-13
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
10131013
/// ```
10141014
#[stable(feature = "rust1", since = "1.0.0")]
10151015
pub fn iter(&self) -> Iter<'_, T> {
1016-
Iter { tail: self.tail, head: self.head, ring: unsafe { self.buffer_as_slice() } }
1016+
Iter::new(unsafe { self.buffer_as_slice() }, self.tail, self.head)
10171017
}
10181018

10191019
/// Returns a front-to-back iterator that returns mutable references.
@@ -1192,12 +1192,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
11921192
R: RangeBounds<usize>,
11931193
{
11941194
let (tail, head) = self.range_tail_head(range);
1195-
Iter {
1196-
tail,
1197-
head,
1198-
// The shared reference we have in &self is maintained in the '_ of Iter.
1199-
ring: unsafe { self.buffer_as_slice() },
1200-
}
1195+
// The shared reference we have in &self is maintained in the '_ of Iter.
1196+
Iter::new(unsafe { self.buffer_as_slice() }, tail, head)
12011197
}
12021198

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

13151311
let deque = NonNull::from(&mut *self);
1316-
let iter = Iter {
1317-
tail: drain_tail,
1318-
head: drain_head,
1312+
unsafe {
13191313
// Crucially, we only create shared references from `self` here and read from
13201314
// it. We do not write to `self` nor reborrow to a mutable reference.
13211315
// Hence the raw pointer we created above, for `deque`, remains valid.
1322-
ring: unsafe { self.buffer_as_slice() },
1323-
};
1316+
let ring = self.buffer_as_slice();
1317+
let iter = Iter::new(ring, drain_tail, drain_head);
13241318

1325-
unsafe { Drain::new(drain_head, head, iter, deque) }
1319+
Drain::new(drain_head, head, iter, deque)
1320+
}
13261321
}
13271322

13281323
/// Clears the deque, removing all values.

0 commit comments

Comments
 (0)