@@ -14,6 +14,7 @@ use core::cmp::{self, Ordering};
1414use core:: fmt;
1515use core:: hash:: { Hash , Hasher } ;
1616use core:: iter:: { repeat_with, FromIterator , FusedIterator } ;
17+ use core:: marker:: PhantomData ;
1718use core:: mem:: { self , replace, ManuallyDrop } ;
1819use core:: ops:: { Index , IndexMut , Range , RangeBounds , Try } ;
1920use core:: ptr:: { self , NonNull } ;
@@ -982,7 +983,14 @@ impl<T> VecDeque<T> {
982983 /// ```
983984 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
984985 pub fn iter_mut ( & mut self ) -> IterMut < ' _ , T > {
985- IterMut { tail : self . tail , head : self . head , ring : unsafe { self . buffer_as_mut_slice ( ) } }
986+ // SAFETY: The internal `IterMut` safety invariant is established because the
987+ // `ring` we create is a dereferencable slice for lifetime '_.
988+ IterMut {
989+ tail : self . tail ,
990+ head : self . head ,
991+ ring : ptr:: slice_from_raw_parts_mut ( self . ptr ( ) , self . cap ( ) ) ,
992+ phantom : PhantomData ,
993+ }
986994 }
987995
988996 /// Returns a pair of slices which contain, in order, the contents of the
@@ -1170,11 +1178,14 @@ impl<T> VecDeque<T> {
11701178 R : RangeBounds < usize > ,
11711179 {
11721180 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 '_.
11731184 IterMut {
11741185 tail,
11751186 head,
1176- // The shared reference we have in &mut self is maintained in the '_ of IterMut.
1177- ring : unsafe { self . buffer_as_mut_slice ( ) } ,
1187+ ring : ptr :: slice_from_raw_parts_mut ( self . ptr ( ) , self . cap ( ) ) ,
1188+ phantom : PhantomData ,
11781189 }
11791190 }
11801191
@@ -2493,6 +2504,25 @@ impl<T> RingSlices for &mut [T] {
24932504 }
24942505}
24952506
2507+ impl < T > RingSlices for * mut [ T ] {
2508+ fn slice ( self , from : usize , to : usize ) -> Self {
2509+ assert ! ( from <= to && to < self . len( ) ) ;
2510+ // Not using `get_unchecked_mut` to keep this a safe operation.
2511+ let len = to - from;
2512+ ptr:: slice_from_raw_parts_mut ( self . as_mut_ptr ( ) . wrapping_add ( from) , len)
2513+ }
2514+
2515+ fn split_at ( self , mid : usize ) -> ( Self , Self ) {
2516+ let len = self . len ( ) ;
2517+ let ptr = self . as_mut_ptr ( ) ;
2518+ assert ! ( mid <= len) ;
2519+ (
2520+ ptr:: slice_from_raw_parts_mut ( ptr, mid) ,
2521+ ptr:: slice_from_raw_parts_mut ( ptr. wrapping_add ( mid) , len - mid) ,
2522+ )
2523+ }
2524+ }
2525+
24962526/// Calculate the number of elements left to be read in the buffer
24972527#[ inline]
24982528fn count ( tail : usize , head : usize , size : usize ) -> usize {
@@ -2662,15 +2692,27 @@ impl<T> FusedIterator for Iter<'_, T> {}
26622692/// [`iter_mut`]: VecDeque::iter_mut
26632693#[ stable( feature = "rust1" , since = "1.0.0" ) ]
26642694pub struct IterMut < ' a , T : ' a > {
2665- ring : & ' a mut [ T ] ,
2695+ // Internal safety invariant: the entire slice is dereferencable.
2696+ ring : * mut [ T ] ,
26662697 tail : usize ,
26672698 head : usize ,
2699+ phantom : PhantomData < & ' a mut [ T ] > ,
26682700}
26692701
2702+ // SAFETY: we do nothing thread-local and there is no interior mutability,
2703+ // so the usual structural `Send`/`Sync` apply.
2704+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2705+ unsafe impl < T : Send > Send for IterMut < ' _ , T > { }
2706+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2707+ unsafe impl < T : Sync > Sync for IterMut < ' _ , T > { }
2708+
26702709#[ stable( feature = "collection_debug" , since = "1.17.0" ) ]
26712710impl < T : fmt:: Debug > fmt:: Debug for IterMut < ' _ , T > {
26722711 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2673- let ( front, back) = RingSlices :: ring_slices ( & * self . ring , self . head , self . tail ) ;
2712+ let ( front, back) = RingSlices :: ring_slices ( self . ring , self . head , self . tail ) ;
2713+ // SAFETY: these are the elements we have not handed out yet, so aliasing is fine.
2714+ // The `IterMut` invariant also ensures everything is dereferencable.
2715+ let ( front, back) = unsafe { ( & * front, & * back) } ;
26742716 f. debug_tuple ( "IterMut" ) . field ( & front) . field ( & back) . finish ( )
26752717 }
26762718}
@@ -2689,7 +2731,7 @@ impl<'a, T> Iterator for IterMut<'a, T> {
26892731
26902732 unsafe {
26912733 let elem = self . ring . get_unchecked_mut ( tail) ;
2692- Some ( & mut * ( elem as * mut _ ) )
2734+ Some ( & mut * elem)
26932735 }
26942736 }
26952737
@@ -2704,6 +2746,9 @@ impl<'a, T> Iterator for IterMut<'a, T> {
27042746 F : FnMut ( Acc , Self :: Item ) -> Acc ,
27052747 {
27062748 let ( front, back) = RingSlices :: ring_slices ( self . ring , self . head , self . tail ) ;
2749+ // SAFETY: these are the elements we have not handed out yet, so aliasing is fine.
2750+ // The `IterMut` invariant also ensures everything is dereferencable.
2751+ let ( front, back) = unsafe { ( & mut * front, & mut * back) } ;
27072752 accum = front. iter_mut ( ) . fold ( accum, & mut f) ;
27082753 back. iter_mut ( ) . fold ( accum, & mut f)
27092754 }
@@ -2735,7 +2780,7 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
27352780
27362781 unsafe {
27372782 let elem = self . ring . get_unchecked_mut ( self . head ) ;
2738- Some ( & mut * ( elem as * mut _ ) )
2783+ Some ( & mut * elem)
27392784 }
27402785 }
27412786
@@ -2744,6 +2789,9 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
27442789 F : FnMut ( Acc , Self :: Item ) -> Acc ,
27452790 {
27462791 let ( front, back) = RingSlices :: ring_slices ( self . ring , self . head , self . tail ) ;
2792+ // SAFETY: these are the elements we have not handed out yet, so aliasing is fine.
2793+ // The `IterMut` invariant also ensures everything is dereferencable.
2794+ let ( front, back) = unsafe { ( & mut * front, & mut * back) } ;
27472795 accum = back. iter_mut ( ) . rfold ( accum, & mut f) ;
27482796 front. iter_mut ( ) . rfold ( accum, & mut f)
27492797 }
0 commit comments