Skip to content

Commit

Permalink
Added SAFETY comment as request
Browse files Browse the repository at this point in the history
  • Loading branch information
SkiFire13 committed Nov 9, 2020
1 parent 8d15753 commit 387568c
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions library/alloc/src/collections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,10 @@ impl<T: Ord> BinaryHeap<T> {
let mut end = self.len();
while end > 1 {
end -= 1;
// SAFETY: `end` goes from `self.len() - 1` to 1 (both included),
// so it's always a valid index to access.
// It is safe to access index 0 (i.e. `ptr`), because
// 1 <= end < self.len(), which means self.len() >= 2.
unsafe {
let ptr = self.data.as_mut_ptr();
ptr::swap(ptr, ptr.add(end));
Expand Down

0 comments on commit 387568c

Please sign in to comment.