@@ -14,6 +14,7 @@ use core::cmp::{self, Ordering};
14
14
use core:: fmt;
15
15
use core:: hash:: { Hash , Hasher } ;
16
16
use core:: iter:: { repeat_with, FromIterator , FusedIterator } ;
17
+ use core:: marker:: PhantomData ;
17
18
use core:: mem:: { self , replace, ManuallyDrop } ;
18
19
use core:: ops:: { Index , IndexMut , Range , RangeBounds , Try } ;
19
20
use core:: ptr:: { self , NonNull } ;
@@ -982,7 +983,14 @@ impl<T> VecDeque<T> {
982
983
/// ```
983
984
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
984
985
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
+ }
986
994
}
987
995
988
996
/// Returns a pair of slices which contain, in order, the contents of the
@@ -1170,11 +1178,14 @@ impl<T> VecDeque<T> {
1170
1178
R : RangeBounds < usize > ,
1171
1179
{
1172
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 '_.
1173
1184
IterMut {
1174
1185
tail,
1175
1186
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 ,
1178
1189
}
1179
1190
}
1180
1191
@@ -2493,6 +2504,25 @@ impl<T> RingSlices for &mut [T] {
2493
2504
}
2494
2505
}
2495
2506
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
+
2496
2526
/// Calculate the number of elements left to be read in the buffer
2497
2527
#[ inline]
2498
2528
fn count ( tail : usize , head : usize , size : usize ) -> usize {
@@ -2662,15 +2692,27 @@ impl<T> FusedIterator for Iter<'_, T> {}
2662
2692
/// [`iter_mut`]: VecDeque::iter_mut
2663
2693
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2664
2694
pub struct IterMut < ' a , T : ' a > {
2665
- ring : & ' a mut [ T ] ,
2695
+ // Internal safety invariant: the entire slice is dereferencable.
2696
+ ring : * mut [ T ] ,
2666
2697
tail : usize ,
2667
2698
head : usize ,
2699
+ phantom : PhantomData < & ' a mut [ T ] > ,
2668
2700
}
2669
2701
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
+
2670
2709
#[ stable( feature = "collection_debug" , since = "1.17.0" ) ]
2671
2710
impl < T : fmt:: Debug > fmt:: Debug for IterMut < ' _ , T > {
2672
2711
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) } ;
2674
2716
f. debug_tuple ( "IterMut" ) . field ( & front) . field ( & back) . finish ( )
2675
2717
}
2676
2718
}
@@ -2689,7 +2731,7 @@ impl<'a, T> Iterator for IterMut<'a, T> {
2689
2731
2690
2732
unsafe {
2691
2733
let elem = self . ring . get_unchecked_mut ( tail) ;
2692
- Some ( & mut * ( elem as * mut _ ) )
2734
+ Some ( & mut * elem)
2693
2735
}
2694
2736
}
2695
2737
@@ -2704,6 +2746,9 @@ impl<'a, T> Iterator for IterMut<'a, T> {
2704
2746
F : FnMut ( Acc , Self :: Item ) -> Acc ,
2705
2747
{
2706
2748
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) } ;
2707
2752
accum = front. iter_mut ( ) . fold ( accum, & mut f) ;
2708
2753
back. iter_mut ( ) . fold ( accum, & mut f)
2709
2754
}
@@ -2735,7 +2780,7 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
2735
2780
2736
2781
unsafe {
2737
2782
let elem = self . ring . get_unchecked_mut ( self . head ) ;
2738
- Some ( & mut * ( elem as * mut _ ) )
2783
+ Some ( & mut * elem)
2739
2784
}
2740
2785
}
2741
2786
@@ -2744,6 +2789,9 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
2744
2789
F : FnMut ( Acc , Self :: Item ) -> Acc ,
2745
2790
{
2746
2791
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) } ;
2747
2795
accum = back. iter_mut ( ) . rfold ( accum, & mut f) ;
2748
2796
front. iter_mut ( ) . rfold ( accum, & mut f)
2749
2797
}
0 commit comments