Added SAFETY comment as request

This commit is contained in:
Giacomo Stevanato 2020-11-09 22:34:31 +01:00
parent 8d1575365d
commit 387568cd56

View File

@ -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));