Skip to content

Commit af1e363

Browse files
committed
Set sift=true only when PeekMut yields a mutable reference
1 parent 924cd13 commit af1e363

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

library/alloc/src/collections/binary_heap.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ impl<T: Ord> Deref for PeekMut<'_, T> {
293293
impl<T: Ord> DerefMut for PeekMut<'_, T> {
294294
fn deref_mut(&mut self) -> &mut T {
295295
debug_assert!(!self.heap.is_empty());
296+
self.sift = true;
296297
// SAFE: PeekMut is only instantiated for non-empty heaps
297298
unsafe { self.heap.data.get_unchecked_mut(0) }
298299
}
@@ -401,7 +402,7 @@ impl<T: Ord> BinaryHeap<T> {
401402
/// Cost is *O*(1) in the worst case.
402403
#[stable(feature = "binary_heap_peek_mut", since = "1.12.0")]
403404
pub fn peek_mut(&mut self) -> Option<PeekMut<'_, T>> {
404-
if self.is_empty() { None } else { Some(PeekMut { heap: self, sift: true }) }
405+
if self.is_empty() { None } else { Some(PeekMut { heap: self, sift: false }) }
405406
}
406407

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

0 commit comments

Comments
 (0)