Skip to content

Commit 7349f2c

Browse files
committed
Added unsafety documentation to shift_head
1 parent 1fb612b commit 7349f2c

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/libcore/slice/sort.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Slice sorting
22
//!
3-
//! This module contains an sort algorithm based on Orson Peters' pattern-defeating quicksort,
3+
//! This module contains a sorting algorithm based on Orson Peters' pattern-defeating quicksort,
44
//! published at: https://github.com/orlp/pdqsort
55
//!
66
//! Unstable sorting is compatible with libcore because it doesn't allocate memory, unlike our
@@ -32,6 +32,20 @@ where
3232
F: FnMut(&T, &T) -> bool,
3333
{
3434
let len = v.len();
35+
// SAFETY: The unsafe operations below involves indexing without a bound check (`get_unchecked` and `get_unchecked_mut`)
36+
// and copying memory (`ptr::copy_nonoverlapping`).
37+
//
38+
// a. Indexing:
39+
// 1. We checked the size of the array to >=2.
40+
// 2. All the indexing that we will do is always between {0 <= index < len} at most.
41+
//
42+
// b. Memory copying
43+
// 1. We are obtaining pointers to references which are guaranteed to be valid.
44+
// 2. They cannot overlap because we obtain pointers to difference indices of the slice.
45+
// Namely, `i` and `i-1`.
46+
// 3. FIXME: Guarantees that the elements are properly aligned?
47+
//
48+
// See comments below for further detail.
3549
unsafe {
3650
// If the first two elements are out-of-order...
3751
if len >= 2 && is_less(v.get_unchecked(1), v.get_unchecked(0)) {

0 commit comments

Comments
 (0)