diff options
| author | Laurențiu Nicola <lnicola@dend.ro> | 2019-02-03 18:24:05 +0200 |
|---|---|---|
| committer | Laurențiu Nicola <lnicola@dend.ro> | 2019-02-03 20:38:08 +0200 |
| commit | ea72066f5c0cc93ea7efe73396eb691724fccaaf (patch) | |
| tree | f719c9dcf74901e52afb3863dcdffdf32374cc58 /src/liballoc | |
| parent | fc6e9a2845e8bb4560811ed21136483a596505bb (diff) | |
| download | rust-ea72066f5c0cc93ea7efe73396eb691724fccaaf.tar.gz rust-ea72066f5c0cc93ea7efe73396eb691724fccaaf.zip | |
Avoid some bounds checks in binary_heap::{PeekMut,Hole}
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 ad544e6015e..473f7aee5c7 100644 --- a/src/liballoc/collections/binary_heap.rs +++ b/src/liballoc/collections/binary_heap.rs @@ -248,14 +248,18 @@ impl<'a, T: Ord> Drop for PeekMut<'a, T> { impl<'a, T: Ord> Deref for PeekMut<'a, 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<'a, T: Ord> DerefMut for PeekMut<'a, 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), |
