1
Fork 0

Rollup merge of #58123 - lnicola:binary-heap-no-bounds-checks, r=sfackler

Avoid some bounds checks in binary_heap::{PeekMut,Hole}

Fixes #58121.
This commit is contained in:
kennytm 2019-02-07 13:57:38 +08:00 committed by GitHub
commit 8bc05bacbf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

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