@@ -435,15 +435,17 @@ where
435
435
// Find the first pair of out-of-order elements.
436
436
let mut l = 0 ;
437
437
let mut r = v. len ( ) ;
438
+
439
+ // SAFETY: The unsafety below involves indexing an array.
440
+ // For the first one: we already do the bound checking here with `l<r`.
441
+ // For the secondn one: the minimum value for `l` is 0 and the maximum value for `r` is `v.len().`
438
442
unsafe {
439
443
// Find the first element greater than or equal to the pivot.
440
- // SAFETY: We already do the bound checking here with `l<r`.
441
444
while l < r && is_less ( v. get_unchecked ( l) , pivot) {
442
445
l += 1 ;
443
446
}
444
447
445
448
// Find the last element smaller that the pivot.
446
- // SAFETY: The minimum value for `l` is 0 and the maximum value for `r` is `v.len().`
447
449
while l < r && !is_less ( v. get_unchecked ( r - 1 ) , pivot) {
448
450
r -= 1 ;
449
451
}
@@ -477,6 +479,7 @@ where
477
479
478
480
// Read the pivot into a stack-allocated variable for efficiency. If a following comparison
479
481
// operation panics, the pivot will be automatically written back into the slice.
482
+ // SAFETY: The pointer here is valid because it is obtained from a reference to a slice.
480
483
let mut tmp = mem:: ManuallyDrop :: new ( unsafe { ptr:: read ( pivot) } ) ;
481
484
let _pivot_guard = CopyOnDrop { src : & mut * tmp, dest : pivot } ;
482
485
let pivot = & * tmp;
@@ -485,15 +488,16 @@ where
485
488
let mut l = 0 ;
486
489
let mut r = v. len ( ) ;
487
490
loop {
491
+ // SAFETY: The unsafety below involves indexing an array.
492
+ // For the first one: we already do the bound checking here with `l<r`.
493
+ // For the second one: the minimum value for `l` is 0 and the maximum value for `r` is `v.len().`
488
494
unsafe {
489
495
// Find the first element greater than the pivot.
490
- // SAFETY: We already do the bound checking here with `l<r`
491
496
while l < r && !is_less ( pivot, v. get_unchecked ( l) ) {
492
497
l += 1 ;
493
498
}
494
499
495
500
// Find the last element equal to the pivot.
496
- // SAFETY: The minimum value for `l` is 0 and the maximum value for `r` is `v.len().`
497
501
while l < r && is_less ( pivot, v. get_unchecked ( r - 1 ) ) {
498
502
r -= 1 ;
499
503
}
0 commit comments