@@ -2187,7 +2187,9 @@ impl<T: Clone, A: Allocator + Clone> Arc<T, A> {
2187
2187
// either unique to begin with, or became one upon cloning the contents.
2188
2188
unsafe { Self :: get_mut_unchecked ( this) }
2189
2189
}
2190
+ }
2190
2191
2192
+ impl < T : Clone , A : Allocator > Arc < T , A > {
2191
2193
/// If we have the only reference to `T` then unwrap it. Otherwise, clone `T` and return the
2192
2194
/// clone.
2193
2195
///
@@ -2432,7 +2434,7 @@ unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Arc<T, A> {
2432
2434
}
2433
2435
}
2434
2436
2435
- impl < A : Allocator + Clone > Arc < dyn Any + Send + Sync , A > {
2437
+ impl < A : Allocator > Arc < dyn Any + Send + Sync , A > {
2436
2438
/// Attempt to downcast the `Arc<dyn Any + Send + Sync>` to a concrete type.
2437
2439
///
2438
2440
/// # Examples
@@ -2458,12 +2460,11 @@ impl<A: Allocator + Clone> Arc<dyn Any + Send + Sync, A> {
2458
2460
T : Any + Send + Sync ,
2459
2461
{
2460
2462
if ( * self ) . is :: < T > ( ) {
2461
- unsafe {
2462
- let ptr = self . ptr . cast :: < ArcInner < T > > ( ) ;
2463
- let alloc = self . alloc . clone ( ) ;
2464
- mem:: forget ( self ) ;
2465
- Ok ( Arc :: from_inner_in ( ptr, alloc) )
2466
- }
2463
+ let this = mem:: ManuallyDrop :: new ( self ) ;
2464
+ let ptr = this. ptr . cast :: < ArcInner < T > > ( ) ;
2465
+ // Safety: `this` is ManuallyDrop so the allocator will not be double-dropped
2466
+ let alloc = unsafe { ptr:: read ( & this. alloc ) } ;
2467
+ unsafe { Ok ( Arc :: from_inner_in ( ptr, alloc) ) }
2467
2468
} else {
2468
2469
Err ( self )
2469
2470
}
@@ -2501,12 +2502,11 @@ impl<A: Allocator + Clone> Arc<dyn Any + Send + Sync, A> {
2501
2502
where
2502
2503
T : Any + Send + Sync ,
2503
2504
{
2504
- unsafe {
2505
- let ptr = self . ptr . cast :: < ArcInner < T > > ( ) ;
2506
- let alloc = self . alloc . clone ( ) ;
2507
- mem:: forget ( self ) ;
2508
- Arc :: from_inner_in ( ptr, alloc)
2509
- }
2505
+ let this = mem:: ManuallyDrop :: new ( self ) ;
2506
+ let ptr = this. ptr . cast :: < ArcInner < T > > ( ) ;
2507
+ // Safety: `this` is ManuallyDrop so the allocator will not be double-dropped
2508
+ let alloc = unsafe { ptr:: read ( & this. alloc ) } ;
2509
+ unsafe { Arc :: from_inner_in ( ptr, alloc) }
2510
2510
}
2511
2511
}
2512
2512
@@ -3432,7 +3432,7 @@ impl<T: ?Sized, A: Allocator> From<Box<T, A>> for Arc<T, A> {
3432
3432
3433
3433
#[ cfg( not( no_global_oom_handling) ) ]
3434
3434
#[ stable( feature = "shared_from_slice" , since = "1.21.0" ) ]
3435
- impl < T , A : Allocator + Clone > From < Vec < T , A > > for Arc < [ T ] , A > {
3435
+ impl < T , A : Allocator > From < Vec < T , A > > for Arc < [ T ] , A > {
3436
3436
/// Allocate a reference-counted slice and move `v`'s items into it.
3437
3437
///
3438
3438
/// # Example
@@ -3507,13 +3507,13 @@ impl From<Arc<str>> for Arc<[u8]> {
3507
3507
}
3508
3508
3509
3509
#[ stable( feature = "boxed_slice_try_from" , since = "1.43.0" ) ]
3510
- impl < T , A : Allocator + Clone , const N : usize > TryFrom < Arc < [ T ] , A > > for Arc < [ T ; N ] , A > {
3510
+ impl < T , A : Allocator , const N : usize > TryFrom < Arc < [ T ] , A > > for Arc < [ T ; N ] , A > {
3511
3511
type Error = Arc < [ T ] , A > ;
3512
3512
3513
3513
fn try_from ( boxed_slice : Arc < [ T ] , A > ) -> Result < Self , Self :: Error > {
3514
3514
if boxed_slice. len ( ) == N {
3515
- let alloc = boxed_slice . alloc . clone ( ) ;
3516
- Ok ( unsafe { Arc :: from_raw_in ( Arc :: into_raw ( boxed_slice ) as * mut [ T ; N ] , alloc) } )
3515
+ let ( ptr , alloc) = Arc :: into_raw_with_allocator ( boxed_slice ) ;
3516
+ Ok ( unsafe { Arc :: from_raw_in ( ptr as * mut [ T ; N ] , alloc) } )
3517
3517
} else {
3518
3518
Err ( boxed_slice)
3519
3519
}
0 commit comments