@@ -16,7 +16,7 @@ use std::cmp::Ordering;
16
16
use std:: fmt:: { self , Debug , Display } ;
17
17
use std:: hash:: { Hash , Hasher } ;
18
18
use std:: marker:: PhantomData ;
19
- use std:: mem;
19
+ use std:: mem:: { self , ManuallyDrop } ;
20
20
use std:: ops:: { Deref , DerefMut } ;
21
21
use std:: ptr:: { self , NonNull } ;
22
22
use std:: rc:: Rc ;
@@ -194,9 +194,7 @@ impl<T: ?Sized> Gc<T> {
194
194
/// unsafe { Gc::from_raw(x_ptr) };
195
195
/// ```
196
196
pub fn into_raw ( this : Self ) -> * const T {
197
- let ptr: * const T = GcBox :: value_ptr ( this. inner_ptr ( ) ) ;
198
- mem:: forget ( this) ;
199
- ptr
197
+ GcBox :: value_ptr ( ManuallyDrop :: new ( this) . inner_ptr ( ) )
200
198
}
201
199
202
200
/// Constructs an `Gc` from a raw pointer.
@@ -771,16 +769,16 @@ impl<'a, T: ?Sized> GcCellRef<'a, T> {
771
769
U : ?Sized ,
772
770
F : FnOnce ( & T ) -> & U ,
773
771
{
774
- let ret = GcCellRef {
775
- flags : orig. flags ,
776
- value : f ( orig. value ) ,
777
- } ;
772
+ let value = f ( orig. value ) ;
778
773
779
774
// We have to tell the compiler not to call the destructor of GcCellRef,
780
775
// because it will update the borrow flags.
781
- std :: mem :: forget ( orig) ;
776
+ let orig = ManuallyDrop :: new ( orig) ;
782
777
783
- ret
778
+ GcCellRef {
779
+ flags : orig. flags ,
780
+ value,
781
+ }
784
782
}
785
783
786
784
/// Splits a `GcCellRef` into multiple `GcCellRef`s for different components of the borrowed data.
@@ -812,7 +810,11 @@ impl<'a, T: ?Sized> GcCellRef<'a, T> {
812
810
813
811
orig. flags . set ( orig. flags . get ( ) . add_reading ( ) ) ;
814
812
815
- let ret = (
813
+ // We have to tell the compiler not to call the destructor of GcCellRef,
814
+ // because it will update the borrow flags.
815
+ let orig = ManuallyDrop :: new ( orig) ;
816
+
817
+ (
816
818
GcCellRef {
817
819
flags : orig. flags ,
818
820
value : a,
@@ -821,13 +823,7 @@ impl<'a, T: ?Sized> GcCellRef<'a, T> {
821
823
flags : orig. flags ,
822
824
value : b,
823
825
} ,
824
- ) ;
825
-
826
- // We have to tell the compiler not to call the destructor of GcCellRef,
827
- // because it will update the borrow flags.
828
- std:: mem:: forget ( orig) ;
829
-
830
- ret
826
+ )
831
827
}
832
828
}
833
829
0 commit comments