Skip to content

Commit cf30ac8

Browse files
committed
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(...).
1 parent e95b10b commit cf30ac8

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
@@ -1009,7 +1009,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
10091009
/// ```
10101010
#[stable(feature = "rust1", since = "1.0.0")]
10111011
pub fn iter(&self) -> Iter<'_, T> {
1012-
Iter { tail: self.tail, head: self.head, ring: unsafe { self.buffer_as_slice() } }
1012+
Iter::new(unsafe { self.buffer_as_slice() }, self.tail, self.head)
10131013
}
10141014

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

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

13111307
let deque = NonNull::from(&mut *self);
1312-
let iter = Iter {
1313-
tail: drain_tail,
1314-
head: drain_head,
1308+
unsafe {
13151309
// Crucially, we only create shared references from `self` here and read from
13161310
// it. We do not write to `self` nor reborrow to a mutable reference.
13171311
// Hence the raw pointer we created above, for `deque`, remains valid.
1318-
ring: unsafe { self.buffer_as_slice() },
1319-
};
1312+
let ring = self.buffer_as_slice();
1313+
let iter = Iter::new(ring, drain_tail, drain_head);
13201314

1321-
unsafe { Drain::new(drain_head, head, iter, deque) }
1315+
Drain::new(drain_head, head, iter, deque)
1316+
}
13221317
}
13231318

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

0 commit comments

Comments
 (0)