@@ -8,7 +8,8 @@ use core::iter::{
88 FusedIterator , InPlaceIterable , SourceIter , TrustedLen , TrustedRandomAccessNoCoerce ,
99} ;
1010use core:: marker:: PhantomData ;
11- use core:: mem:: { self } ;
11+ use core:: mem:: { self , ManuallyDrop } ;
12+ use core:: ops:: Deref ;
1213use core:: ptr:: { self , NonNull } ;
1314use core:: slice:: { self } ;
1415
@@ -32,7 +33,9 @@ pub struct IntoIter<
3233 pub ( super ) buf : NonNull < T > ,
3334 pub ( super ) phantom : PhantomData < T > ,
3435 pub ( super ) cap : usize ,
35- pub ( super ) alloc : A ,
36+ // the drop impl reconstructs a RawVec from buf, cap and alloc
37+ // to avoid dropping the allocator twice we need to wrap it into ManuallyDrop
38+ pub ( super ) alloc : ManuallyDrop < A > ,
3639 pub ( super ) ptr : * const T ,
3740 pub ( super ) end : * const T ,
3841}
@@ -295,11 +298,11 @@ where
295298impl < T : Clone , A : Allocator + Clone > Clone for IntoIter < T , A > {
296299 #[ cfg( not( test) ) ]
297300 fn clone ( & self ) -> Self {
298- self . as_slice ( ) . to_vec_in ( self . alloc . clone ( ) ) . into_iter ( )
301+ self . as_slice ( ) . to_vec_in ( self . alloc . deref ( ) . clone ( ) ) . into_iter ( )
299302 }
300303 #[ cfg( test) ]
301304 fn clone ( & self ) -> Self {
302- crate :: slice:: to_vec ( self . as_slice ( ) , self . alloc . clone ( ) ) . into_iter ( )
305+ crate :: slice:: to_vec ( self . as_slice ( ) , self . alloc . deref ( ) . clone ( ) ) . into_iter ( )
303306 }
304307}
305308
@@ -311,8 +314,8 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for IntoIter<T, A> {
311314 impl < T , A : Allocator > Drop for DropGuard < ' _ , T , A > {
312315 fn drop ( & mut self ) {
313316 unsafe {
314- // `IntoIter::alloc` is not used anymore after this
315- let alloc = ptr:: read ( & self . 0 . alloc ) ;
317+ // `IntoIter::alloc` is not used anymore after this and will be dropped by RawVec
318+ let alloc = ManuallyDrop :: into_inner ( ptr:: read ( & self . 0 . alloc ) ) ;
316319 // RawVec handles deallocation
317320 let _ = RawVec :: from_raw_parts_in ( self . 0 . buf . as_ptr ( ) , self . 0 . cap , alloc) ;
318321 }
0 commit comments