@@ -260,7 +260,6 @@ pub struct BinaryHeap<T> {
260
260
#[ stable( feature = "binary_heap_peek_mut" , since = "1.12.0" ) ]
261
261
pub struct PeekMut < ' a , T : ' a + Ord > {
262
262
heap : & ' a mut BinaryHeap < T > ,
263
- sift : bool ,
264
263
}
265
264
266
265
#[ stable( feature = "collection_debug" , since = "1.17.0" ) ]
@@ -273,9 +272,7 @@ impl<T: Ord + fmt::Debug> fmt::Debug for PeekMut<'_, T> {
273
272
#[ stable( feature = "binary_heap_peek_mut" , since = "1.12.0" ) ]
274
273
impl < T : Ord > Drop for PeekMut < ' _ , T > {
275
274
fn drop ( & mut self ) {
276
- if self . sift {
277
- self . heap . sift_down ( 0 ) ;
278
- }
275
+ self . heap . sift_down ( 0 ) ;
279
276
}
280
277
}
281
278
@@ -301,10 +298,10 @@ impl<T: Ord> DerefMut for PeekMut<'_, T> {
301
298
impl < ' a , T : Ord > PeekMut < ' a , T > {
302
299
/// Removes the peeked value from the heap and returns it.
303
300
#[ 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 ( )
308
305
}
309
306
}
310
307
@@ -401,7 +398,7 @@ impl<T: Ord> BinaryHeap<T> {
401
398
/// Cost is *O*(1) in the worst case.
402
399
#[ stable( feature = "binary_heap_peek_mut" , since = "1.12.0" ) ]
403
400
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 } ) }
405
402
}
406
403
407
404
/// Removes the greatest item from the binary heap and returns it, or `None` if it
0 commit comments