about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGiacomo Stevanato <giaco.stevanato@gmail.com>2020-11-09 22:34:31 +0100
committerGiacomo Stevanato <giaco.stevanato@gmail.com>2020-11-09 22:34:31 +0100
commit387568cd564317ca7491e6960ddcbe13beecae13 (patch)
tree0f544741db367fcc43a991bc79fa75742e748e5f
parent8d1575365dd9c3260d0aa44203b9ad169f0b5d74 (diff)
downloadrust-387568cd564317ca7491e6960ddcbe13beecae13.tar.gz
rust-387568cd564317ca7491e6960ddcbe13beecae13.zip
Added SAFETY comment as request
-rw-r--r--library/alloc/src/collections/binary_heap.rs4
1 files changed, 4 insertions, 0 deletions
diff --git a/library/alloc/src/collections/binary_heap.rs b/library/alloc/src/collections/binary_heap.rs
index 17f0668c0ea..97ebc12175f 100644
--- a/library/alloc/src/collections/binary_heap.rs
+++ b/library/alloc/src/collections/binary_heap.rs
@@ -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));