Skip to content

Commit

Permalink
Avoid some bounds checks in binary_heap::{PeekMut,Hole}
Browse files Browse the repository at this point in the history
  • Loading branch information
lnicola committed Feb 3, 2019
1 parent fc6e9a2 commit ea72066
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/liballoc/collections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
}

Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit ea72066

Please sign in to comment.