Skip to content

Commit 9f0fd13

Browse files
authoredOct 6, 2021
Rollup merge of rust-lang#89245 - DeveloperC286:iter_mut_fields_to_private, r=joshtriplett
refactor: make VecDeque's IterMut fields module-private, not just crate-private Made the fields of VecDeque's IterMut private by creating a IterMut::new(...) function to create a new instance of IterMut and migrating usage to use IterMut::new(...).
2 parents de72bc4 + 5af61cb commit 9f0fd13

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed
 

‎library/alloc/src/collections/vec_deque/iter_mut.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,21 @@ use super::{count, wrap_index, RingSlices};
1313
#[stable(feature = "rust1", since = "1.0.0")]
1414
pub struct IterMut<'a, T: 'a> {
1515
// Internal safety invariant: the entire slice is dereferencable.
16-
pub(crate) ring: *mut [T],
17-
pub(crate) tail: usize,
18-
pub(crate) head: usize,
19-
pub(crate) phantom: PhantomData<&'a mut [T]>,
16+
ring: *mut [T],
17+
tail: usize,
18+
head: usize,
19+
phantom: PhantomData<&'a mut [T]>,
20+
}
21+
22+
impl<'a, T> IterMut<'a, T> {
23+
pub(super) unsafe fn new(
24+
ring: *mut [T],
25+
tail: usize,
26+
head: usize,
27+
phantom: PhantomData<&'a mut [T]>,
28+
) -> Self {
29+
IterMut { ring, tail, head, phantom }
30+
}
2031
}
2132

2233
// SAFETY: we do nothing thread-local and there is no interior mutability,

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

+6-12
Original file line numberDiff line numberDiff line change
@@ -1000,12 +1000,9 @@ impl<T, A: Allocator> VecDeque<T, A> {
10001000
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
10011001
// SAFETY: The internal `IterMut` safety invariant is established because the
10021002
// `ring` we create is a dereferencable slice for lifetime '_.
1003-
IterMut {
1004-
tail: self.tail,
1005-
head: self.head,
1006-
ring: ptr::slice_from_raw_parts_mut(self.ptr(), self.cap()),
1007-
phantom: PhantomData,
1008-
}
1003+
let ring = ptr::slice_from_raw_parts_mut(self.ptr(), self.cap());
1004+
1005+
unsafe { IterMut::new(ring, self.tail, self.head, PhantomData) }
10091006
}
10101007

10111008
/// Returns a pair of slices which contain, in order, the contents of the
@@ -1192,12 +1189,9 @@ impl<T, A: Allocator> VecDeque<T, A> {
11921189

11931190
// SAFETY: The internal `IterMut` safety invariant is established because the
11941191
// `ring` we create is a dereferencable slice for lifetime '_.
1195-
IterMut {
1196-
tail,
1197-
head,
1198-
ring: ptr::slice_from_raw_parts_mut(self.ptr(), self.cap()),
1199-
phantom: PhantomData,
1200-
}
1192+
let ring = ptr::slice_from_raw_parts_mut(self.ptr(), self.cap());
1193+
1194+
unsafe { IterMut::new(ring, tail, head, PhantomData) }
12011195
}
12021196

12031197
/// Creates a draining iterator that removes the specified range in the

0 commit comments

Comments
 (0)
Please sign in to comment.