diff options
| author | kennytm <kennytm@gmail.com> | 2019-02-07 13:57:38 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-02-07 13:57:38 +0800 |
| commit | 8bc05bacbff8a4d66c7790d501718d9b7f2c090c (patch) | |
| tree | f6bd0320dbdf37832563f4ad4ec45c6bb7091f2d /src/liballoc | |
| parent | ed500e6cc4230e12ab012ea57a1778560ce03515 (diff) | |
| parent | ea72066f5c0cc93ea7efe73396eb691724fccaaf (diff) | |
| download | rust-8bc05bacbff8a4d66c7790d501718d9b7f2c090c.tar.gz rust-8bc05bacbff8a4d66c7790d501718d9b7f2c090c.zip | |
Rollup merge of #58123 - lnicola:binary-heap-no-bounds-checks, r=sfackler
Avoid some bounds checks in binary_heap::{PeekMut,Hole}
Fixes #58121.
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/collections/binary_heap.rs | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/src/liballoc/collections/binary_heap.rs b/src/liballoc/collections/binary_heap.rs index f97522140a8..6214e1ce245 100644 --- a/src/liballoc/collections/binary_heap.rs +++ b/src/liballoc/collections/binary_heap.rs @@ -248,14 +248,18 @@ impl<T: Ord> Drop for PeekMut<'_, T> { impl<T: Ord> Deref for PeekMut<'_, T> { type Target = T; fn deref(&self) -> &T { - &self.heap.data[0] + debug_assert!(!self.heap.is_empty()); + // SAFE: PeekMut is only instantiated for non-empty heaps + unsafe { self.heap.data.get_unchecked(0) } } } #[stable(feature = "binary_heap_peek_mut", since = "1.12.0")] impl<T: Ord> DerefMut for PeekMut<'_, T> { fn deref_mut(&mut self) -> &mut T { - &mut self.heap.data[0] + debug_assert!(!self.heap.is_empty()); + // SAFE: PeekMut is only instantiated for non-empty heaps + unsafe { self.heap.data.get_unchecked_mut(0) } } } @@ -865,7 +869,8 @@ impl<'a, T> Hole<'a, T> { #[inline] unsafe fn new(data: &'a mut [T], pos: usize) -> Self { debug_assert!(pos < data.len()); - let elt = ptr::read(&data[pos]); + // SAFE: pos should be inside the slice + let elt = ptr::read(data.get_unchecked(pos)); Hole { data, elt: ManuallyDrop::new(elt), |
