diff --git a/mozjs/src/gc/root.rs b/mozjs/src/gc/root.rs index 770a2f0f60..1682a8ba74 100644 --- a/mozjs/src/gc/root.rs +++ b/mozjs/src/gc/root.rs @@ -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 @@ -241,6 +249,13 @@ impl<'a, T> MutableHandle<'a, T> { } } +impl<'a, T> MutableHandle<'a, Option> { + pub fn take(&mut self) -> Option { + // Safety: No GC occurs during take call + unsafe { self.as_mut().take() } + } +} + impl<'a, T> Deref for MutableHandle<'a, T> { type Target = T; @@ -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()) }