Skip to content

Commit 387568c

Browse files
committed
Added SAFETY comment as request
1 parent 8d15753 commit 387568c

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

library/alloc/src/collections/binary_heap.rs

+4
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,10 @@ impl<T: Ord> BinaryHeap<T> {
495495
let mut end = self.len();
496496
while end > 1 {
497497
end -= 1;
498+
// SAFETY: `end` goes from `self.len() - 1` to 1 (both included),
499+
// so it's always a valid index to access.
500+
// It is safe to access index 0 (i.e. `ptr`), because
501+
// 1 <= end < self.len(), which means self.len() >= 2.
498502
unsafe {
499503
let ptr = self.data.as_mut_ptr();
500504
ptr::swap(ptr, ptr.add(end));

0 commit comments

Comments
 (0)