@@ -1629,6 +1629,11 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for Chunks<'a, T> {
1629
1629
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1630
1630
#[ must_use = "iterators are lazy and do nothing unless consumed" ]
1631
1631
pub struct ChunksMut < ' a , T : ' a > {
1632
+ // This slice pointer must point at a valid region of T with at least length v.len(). Normally,
1633
+ // those requirements would mean that we could instead use a &mut [T] here, but we cannot
1634
+ // because __iterator_get_unchecked needs to return &mut [T], which guarantees certain aliasing
1635
+ // properties that we cannot uphold if we hold on to the full original &mut [T]. Wrapping a raw
1636
+ // slice instead lets us hand out non-overlapping &mut [T] subslices of the slice we wrap.
1632
1637
v : * mut [ T ] ,
1633
1638
chunk_size : usize ,
1634
1639
_marker : PhantomData < & ' a mut T > ,
@@ -1651,10 +1656,10 @@ impl<'a, T> Iterator for ChunksMut<'a, T> {
1651
1656
None
1652
1657
} else {
1653
1658
let sz = cmp:: min ( self . v . len ( ) , self . chunk_size ) ;
1654
- // SAFETY: sz cannot exceed the slice length based on the calculation above
1659
+ // SAFETY: This type ensures that any split_at_mut on self.v is valid.
1655
1660
let ( head, tail) = unsafe { self . v . split_at_mut ( sz) } ;
1656
1661
self . v = tail;
1657
- // SAFETY: Nothing points to or will point to the contents of this slice
1662
+ // SAFETY: Nothing else points to or will point to the contents of this slice.
1658
1663
Some ( unsafe { & mut * head } )
1659
1664
}
1660
1665
}
@@ -1687,12 +1692,12 @@ impl<'a, T> Iterator for ChunksMut<'a, T> {
1687
1692
Some ( sum) => cmp:: min ( self . v . len ( ) , sum) ,
1688
1693
None => self . v . len ( ) ,
1689
1694
} ;
1690
- // SAFETY: end is inbounds because we compared above against self.v.len()
1695
+ // SAFETY: This type ensures that any split_at_mut on self.v is valid.
1691
1696
let ( head, tail) = unsafe { self . v . split_at_mut ( end) } ;
1692
- // SAFETY: start is inbounds because
1697
+ // SAFETY: This type ensures that any split_at_mut on self.v is valid.
1693
1698
let ( _, nth) = unsafe { head. split_at_mut ( start) } ;
1694
1699
self . v = tail;
1695
- // SAFETY: Nothing points to or will point to the contents of this slice
1700
+ // SAFETY: Nothing else points to or will point to the contents of this slice.
1696
1701
Some ( unsafe { & mut * nth } )
1697
1702
}
1698
1703
}
@@ -1703,7 +1708,7 @@ impl<'a, T> Iterator for ChunksMut<'a, T> {
1703
1708
None
1704
1709
} else {
1705
1710
let start = ( self . v . len ( ) - 1 ) / self . chunk_size * self . chunk_size ;
1706
- // SAFETY: Nothing points to or will point to the contents of this slice
1711
+ // SAFETY: Nothing else points to or will point to the contents of this slice.
1707
1712
Some ( unsafe { & mut * self . v . get_unchecked_mut ( start..) } )
1708
1713
}
1709
1714
}
@@ -1736,7 +1741,7 @@ impl<'a, T> DoubleEndedIterator for ChunksMut<'a, T> {
1736
1741
// SAFETY: Similar to `Chunks::next_back`
1737
1742
let ( head, tail) = unsafe { self . v . split_at_mut_unchecked ( len - sz) } ;
1738
1743
self . v = head;
1739
- // SAFETY: Nothing points to or will point to the contents of this slice
1744
+ // SAFETY: Nothing else points to or will point to the contents of this slice.
1740
1745
Some ( unsafe { & mut * tail } )
1741
1746
}
1742
1747
}
@@ -1753,11 +1758,12 @@ impl<'a, T> DoubleEndedIterator for ChunksMut<'a, T> {
1753
1758
Some ( res) => cmp:: min ( self . v . len ( ) , res) ,
1754
1759
None => self . v . len ( ) ,
1755
1760
} ;
1756
- // SAFETY: end is inbounds because we compared above against self.v.len()
1761
+ // SAFETY: This type ensures that any split_at_mut on self.v is valid.
1757
1762
let ( temp, _tail) = unsafe { self . v . split_at_mut ( end) } ;
1763
+ // SAFETY: This type ensures that any split_at_mut on self.v is valid.
1758
1764
let ( head, nth_back) = unsafe { temp. split_at_mut ( start) } ;
1759
1765
self . v = head;
1760
- // SAFETY: Nothing points to or will point to the contents of this slice
1766
+ // SAFETY: Nothing else points to or will point to the contents of this slice.
1761
1767
Some ( unsafe { & mut * nth_back } )
1762
1768
}
1763
1769
}
@@ -1964,6 +1970,11 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for ChunksExact<'a, T> {
1964
1970
#[ stable( feature = "chunks_exact" , since = "1.31.0" ) ]
1965
1971
#[ must_use = "iterators are lazy and do nothing unless consumed" ]
1966
1972
pub struct ChunksExactMut < ' a , T : ' a > {
1973
+ // This slice pointer must point at a valid region of T with at least length v.len(). Normally,
1974
+ // those requirements would mean that we could instead use a &mut [T] here, but we cannot
1975
+ // because __iterator_get_unchecked needs to return &mut [T], which guarantees certain aliasing
1976
+ // properties that we cannot uphold if we hold on to the full original &mut [T]. Wrapping a raw
1977
+ // slice instead lets us hand out non-overlapping &mut [T] subslices of the slice we wrap.
1967
1978
v : * mut [ T ] ,
1968
1979
rem : & ' a mut [ T ] , // The iterator never yields from here, so this can be unique
1969
1980
chunk_size : usize ,
@@ -2002,7 +2013,7 @@ impl<'a, T> Iterator for ChunksExactMut<'a, T> {
2002
2013
// SAFETY: self.chunk_size is inbounds because we compared above against self.v.len()
2003
2014
let ( head, tail) = unsafe { self . v . split_at_mut ( self . chunk_size ) } ;
2004
2015
self . v = tail;
2005
- // SAFETY: Nothing points to or will point to the contents of this slice
2016
+ // SAFETY: Nothing else points to or will point to the contents of this slice.
2006
2017
Some ( unsafe { & mut * head } )
2007
2018
}
2008
2019
}
@@ -2025,6 +2036,7 @@ impl<'a, T> Iterator for ChunksExactMut<'a, T> {
2025
2036
self . v = & mut [ ] ;
2026
2037
None
2027
2038
} else {
2039
+ // SAFETY: This type ensures that any split_at_mut on self.v is valid.
2028
2040
let ( _, snd) = unsafe { self . v . split_at_mut ( start) } ;
2029
2041
self . v = snd;
2030
2042
self . next ( )
@@ -2053,7 +2065,7 @@ impl<'a, T> DoubleEndedIterator for ChunksExactMut<'a, T> {
2053
2065
// SAFETY: This subtraction is inbounds because of the check above
2054
2066
let ( head, tail) = unsafe { self . v . split_at_mut ( self . v . len ( ) - self . chunk_size ) } ;
2055
2067
self . v = head;
2056
- // SAFETY: Nothing points to or will point to the contents of this slice
2068
+ // SAFETY: Nothing else points to or will point to the contents of this slice.
2057
2069
Some ( unsafe { & mut * tail } )
2058
2070
}
2059
2071
}
@@ -2067,10 +2079,12 @@ impl<'a, T> DoubleEndedIterator for ChunksExactMut<'a, T> {
2067
2079
} else {
2068
2080
let start = ( len - 1 - n) * self . chunk_size ;
2069
2081
let end = start + self . chunk_size ;
2082
+ // SAFETY: This type ensures that any split_at_mut on self.v is valid.
2070
2083
let ( temp, _tail) = unsafe { mem:: replace ( & mut self . v , & mut [ ] ) . split_at_mut ( end) } ;
2084
+ // SAFETY: This type ensures that any split_at_mut on self.v is valid.
2071
2085
let ( head, nth_back) = unsafe { temp. split_at_mut ( start) } ;
2072
2086
self . v = head;
2073
- // SAFETY: Nothing points to or will point to the contents of this slice
2087
+ // SAFETY: Nothing else points to or will point to the contents of this slice.
2074
2088
Some ( unsafe { & mut * nth_back } )
2075
2089
}
2076
2090
}
@@ -2655,6 +2669,11 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for RChunks<'a, T> {
2655
2669
#[ stable( feature = "rchunks" , since = "1.31.0" ) ]
2656
2670
#[ must_use = "iterators are lazy and do nothing unless consumed" ]
2657
2671
pub struct RChunksMut < ' a , T : ' a > {
2672
+ // This slice pointer must point at a valid region of T with at least length v.len(). Normally,
2673
+ // those requirements would mean that we could instead use a &mut [T] here, but we cannot
2674
+ // because __iterator_get_unchecked needs to return &mut [T], which guarantees certain aliasing
2675
+ // properties that we cannot uphold if we hold on to the full original &mut [T]. Wrapping a raw
2676
+ // slice instead lets us hand out non-overlapping &mut [T] subslices of the slice we wrap.
2658
2677
v : * mut [ T ] ,
2659
2678
chunk_size : usize ,
2660
2679
_marker : PhantomData < & ' a mut T > ,
@@ -2685,7 +2704,7 @@ impl<'a, T> Iterator for RChunksMut<'a, T> {
2685
2704
// `self.v.len()` (e.g. `len`) and `self.chunk_size`.
2686
2705
let ( head, tail) = unsafe { self . v . split_at_mut_unchecked ( len - sz) } ;
2687
2706
self . v = head;
2688
- // SAFETY: Nothing points to or will point to the contents of this slice
2707
+ // SAFETY: Nothing else points to or will point to the contents of this slice.
2689
2708
Some ( unsafe { & mut * tail } )
2690
2709
}
2691
2710
}
@@ -2720,10 +2739,14 @@ impl<'a, T> Iterator for RChunksMut<'a, T> {
2720
2739
Some ( sum) => sum,
2721
2740
None => 0 ,
2722
2741
} ;
2742
+ // SAFETY: This type ensures that self.v is a valid pointer with a correct len.
2743
+ // Therefore the bounds check in split_at_mut guarantess the split point is inbounds.
2723
2744
let ( head, tail) = unsafe { self . v . split_at_mut ( start) } ;
2745
+ // SAFETY: This type ensures that self.v is a valid pointer with a correct len.
2746
+ // Therefore the bounds check in split_at_mut guarantess the split point is inbounds.
2724
2747
let ( nth, _) = unsafe { tail. split_at_mut ( end - start) } ;
2725
2748
self . v = head;
2726
- // SAFETY: Nothing points to or will point to the contents of this slice
2749
+ // SAFETY: Nothing else points to or will point to the contents of this slice.
2727
2750
Some ( unsafe { & mut * nth } )
2728
2751
}
2729
2752
}
@@ -2735,7 +2758,7 @@ impl<'a, T> Iterator for RChunksMut<'a, T> {
2735
2758
} else {
2736
2759
let rem = self . v . len ( ) % self . chunk_size ;
2737
2760
let end = if rem == 0 { self . chunk_size } else { rem } ;
2738
- // SAFETY: Nothing points to or will point to the contents of this slice
2761
+ // SAFETY: Nothing else points to or will point to the contents of this slice.
2739
2762
Some ( unsafe { & mut * self . v . get_unchecked_mut ( 0 ..end) } )
2740
2763
}
2741
2764
}
@@ -2764,7 +2787,7 @@ impl<'a, T> DoubleEndedIterator for RChunksMut<'a, T> {
2764
2787
// SAFETY: Similar to `Chunks::next_back`
2765
2788
let ( head, tail) = unsafe { self . v . split_at_mut_unchecked ( sz) } ;
2766
2789
self . v = tail;
2767
- // SAFETY: Nothing points to or will point to the contents of this slice
2790
+ // SAFETY: Nothing else points to or will point to the contents of this slice.
2768
2791
Some ( unsafe { & mut * head } )
2769
2792
}
2770
2793
}
@@ -2780,10 +2803,12 @@ impl<'a, T> DoubleEndedIterator for RChunksMut<'a, T> {
2780
2803
let offset_from_end = ( len - 1 - n) * self . chunk_size ;
2781
2804
let end = self . v . len ( ) - offset_from_end;
2782
2805
let start = end. saturating_sub ( self . chunk_size ) ;
2806
+ // SAFETY: This type ensures that any split_at_mut on self.v is valid.
2783
2807
let ( tmp, tail) = unsafe { self . v . split_at_mut ( end) } ;
2808
+ // SAFETY: This type ensures that any split_at_mut on self.v is valid.
2784
2809
let ( _, nth_back) = unsafe { tmp. split_at_mut ( start) } ;
2785
2810
self . v = tail;
2786
- // SAFETY: Nothing points to or will point to the contents of this slice
2811
+ // SAFETY: Nothing else points to or will point to the contents of this slice.
2787
2812
Some ( unsafe { & mut * nth_back } )
2788
2813
}
2789
2814
}
@@ -2993,6 +3018,11 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for RChunksExact<'a, T> {
2993
3018
#[ stable( feature = "rchunks" , since = "1.31.0" ) ]
2994
3019
#[ must_use = "iterators are lazy and do nothing unless consumed" ]
2995
3020
pub struct RChunksExactMut < ' a , T : ' a > {
3021
+ // This slice pointer must point at a valid region of T with at least length v.len(). Normally,
3022
+ // those requirements would mean that we could instead use a &mut [T] here, but we cannot
3023
+ // because __iterator_get_unchecked needs to return &mut [T], which guarantees certain aliasing
3024
+ // properties that we cannot uphold if we hold on to the full original &mut [T]. Wrapping a raw
3025
+ // slice instead lets us hand out non-overlapping &mut [T] subslices of the slice we wrap.
2996
3026
v : * mut [ T ] ,
2997
3027
rem : & ' a mut [ T ] ,
2998
3028
chunk_size : usize ,
@@ -3027,9 +3057,10 @@ impl<'a, T> Iterator for RChunksExactMut<'a, T> {
3027
3057
None
3028
3058
} else {
3029
3059
let len = self . v . len ( ) ;
3060
+ // SAFETY: This type ensures that any split_at_mut on self.v is valid.
3030
3061
let ( head, tail) = unsafe { self . v . split_at_mut ( len - self . chunk_size ) } ;
3031
3062
self . v = head;
3032
- // SAFETY: Nothing points to or will point to the contents of this slice
3063
+ // SAFETY: Nothing else points to or will point to the contents of this slice.
3033
3064
Some ( unsafe { & mut * tail } )
3034
3065
}
3035
3066
}
@@ -3053,6 +3084,7 @@ impl<'a, T> Iterator for RChunksExactMut<'a, T> {
3053
3084
None
3054
3085
} else {
3055
3086
let len = self . v . len ( ) ;
3087
+ // SAFETY: This type ensures that any split_at_mut on self.v is valid.
3056
3088
let ( fst, _) = unsafe { self . v . split_at_mut ( len - end) } ;
3057
3089
self . v = fst;
3058
3090
self . next ( )
@@ -3079,9 +3111,10 @@ impl<'a, T> DoubleEndedIterator for RChunksExactMut<'a, T> {
3079
3111
if self . v . len ( ) < self . chunk_size {
3080
3112
None
3081
3113
} else {
3114
+ // SAFETY: This type ensures that any split_at_mut on self.v is valid.
3082
3115
let ( head, tail) = unsafe { self . v . split_at_mut ( self . chunk_size ) } ;
3083
3116
self . v = tail;
3084
- // SAFETY: Nothing points to or will point to the contents of this slice
3117
+ // SAFETY: Nothing else points to or will point to the contents of this slice.
3085
3118
Some ( unsafe { & mut * head } )
3086
3119
}
3087
3120
}
@@ -3098,10 +3131,12 @@ impl<'a, T> DoubleEndedIterator for RChunksExactMut<'a, T> {
3098
3131
let offset = ( len - n) * self . chunk_size ;
3099
3132
let start = self . v . len ( ) - offset;
3100
3133
let end = start + self . chunk_size ;
3134
+ // SAFETY: This type ensures that any split_at_mut on self.v is valid.
3101
3135
let ( tmp, tail) = unsafe { self . v . split_at_mut ( end) } ;
3136
+ // SAFETY: This type ensures that any split_at_mut on self.v is valid.
3102
3137
let ( _, nth_back) = unsafe { tmp. split_at_mut ( start) } ;
3103
3138
self . v = tail;
3104
- // SAFETY: Nothing points to or will point to the contents of this slice
3139
+ // SAFETY: Nothing else points to or will point to the contents of this slice.
3105
3140
Some ( unsafe { & mut * nth_back } )
3106
3141
}
3107
3142
}
0 commit comments