@@ -983,10 +983,12 @@ impl<T> VecDeque<T> {
983
983
/// ```
984
984
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
985
985
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 '_.
986
988
IterMut {
987
989
tail : self . tail ,
988
990
head : self . head ,
989
- ring : unsafe { self . buffer_as_mut_slice ( ) } ,
991
+ ring : ptr :: slice_from_raw_parts_mut ( self . ptr ( ) , self . cap ( ) ) ,
990
992
phantom : PhantomData ,
991
993
}
992
994
}
@@ -1176,11 +1178,13 @@ impl<T> VecDeque<T> {
1176
1178
R : RangeBounds < usize > ,
1177
1179
{
1178
1180
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 '_.
1179
1184
IterMut {
1180
1185
tail,
1181
1186
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 ( ) ) ,
1184
1188
phantom : PhantomData ,
1185
1189
}
1186
1190
}
@@ -2688,6 +2692,7 @@ impl<T> FusedIterator for Iter<'_, T> {}
2688
2692
/// [`iter_mut`]: VecDeque::iter_mut
2689
2693
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2690
2694
pub struct IterMut < ' a , T : ' a > {
2695
+ // Internal safety invariant: the entire slice is dereferencable.
2691
2696
ring : * mut [ T ] ,
2692
2697
tail : usize ,
2693
2698
head : usize ,
@@ -2706,7 +2711,7 @@ impl<T: fmt::Debug> fmt::Debug for IterMut<'_, T> {
2706
2711
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2707
2712
let ( front, back) = RingSlices :: ring_slices ( self . ring , self . head , self . tail ) ;
2708
2713
// 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.
2710
2715
let ( front, back) = unsafe { ( & * front, & * back) } ;
2711
2716
f. debug_tuple ( "IterMut" ) . field ( & front) . field ( & back) . finish ( )
2712
2717
}
@@ -2742,7 +2747,7 @@ impl<'a, T> Iterator for IterMut<'a, T> {
2742
2747
{
2743
2748
let ( front, back) = RingSlices :: ring_slices ( self . ring , self . head , self . tail ) ;
2744
2749
// 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.
2746
2751
let ( front, back) = unsafe { ( & mut * front, & mut * back) } ;
2747
2752
accum = front. iter_mut ( ) . fold ( accum, & mut f) ;
2748
2753
back. iter_mut ( ) . fold ( accum, & mut f)
@@ -2785,7 +2790,7 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
2785
2790
{
2786
2791
let ( front, back) = RingSlices :: ring_slices ( self . ring , self . head , self . tail ) ;
2787
2792
// 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.
2789
2794
let ( front, back) = unsafe { ( & mut * front, & mut * back) } ;
2790
2795
accum = back. iter_mut ( ) . rfold ( accum, & mut f) ;
2791
2796
front. iter_mut ( ) . rfold ( accum, & mut f)
0 commit comments