Skip to content

Commit d25e341

Browse files
committed
Simplify with ManuallyDrop
Signed-off-by: Anders Kaseorg <[email protected]>
1 parent 8a5cfdd commit d25e341

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

gc/src/lib.rs

+14-18
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::cmp::Ordering;
1616
use std::fmt::{self, Debug, Display};
1717
use std::hash::{Hash, Hasher};
1818
use std::marker::PhantomData;
19-
use std::mem;
19+
use std::mem::{self, ManuallyDrop};
2020
use std::ops::{Deref, DerefMut};
2121
use std::ptr::{self, NonNull};
2222
use std::rc::Rc;
@@ -194,9 +194,7 @@ impl<T: ?Sized> Gc<T> {
194194
/// unsafe { Gc::from_raw(x_ptr) };
195195
/// ```
196196
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())
200198
}
201199

202200
/// Constructs an `Gc` from a raw pointer.
@@ -771,16 +769,16 @@ impl<'a, T: ?Sized> GcCellRef<'a, T> {
771769
U: ?Sized,
772770
F: FnOnce(&T) -> &U,
773771
{
774-
let ret = GcCellRef {
775-
flags: orig.flags,
776-
value: f(orig.value),
777-
};
772+
let value = f(orig.value);
778773

779774
// We have to tell the compiler not to call the destructor of GcCellRef,
780775
// because it will update the borrow flags.
781-
std::mem::forget(orig);
776+
let orig = ManuallyDrop::new(orig);
782777

783-
ret
778+
GcCellRef {
779+
flags: orig.flags,
780+
value,
781+
}
784782
}
785783

786784
/// 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> {
812810

813811
orig.flags.set(orig.flags.get().add_reading());
814812

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+
(
816818
GcCellRef {
817819
flags: orig.flags,
818820
value: a,
@@ -821,13 +823,7 @@ impl<'a, T: ?Sized> GcCellRef<'a, T> {
821823
flags: orig.flags,
822824
value: b,
823825
},
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+
)
831827
}
832828
}
833829

0 commit comments

Comments
 (0)