Skip to content

Commit

Permalink
Added some unsafety documentation to partition_equal
Browse files Browse the repository at this point in the history
  • Loading branch information
hbina committed Jun 13, 2020
1 parent c471519 commit c710461
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/libcore/slice/sort.rs
Expand Up @@ -435,15 +435,17 @@ where
// Find the first pair of out-of-order elements.
let mut l = 0;
let mut r = v.len();

// SAFETY: The unsafety below involves indexing an array.
// For the first one: we already do the bound checking here with `l<r`.
// For the secondn one: the minimum value for `l` is 0 and the maximum value for `r` is `v.len().`
unsafe {
// Find the first element greater than or equal to the pivot.
// SAFETY: We already do the bound checking here with `l<r`.
while l < r && is_less(v.get_unchecked(l), pivot) {
l += 1;
}

// Find the last element smaller that the pivot.
// SAFETY: The minimum value for `l` is 0 and the maximum value for `r` is `v.len().`
while l < r && !is_less(v.get_unchecked(r - 1), pivot) {
r -= 1;
}
Expand Down Expand Up @@ -477,6 +479,7 @@ where

// Read the pivot into a stack-allocated variable for efficiency. If a following comparison
// operation panics, the pivot will be automatically written back into the slice.
// SAFETY: The pointer here is valid because it is obtained from a reference to a slice.
let mut tmp = mem::ManuallyDrop::new(unsafe { ptr::read(pivot) });
let _pivot_guard = CopyOnDrop { src: &mut *tmp, dest: pivot };
let pivot = &*tmp;
Expand All @@ -485,15 +488,16 @@ where
let mut l = 0;
let mut r = v.len();
loop {
// SAFETY: The unsafety below involves indexing an array.
// For the first one: we already do the bound checking here with `l<r`.
// For the second one: the minimum value for `l` is 0 and the maximum value for `r` is `v.len().`
unsafe {
// Find the first element greater than the pivot.
// SAFETY: We already do the bound checking here with `l<r`
while l < r && !is_less(pivot, v.get_unchecked(l)) {
l += 1;
}

// Find the last element equal to the pivot.
// SAFETY: The minimum value for `l` is 0 and the maximum value for `r` is `v.len().`
while l < r && is_less(pivot, v.get_unchecked(r - 1)) {
r -= 1;
}
Expand Down

0 comments on commit c710461

Please sign in to comment.