Skip to content

Commit 04e1612

Browse files
committed
Simplify PeekMut struct and destructor
1 parent 80cacd7 commit 04e1612

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

library/alloc/src/collections/binary_heap.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ pub struct BinaryHeap<T> {
260260
#[stable(feature = "binary_heap_peek_mut", since = "1.12.0")]
261261
pub struct PeekMut<'a, T: 'a + Ord> {
262262
heap: &'a mut BinaryHeap<T>,
263-
sift: bool,
264263
}
265264

266265
#[stable(feature = "collection_debug", since = "1.17.0")]
@@ -273,9 +272,7 @@ impl<T: Ord + fmt::Debug> fmt::Debug for PeekMut<'_, T> {
273272
#[stable(feature = "binary_heap_peek_mut", since = "1.12.0")]
274273
impl<T: Ord> Drop for PeekMut<'_, T> {
275274
fn drop(&mut self) {
276-
if self.sift {
277-
self.heap.sift_down(0);
278-
}
275+
self.heap.sift_down(0);
279276
}
280277
}
281278

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

@@ -401,7 +398,7 @@ impl<T: Ord> BinaryHeap<T> {
401398
/// Cost is *O*(1) in the worst case.
402399
#[stable(feature = "binary_heap_peek_mut", since = "1.12.0")]
403400
pub fn peek_mut(&mut self) -> Option<PeekMut<'_, T>> {
404-
if self.is_empty() { None } else { Some(PeekMut { heap: self, sift: true }) }
401+
if self.is_empty() { None } else { Some(PeekMut { heap: self }) }
405402
}
406403

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

0 commit comments

Comments
 (0)