@@ -1695,9 +1695,9 @@ impl<T> [T] {
1695
1695
while size > 1 {
1696
1696
let half = size / 2 ;
1697
1697
let mid = base + half;
1698
- // SAFETY:
1699
- // mid >= 0: by definition
1700
- // mid < size: mid = size / 2 + size / 4 + size / 8 ...
1698
+ // SAFETY: the call is made safe by the following inconstants:
1699
+ // - ` mid >= 0` : by definition
1700
+ // - ` mid < size`: ` mid = size / 2 + size / 4 + size / 8 ...`
1701
1701
let cmp = f ( unsafe { s. get_unchecked ( mid) } ) ;
1702
1702
base = if cmp == Greater { base } else { mid } ;
1703
1703
size -= half;
@@ -2690,6 +2690,7 @@ impl<T> [T] {
2690
2690
// First, find at what point do we split between the first and 2nd slice. Easy with
2691
2691
// ptr.align_offset.
2692
2692
let ptr = self . as_ptr ( ) ;
2693
+ // SAFETY: See the `align_to_mut` method for the detailed safety comment.
2693
2694
let offset = unsafe { crate :: ptr:: align_offset ( ptr, mem:: align_of :: < U > ( ) ) } ;
2694
2695
if offset > self . len ( ) {
2695
2696
( self , & [ ] , & [ ] )
@@ -2749,6 +2750,13 @@ impl<T> [T] {
2749
2750
// First, find at what point do we split between the first and 2nd slice. Easy with
2750
2751
// ptr.align_offset.
2751
2752
let ptr = self . as_ptr ( ) ;
2753
+ // SAFETY: Here we are ensuring we will use aligned pointers for U for the
2754
+ // rest of the method. This is done by passing a pointer to &[T] with an
2755
+ // alignment targeted for U.
2756
+ // `crate::ptr::align_offset` is called with a correctly aligned and
2757
+ // valid pointer `ptr` (it comes from a reference to `self`) and with
2758
+ // a size that is a power of two (since it comes from the alignement for U),
2759
+ // satisfying its safety constraints.
2752
2760
let offset = unsafe { crate :: ptr:: align_offset ( ptr, mem:: align_of :: < U > ( ) ) } ;
2753
2761
if offset > self . len ( ) {
2754
2762
( self , & mut [ ] , & mut [ ] )
@@ -2874,15 +2882,13 @@ impl<T> [T] {
2874
2882
2875
2883
while left != right {
2876
2884
let mid = left + ( right - left) / 2 ;
2877
- // SAFETY:
2878
- // When left < right, left <= mid < right.
2879
- // Therefore left always increases and right always decreases,
2880
- // and eigher of them is selected.
2881
- // In both cases left <= right is satisfied.
2882
- // Therefore if left < right in a step,
2883
- // left <= right is satisfied in the next step.
2884
- // Therefore as long as left != right, 0 <= left < right <= len is satisfied
2885
- // and if this case 0 <= mid < len is satisfied too.
2885
+ // SAFETY: When `left < right`, `left <= mid < right`.
2886
+ // Therefore `left` always increases and `right` always decreases,
2887
+ // and either of them is selected. In both cases `left <= right` is
2888
+ // satisfied. Therefore if `left < right` in a step, `left <= right`
2889
+ // is satisfied in the next step. Therefore as long as `left != right`,
2890
+ // `0 <= left < right <= len` is satisfied and if this case
2891
+ // `0 <= mid < len` is satisfied too.
2886
2892
let value = unsafe { self . get_unchecked ( mid) } ;
2887
2893
if pred ( value) {
2888
2894
left = mid + 1 ;
@@ -3002,7 +3008,8 @@ fn is_ascii(s: &[u8]) -> bool {
3002
3008
// above.
3003
3009
debug_assert ! ( offset_to_aligned <= len) ;
3004
3010
3005
- // word_ptr is the (properly aligned) usize ptr we use to read the middle chunk of the slice.
3011
+ // SAFETY: word_ptr is the (properly aligned) usize ptr we use to read the
3012
+ // middle chunk of the slice.
3006
3013
let mut word_ptr = unsafe { start. add ( offset_to_aligned) as * const usize } ;
3007
3014
3008
3015
// `byte_pos` is the byte index of `word_ptr`, used for loop end checks.
@@ -5660,6 +5667,8 @@ impl<T, const N: usize> FusedIterator for ArrayChunks<'_, T, N> {}
5660
5667
#[ unstable( feature = "array_chunks" , issue = "74985" ) ]
5661
5668
unsafe impl < ' a , T , const N : usize > TrustedRandomAccess for ArrayChunks < ' a , T , N > {
5662
5669
unsafe fn get_unchecked ( & mut self , i : usize ) -> & ' a [ T ; N ] {
5670
+ // SAFETY: The safety guarantees of `get_unchecked` are transferred to
5671
+ // the caller.
5663
5672
unsafe { self . iter . get_unchecked ( i) }
5664
5673
}
5665
5674
fn may_have_side_effect ( ) -> bool {
0 commit comments