@@ -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,12 @@ 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
+ IterMut {
987
+ tail : self . tail ,
988
+ head : self . head ,
989
+ ring : unsafe { self . buffer_as_mut_slice ( ) } ,
990
+ phantom : PhantomData ,
991
+ }
986
992
}
987
993
988
994
/// Returns a pair of slices which contain, in order, the contents of the
@@ -1175,6 +1181,7 @@ impl<T> VecDeque<T> {
1175
1181
head,
1176
1182
// The shared reference we have in &mut self is maintained in the '_ of IterMut.
1177
1183
ring : unsafe { self . buffer_as_mut_slice ( ) } ,
1184
+ phantom : PhantomData ,
1178
1185
}
1179
1186
}
1180
1187
@@ -2662,15 +2669,19 @@ impl<T> FusedIterator for Iter<'_, T> {}
2662
2669
/// [`iter_mut`]: VecDeque::iter_mut
2663
2670
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2664
2671
pub struct IterMut < ' a , T : ' a > {
2665
- ring : & ' a mut [ T ] ,
2672
+ ring : * mut [ T ] ,
2666
2673
tail : usize ,
2667
2674
head : usize ,
2675
+ phantom : PhantomData < & ' a mut [ T ] > ,
2668
2676
}
2669
2677
2670
2678
#[ stable( feature = "collection_debug" , since = "1.17.0" ) ]
2671
2679
impl < T : fmt:: Debug > fmt:: Debug for IterMut < ' _ , T > {
2672
2680
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2673
- let ( front, back) = RingSlices :: ring_slices ( & * self . ring , self . head , self . tail ) ;
2681
+ // FIXME: this creates a reference to the full ring, including the part
2682
+ // to which we already handed out mutable references via `next()`. This
2683
+ // is an aliasing violation.
2684
+ let ( front, back) = RingSlices :: ring_slices ( unsafe { & * self . ring } , self . head , self . tail ) ;
2674
2685
f. debug_tuple ( "IterMut" ) . field ( & front) . field ( & back) . finish ( )
2675
2686
}
2676
2687
}
@@ -2689,7 +2700,7 @@ impl<'a, T> Iterator for IterMut<'a, T> {
2689
2700
2690
2701
unsafe {
2691
2702
let elem = self . ring . get_unchecked_mut ( tail) ;
2692
- Some ( & mut * ( elem as * mut _ ) )
2703
+ Some ( & mut * elem)
2693
2704
}
2694
2705
}
2695
2706
@@ -2703,7 +2714,10 @@ impl<'a, T> Iterator for IterMut<'a, T> {
2703
2714
where
2704
2715
F : FnMut ( Acc , Self :: Item ) -> Acc ,
2705
2716
{
2706
- let ( front, back) = RingSlices :: ring_slices ( self . ring , self . head , self . tail ) ;
2717
+ // FIXME: this creates a reference to the full ring, including the part
2718
+ // to which we already handed out mutable references via `next()`. This
2719
+ // is an aliasing violation.
2720
+ let ( front, back) = RingSlices :: ring_slices ( unsafe { & mut * self . ring } , self . head , self . tail ) ;
2707
2721
accum = front. iter_mut ( ) . fold ( accum, & mut f) ;
2708
2722
back. iter_mut ( ) . fold ( accum, & mut f)
2709
2723
}
@@ -2735,15 +2749,18 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
2735
2749
2736
2750
unsafe {
2737
2751
let elem = self . ring . get_unchecked_mut ( self . head ) ;
2738
- Some ( & mut * ( elem as * mut _ ) )
2752
+ Some ( & mut * elem)
2739
2753
}
2740
2754
}
2741
2755
2742
2756
fn rfold < Acc , F > ( self , mut accum : Acc , mut f : F ) -> Acc
2743
2757
where
2744
2758
F : FnMut ( Acc , Self :: Item ) -> Acc ,
2745
2759
{
2746
- let ( front, back) = RingSlices :: ring_slices ( self . ring , self . head , self . tail ) ;
2760
+ // FIXME: this creates a reference to the full ring, including the part
2761
+ // to which we already handed out mutable references via `next()`. This
2762
+ // is an aliasing violation.
2763
+ let ( front, back) = RingSlices :: ring_slices ( unsafe { & mut * self . ring } , self . head , self . tail ) ;
2747
2764
accum = back. iter_mut ( ) . rfold ( accum, & mut f) ;
2748
2765
front. iter_mut ( ) . rfold ( accum, & mut f)
2749
2766
}
0 commit comments