Skip to content

Commit 36f1e77

Browse files
authored
Remove DerefMut impl for RootedGuard (#572)
* Construct handle_mut without going through a mutable reference Signed-off-by: Greg Morenz <[email protected]> * Remove DerefMut impl for RootedGuard Signed-off-by: Greg Morenz <[email protected]> * Allow unrooted interior in MutableHandle Signed-off-by: Greg Morenz <[email protected]> --------- Signed-off-by: Greg Morenz <[email protected]>
1 parent db131fc commit 36f1e77

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

mozjs/src/gc/root.rs

+28-8
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,20 @@ impl<'a, T: 'a + RootKind> RootedGuard<'a, T> {
4343
}
4444

4545
pub fn handle_mut(&mut self) -> MutableHandle<T> {
46-
unsafe { MutableHandle::from_marked_location(self.deref_mut()) }
46+
unsafe { MutableHandle::from_marked_location(self.as_ptr()) }
47+
}
48+
49+
pub fn as_ptr(&self) -> *mut T {
50+
// SAFETY: self.root points to an inbounds allocation
51+
unsafe { (&raw mut (*self.root).ptr).cast() }
52+
}
53+
54+
/// Safety: GC must not run during the lifetime of the returned reference.
55+
pub unsafe fn as_mut<'b>(&'b mut self) -> &'b mut T
56+
where
57+
'a: 'b,
58+
{
59+
&mut *(self.as_ptr())
4760
}
4861

4962
pub fn get(&self) -> T
@@ -64,6 +77,16 @@ impl<'a, T: 'a + RootKind> RootedGuard<'a, T> {
6477
}
6578
}
6679

80+
impl<'a, T> RootedGuard<'a, Option<T>>
81+
where
82+
Option<T>: RootKind,
83+
{
84+
pub fn take(&mut self) -> Option<T> {
85+
// Safety: No GC occurs during take call
86+
unsafe { self.as_mut().take() }
87+
}
88+
}
89+
6790
impl<'a, T: 'a + RootKind> Deref for RootedGuard<'a, T> {
6891
type Target = T;
6992
fn deref(&self) -> &T {
@@ -72,13 +95,6 @@ impl<'a, T: 'a + RootKind> Deref for RootedGuard<'a, T> {
7295
}
7396
}
7497

75-
impl<'a, T: 'a + RootKind> DerefMut for RootedGuard<'a, T> {
76-
fn deref_mut(&mut self) -> &mut T {
77-
// SAFETY: The rooted value is initialized as long as we exist
78-
unsafe { (*self.root).ptr.assume_init_mut() }
79-
}
80-
}
81-
8298
impl<'a, T: 'a + RootKind> Drop for RootedGuard<'a, T> {
8399
fn drop(&mut self) {
84100
// SAFETY: The rooted value is initialized as long as we exist
@@ -112,6 +128,10 @@ impl<T> Clone for Handle<'_, T> {
112128

113129
impl<T> Copy for Handle<'_, T> {}
114130

131+
#[cfg_attr(
132+
feature = "crown",
133+
crown::unrooted_must_root_lint::allow_unrooted_interior
134+
)]
115135
pub struct MutableHandle<'a, T: 'a> {
116136
pub(crate) ptr: *mut T,
117137
anchor: PhantomData<&'a mut T>,

0 commit comments

Comments
 (0)