Skip to content

Commit

Permalink
Set sift=true only when PeekMut yields a mutable reference
Browse files Browse the repository at this point in the history
  • Loading branch information
SkiFire13 committed Sep 19, 2020
1 parent 924cd13 commit af1e363
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion library/alloc/src/collections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ impl<T: Ord> Deref for PeekMut<'_, T> {
impl<T: Ord> DerefMut for PeekMut<'_, T> {
fn deref_mut(&mut self) -> &mut T {
debug_assert!(!self.heap.is_empty());
self.sift = true;
// SAFE: PeekMut is only instantiated for non-empty heaps
unsafe { self.heap.data.get_unchecked_mut(0) }
}
Expand Down Expand Up @@ -401,7 +402,7 @@ impl<T: Ord> BinaryHeap<T> {
/// Cost is *O*(1) in the worst case.
#[stable(feature = "binary_heap_peek_mut", since = "1.12.0")]
pub fn peek_mut(&mut self) -> Option<PeekMut<'_, T>> {
if self.is_empty() { None } else { Some(PeekMut { heap: self, sift: true }) }
if self.is_empty() { None } else { Some(PeekMut { heap: self, sift: false }) }
}

/// Removes the greatest item from the binary heap and returns it, or `None` if it
Expand Down

0 comments on commit af1e363

Please sign in to comment.