diff options
| author | Hanif Bin Ariffin <hanif.ariffin.4326@gmail.com> | 2020-04-25 21:18:47 -0400 |
|---|---|---|
| committer | Hanif Bin Ariffin <hanif.ariffin.4326@gmail.com> | 2020-06-13 15:06:22 -0400 |
| commit | c710461b02660cef1d077420d5b486b1d8f1f28b (patch) | |
| tree | 6bb06b96ddfe99e93dbafe65e96ae7202f86a703 /src/libcore/slice | |
| parent | c471519b999265c45595750e5b98de6865325650 (diff) | |
| download | rust-c710461b02660cef1d077420d5b486b1d8f1f28b.tar.gz rust-c710461b02660cef1d077420d5b486b1d8f1f28b.zip | |
Added some unsafety documentation to partition_equal
Diffstat (limited to 'src/libcore/slice')
| -rw-r--r-- | src/libcore/slice/sort.rs | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/src/libcore/slice/sort.rs b/src/libcore/slice/sort.rs index 046b2f6c13a..2ec4f43b1f7 100644 --- a/src/libcore/slice/sort.rs +++ b/src/libcore/slice/sort.rs @@ -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; } @@ -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; @@ -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; } |
