@@ -170,7 +170,7 @@ use std::slice;
170170// use std::vec::Drain;
171171use compare:: Compare ;
172172use core:: fmt;
173- use core:: mem:: { size_of, swap} ;
173+ use core:: mem:: { size_of, swap, ManuallyDrop } ;
174174use core:: ptr;
175175#[ cfg( feature = "serde" ) ]
176176use serde:: { Deserialize , Serialize } ;
@@ -1286,8 +1286,7 @@ impl<T, C: Compare<T>> BinaryHeap<T, C> {
12861286/// position with the value that was originally removed.
12871287struct Hole < ' a , T : ' a > {
12881288 data : & ' a mut [ T ] ,
1289- /// `elt` is always `Some` from new until drop.
1290- elt : Option < T > ,
1289+ elt : ManuallyDrop < T > ,
12911290 pos : usize ,
12921291}
12931292
@@ -1302,7 +1301,7 @@ impl<'a, T> Hole<'a, T> {
13021301 let elt = unsafe { ptr:: read ( data. get_unchecked ( pos) ) } ;
13031302 Hole {
13041303 data,
1305- elt : Some ( elt) ,
1304+ elt : ManuallyDrop :: new ( elt) ,
13061305 pos,
13071306 }
13081307 }
@@ -1315,7 +1314,7 @@ impl<'a, T> Hole<'a, T> {
13151314 /// Returns a reference to the element removed.
13161315 #[ inline]
13171316 fn element ( & self ) -> & T {
1318- self . elt . as_ref ( ) . unwrap ( )
1317+ & self . elt
13191318 }
13201319
13211320 /// Returns a reference to the element at `index`.
@@ -1351,7 +1350,7 @@ impl<'a, T> Drop for Hole<'a, T> {
13511350 // fill the hole again
13521351 unsafe {
13531352 let pos = self . pos ;
1354- ptr:: write ( self . data . get_unchecked_mut ( pos) , self . elt . take ( ) . unwrap ( ) ) ;
1353+ ptr:: copy_nonoverlapping ( & * self . elt , self . data . get_unchecked_mut ( pos) , 1 ) ;
13551354 }
13561355 }
13571356}
0 commit comments