@@ -20,7 +20,7 @@ use core::intrinsics::abort;
20
20
#[ cfg( not( no_global_oom_handling) ) ]
21
21
use core:: iter;
22
22
use core:: marker:: { PhantomData , Unsize } ;
23
- use core:: mem:: { self , align_of_val_raw} ;
23
+ use core:: mem:: { self , align_of_val_raw, ManuallyDrop } ;
24
24
use core:: ops:: { CoerceUnsized , Deref , DerefPure , DispatchFromDyn , Receiver } ;
25
25
use core:: panic:: { RefUnwindSafe , UnwindSafe } ;
26
26
use core:: pin:: Pin ;
@@ -960,16 +960,14 @@ impl<T, A: Allocator> Arc<T, A> {
960
960
961
961
acquire ! ( this. inner( ) . strong) ;
962
962
963
- unsafe {
964
- let elem = ptr:: read ( & this. ptr . as_ref ( ) . data ) ;
965
- let alloc = ptr:: read ( & this. alloc ) ; // copy the allocator
963
+ let this = ManuallyDrop :: new ( this ) ;
964
+ let elem: T = unsafe { ptr:: read ( & this. ptr . as_ref ( ) . data ) } ;
965
+ let alloc: A = unsafe { ptr:: read ( & this. alloc ) } ; // copy the allocator
966
966
967
- // Make a weak pointer to clean up the implicit strong-weak reference
968
- let _weak = Weak { ptr : this. ptr , alloc } ;
969
- mem:: forget ( this) ;
967
+ // Make a weak pointer to clean up the implicit strong-weak reference
968
+ let _weak = Weak { ptr : this. ptr , alloc } ;
970
969
971
- Ok ( elem)
972
- }
970
+ Ok ( elem)
973
971
}
974
972
975
973
/// Returns the inner value, if the `Arc` has exactly one strong reference.
@@ -1493,9 +1491,8 @@ impl<T: ?Sized, A: Allocator> Arc<T, A> {
1493
1491
#[ stable( feature = "rc_raw" , since = "1.17.0" ) ]
1494
1492
#[ rustc_never_returns_null_ptr]
1495
1493
pub fn into_raw ( this : Self ) -> * const T {
1496
- let ptr = Self :: as_ptr ( & this) ;
1497
- mem:: forget ( this) ;
1498
- ptr
1494
+ let this = ManuallyDrop :: new ( this) ;
1495
+ Self :: as_ptr ( & * this)
1499
1496
}
1500
1497
1501
1498
/// Consumes the `Arc`, returning the wrapped pointer and allocator.
@@ -2801,9 +2798,7 @@ impl<T: ?Sized, A: Allocator> Weak<T, A> {
2801
2798
#[ must_use = "losing the pointer will leak memory" ]
2802
2799
#[ stable( feature = "weak_into_raw" , since = "1.45.0" ) ]
2803
2800
pub fn into_raw ( self ) -> * const T {
2804
- let result = self . as_ptr ( ) ;
2805
- mem:: forget ( self ) ;
2806
- result
2801
+ ManuallyDrop :: new ( self ) . as_ptr ( )
2807
2802
}
2808
2803
2809
2804
/// Consumes the `Weak<T>`, returning the wrapped pointer and allocator.
@@ -3875,13 +3870,14 @@ impl<T: ?Sized, A: Allocator> UniqueArcUninit<T, A> {
3875
3870
/// # Safety
3876
3871
///
3877
3872
/// The data must have been initialized (by writing to [`Self::data_ptr()`]).
3878
- unsafe fn into_arc ( mut self ) -> Arc < T , A > {
3879
- let ptr = self . ptr ;
3880
- let alloc = self . alloc . take ( ) . unwrap ( ) ;
3881
- mem:: forget ( self ) ;
3873
+ unsafe fn into_arc ( self ) -> Arc < T , A > {
3874
+ let mut this = ManuallyDrop :: new ( self ) ;
3875
+ let ptr = this. ptr . as_ptr ( ) ;
3876
+ let alloc = this. alloc . take ( ) . unwrap ( ) ;
3877
+
3882
3878
// SAFETY: The pointer is valid as per `UniqueArcUninit::new`, and the caller is responsible
3883
3879
// for having initialized the data.
3884
- unsafe { Arc :: from_ptr_in ( ptr. as_ptr ( ) , alloc) }
3880
+ unsafe { Arc :: from_ptr_in ( ptr, alloc) }
3885
3881
}
3886
3882
}
3887
3883
0 commit comments