pub struct MutableHandle<'a, T: 'a> {
pub(crate) ptr: NonNull<T>,
anchor: PhantomData<&'a mut T>,
}Fields§
§ptr: NonNull<T>§anchor: PhantomData<&'a mut T>Implementations§
Source§impl<'a, T> MutableHandle<'a, T>
impl<'a, T> MutableHandle<'a, T>
pub unsafe fn from_marked_location(ptr: *mut T) -> Self
pub unsafe fn from_raw(handle: RawMutableHandle<T>) -> Self
pub fn handle(&self) -> Handle<'a, T>
pub fn get(&self) -> Twhere
T: Copy,
pub fn set(&mut self, v: T)where
T: Copy,
Sourcepub fn as_ref<'s: 'r, 'cx: 'r, 'r>(&'s self, _no_gc: &'cx NoGC) -> &'r Twhere
'a: 's,
pub fn as_ref<'s: 'r, 'cx: 'r, 'r>(&'s self, _no_gc: &'cx NoGC) -> &'r Twhere
'a: 's,
Obtains a reference to the value pointed to by this handle.
While this reference is alive, no GC can occur, because of the _no_gc argument:
use mozjs::context::*;
use mozjs::jsapi::JSObject;
use mozjs::rooted;
fn gc(cx: &mut JSContext) {}
fn f(cx: &mut JSContext, obj: *mut JSObject) {
rooted!(&in(cx) let mut root = obj);
let handle = root.handle_mut();
let r = handle.as_ref(cx);
gc(cx); // cannot call gc while r (thus cx borrow) is alive
drop(r); // otherwise rust automatically drops r before gc call
}Sourcepub fn as_mut_ref<'s: 'r, 'cx: 'r, 'r>(
&'s mut self,
_no_gc: &'cx NoGC,
) -> &'r mut Twhere
'a: 's,
pub fn as_mut_ref<'s: 'r, 'cx: 'r, 'r>(
&'s mut self,
_no_gc: &'cx NoGC,
) -> &'r mut Twhere
'a: 's,
Obtains a reference to the value pointed to by this handle.
While this reference is alive, no GC can occur, because of the _no_gc argument:
use mozjs::context::*;
use mozjs::jsapi::JSObject;
use mozjs::rooted;
fn gc(cx: &mut JSContext) {}
fn f(cx: &mut JSContext, obj: *mut JSObject) {
rooted!(&in(cx) let mut root = obj);
let mut handle = root.handle_mut();
let r = handle.as_mut_ref(cx);
gc(cx); // cannot call gc while r (thus cx borrow) is alive
drop(r); // otherwise rust automatically drops r before gc call
}Sourcepub unsafe fn as_mut<'b>(&'b mut self) -> &'b mut Twhere
'a: 'b,
pub unsafe fn as_mut<'b>(&'b mut self) -> &'b mut Twhere
'a: 'b,
Safety: GC must not run during the lifetime of the returned reference.
Use MutableHandle::as_mut_ref instead.
Sourcepub fn reborrow<'b>(&'b mut self) -> MutableHandle<'b, T>where
'a: 'b,
pub fn reborrow<'b>(&'b mut self) -> MutableHandle<'b, T>where
'a: 'b,
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
call reborrow on all but the last usage. The same way that you might
naively clone a type to allow it to be passed to multiple functions.
This is the same thing that happens with regular mutable references, except there the compiler implicitly inserts the reborrow calls. Until rust gains a feature to implicitly reborrow other types, we have to do it by hand.