Skip to content

Remove DerefMut impl for Mutable Handle #573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions mozjs/src/gc/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ impl<'a, T> MutableHandle<'a, T> {
unsafe { *self.ptr = v }
}

/// Safety: GC must not run during the lifetime of the returned reference.
pub unsafe fn as_mut<'b>(&'b mut self) -> &'b mut T
where
'a: 'b,
{
&mut *(self.ptr)
}

/// Creates a copy of this object, with a shorter lifetime, that holds a
/// mutable borrow on the original object. When you write code that wants
/// to use a `MutableHandle` more than once, you will typically need to
Expand All @@ -241,6 +249,13 @@ impl<'a, T> MutableHandle<'a, T> {
}
}

impl<'a, T> MutableHandle<'a, Option<T>> {
pub fn take(&mut self) -> Option<T> {
// Safety: No GC occurs during take call
unsafe { self.as_mut().take() }
}
}

impl<'a, T> Deref for MutableHandle<'a, T> {
type Target = T;

Expand All @@ -249,12 +264,6 @@ impl<'a, T> Deref for MutableHandle<'a, T> {
}
}

impl<'a, T> DerefMut for MutableHandle<'a, T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.ptr }
}
}

impl HandleValue<'static> {
pub fn null() -> Self {
unsafe { Self::from_raw(RawHandleValue::null()) }
Expand Down
Loading