Skip to content

Commit

Permalink
Simplify with ManuallyDrop
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk committed Jul 17, 2024
1 parent 8a5cfdd commit d25e341
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions gc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::cmp::Ordering;
use std::fmt::{self, Debug, Display};
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::mem;
use std::mem::{self, ManuallyDrop};
use std::ops::{Deref, DerefMut};
use std::ptr::{self, NonNull};
use std::rc::Rc;
Expand Down Expand Up @@ -194,9 +194,7 @@ impl<T: ?Sized> Gc<T> {
/// unsafe { Gc::from_raw(x_ptr) };
/// ```
pub fn into_raw(this: Self) -> *const T {
let ptr: *const T = GcBox::value_ptr(this.inner_ptr());
mem::forget(this);
ptr
GcBox::value_ptr(ManuallyDrop::new(this).inner_ptr())
}

/// Constructs an `Gc` from a raw pointer.
Expand Down Expand Up @@ -771,16 +769,16 @@ impl<'a, T: ?Sized> GcCellRef<'a, T> {
U: ?Sized,
F: FnOnce(&T) -> &U,
{
let ret = GcCellRef {
flags: orig.flags,
value: f(orig.value),
};
let value = f(orig.value);

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

ret
GcCellRef {
flags: orig.flags,
value,
}
}

/// Splits a `GcCellRef` into multiple `GcCellRef`s for different components of the borrowed data.
Expand Down Expand Up @@ -812,7 +810,11 @@ impl<'a, T: ?Sized> GcCellRef<'a, T> {

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

let ret = (
// We have to tell the compiler not to call the destructor of GcCellRef,
// because it will update the borrow flags.
let orig = ManuallyDrop::new(orig);

(
GcCellRef {
flags: orig.flags,
value: a,
Expand All @@ -821,13 +823,7 @@ impl<'a, T: ?Sized> GcCellRef<'a, T> {
flags: orig.flags,
value: b,
},
);

// We have to tell the compiler not to call the destructor of GcCellRef,
// because it will update the borrow flags.
std::mem::forget(orig);

ret
)
}
}

Expand Down

0 comments on commit d25e341

Please sign in to comment.