@@ -43,7 +43,20 @@ impl<'a, T: 'a + RootKind> RootedGuard<'a, T> {
43
43
}
44
44
45
45
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 ( ) )
47
60
}
48
61
49
62
pub fn get ( & self ) -> T
@@ -64,6 +77,16 @@ impl<'a, T: 'a + RootKind> RootedGuard<'a, T> {
64
77
}
65
78
}
66
79
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
+
67
90
impl < ' a , T : ' a + RootKind > Deref for RootedGuard < ' a , T > {
68
91
type Target = T ;
69
92
fn deref ( & self ) -> & T {
@@ -72,13 +95,6 @@ impl<'a, T: 'a + RootKind> Deref for RootedGuard<'a, T> {
72
95
}
73
96
}
74
97
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
-
82
98
impl < ' a , T : ' a + RootKind > Drop for RootedGuard < ' a , T > {
83
99
fn drop ( & mut self ) {
84
100
// SAFETY: The rooted value is initialized as long as we exist
@@ -112,6 +128,10 @@ impl<T> Clone for Handle<'_, T> {
112
128
113
129
impl < T > Copy for Handle < ' _ , T > { }
114
130
131
+ #[ cfg_attr(
132
+ feature = "crown" ,
133
+ crown:: unrooted_must_root_lint:: allow_unrooted_interior
134
+ ) ]
115
135
pub struct MutableHandle < ' a , T : ' a > {
116
136
pub ( crate ) ptr : * mut T ,
117
137
anchor : PhantomData < & ' a mut T > ,
0 commit comments