Skip to content

Commit fa6a4f7

Browse files
committed
avoid unnecessary intermediate reference and improve safety comments
1 parent 69669cb commit fa6a4f7

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

library/alloc/src/collections/vec_deque.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -983,10 +983,12 @@ impl<T> VecDeque<T> {
983983
/// ```
984984
#[stable(feature = "rust1", since = "1.0.0")]
985985
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
986+
// SAFETY: The internal `IterMut` safety invariant is established because the
987+
// `ring` we create is a dereferencable slice for lifetime '_.
986988
IterMut {
987989
tail: self.tail,
988990
head: self.head,
989-
ring: unsafe { self.buffer_as_mut_slice() },
991+
ring: ptr::slice_from_raw_parts_mut(self.ptr(), self.cap()),
990992
phantom: PhantomData,
991993
}
992994
}
@@ -1176,11 +1178,13 @@ impl<T> VecDeque<T> {
11761178
R: RangeBounds<usize>,
11771179
{
11781180
let (tail, head) = self.range_tail_head(range);
1181+
1182+
// SAFETY: The internal `IterMut` safety invariant is established because the
1183+
// `ring` we create is a dereferencable slice for lifetime '_.
11791184
IterMut {
11801185
tail,
11811186
head,
1182-
// The shared reference we have in &mut self is maintained in the '_ of IterMut.
1183-
ring: unsafe { self.buffer_as_mut_slice() },
1187+
ring: ptr::slice_from_raw_parts_mut(self.ptr(), self.cap()),
11841188
phantom: PhantomData,
11851189
}
11861190
}
@@ -2688,6 +2692,7 @@ impl<T> FusedIterator for Iter<'_, T> {}
26882692
/// [`iter_mut`]: VecDeque::iter_mut
26892693
#[stable(feature = "rust1", since = "1.0.0")]
26902694
pub struct IterMut<'a, T: 'a> {
2695+
// Internal safety invariant: the entire slice is dereferencable.
26912696
ring: *mut [T],
26922697
tail: usize,
26932698
head: usize,
@@ -2706,7 +2711,7 @@ impl<T: fmt::Debug> fmt::Debug for IterMut<'_, T> {
27062711
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27072712
let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail);
27082713
// SAFETY: these are the elements we have not handed out yet, so aliasing is fine.
2709-
// We also ensure everything is dereferencable and in-bounds.
2714+
// The `IterMut` invariant also ensures everything is dereferencable.
27102715
let (front, back) = unsafe { (&*front, &*back) };
27112716
f.debug_tuple("IterMut").field(&front).field(&back).finish()
27122717
}
@@ -2742,7 +2747,7 @@ impl<'a, T> Iterator for IterMut<'a, T> {
27422747
{
27432748
let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail);
27442749
// SAFETY: these are the elements we have not handed out yet, so aliasing is fine.
2745-
// We also ensure everything is dereferencable and in-bounds.
2750+
// The `IterMut` invariant also ensures everything is dereferencable.
27462751
let (front, back) = unsafe { (&mut *front, &mut *back) };
27472752
accum = front.iter_mut().fold(accum, &mut f);
27482753
back.iter_mut().fold(accum, &mut f)
@@ -2785,7 +2790,7 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
27852790
{
27862791
let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail);
27872792
// SAFETY: these are the elements we have not handed out yet, so aliasing is fine.
2788-
// We also ensure everything is dereferencable and in-bounds.
2793+
// The `IterMut` invariant also ensures everything is dereferencable.
27892794
let (front, back) = unsafe { (&mut *front, &mut *back) };
27902795
accum = back.iter_mut().rfold(accum, &mut f);
27912796
front.iter_mut().rfold(accum, &mut f)

0 commit comments

Comments
 (0)