@@ -8,7 +8,8 @@ use core::iter::{
8
8
FusedIterator , InPlaceIterable , SourceIter , TrustedLen , TrustedRandomAccessNoCoerce ,
9
9
} ;
10
10
use core:: marker:: PhantomData ;
11
- use core:: mem:: { self } ;
11
+ use core:: mem:: { self , ManuallyDrop } ;
12
+ use core:: ops:: Deref ;
12
13
use core:: ptr:: { self , NonNull } ;
13
14
use core:: slice:: { self } ;
14
15
@@ -32,7 +33,9 @@ pub struct IntoIter<
32
33
pub ( super ) buf : NonNull < T > ,
33
34
pub ( super ) phantom : PhantomData < T > ,
34
35
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 > ,
36
39
pub ( super ) ptr : * const T ,
37
40
pub ( super ) end : * const T ,
38
41
}
@@ -295,11 +298,11 @@ where
295
298
impl < T : Clone , A : Allocator + Clone > Clone for IntoIter < T , A > {
296
299
#[ cfg( not( test) ) ]
297
300
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 ( )
299
302
}
300
303
#[ cfg( test) ]
301
304
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 ( )
303
306
}
304
307
}
305
308
@@ -311,8 +314,8 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for IntoIter<T, A> {
311
314
impl < T , A : Allocator > Drop for DropGuard < ' _ , T , A > {
312
315
fn drop ( & mut self ) {
313
316
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 ) ) ;
316
319
// RawVec handles deallocation
317
320
let _ = RawVec :: from_raw_parts_in ( self . 0 . buf . as_ptr ( ) , self . 0 . cap , alloc) ;
318
321
}
0 commit comments