Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions library/alloc/src/collections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ pub struct BinaryHeap<T> {
#[stable(feature = "binary_heap_peek_mut", since = "1.12.0")]
pub struct PeekMut<'a, T: 'a + Ord> {
heap: &'a mut BinaryHeap<T>,
sift: bool,
}

#[stable(feature = "collection_debug", since = "1.17.0")]
Expand All @@ -273,9 +272,7 @@ impl<T: Ord + fmt::Debug> fmt::Debug for PeekMut<'_, T> {
#[stable(feature = "binary_heap_peek_mut", since = "1.12.0")]
impl<T: Ord> Drop for PeekMut<'_, T> {
fn drop(&mut self) {
if self.sift {
self.heap.sift_down(0);
}
self.heap.sift_down(0);
}
}

Expand All @@ -301,10 +298,10 @@ impl<T: Ord> DerefMut for PeekMut<'_, T> {
impl<'a, T: Ord> PeekMut<'a, T> {
/// Removes the peeked value from the heap and returns it.
#[stable(feature = "binary_heap_peek_mut_pop", since = "1.18.0")]
pub fn pop(mut this: PeekMut<'a, T>) -> T {
let value = this.heap.pop().unwrap();
this.sift = false;
value
pub fn pop(this: PeekMut<'a, T>) -> T {
// Destructor is unnecessary since pop() already sifts heap
let mut this = ManuallyDrop::new(this);
this.heap.pop().unwrap()
}
}

Expand Down Expand Up @@ -401,7 +398,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 }) }
}

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