pub struct JSCell<T> {
inner: UnsafeCell<T>,
}Expand description
A cell for interior mutability, that (ab)uses NoGC as a borrow token for ensuring safety.
If inner type is Copy one should use std::cell::Cell instead.
§Examples
use mozjs::context::NoGC;
use mozjs::cell::JSCell;
fn f(no_gc: &mut NoGC, cell: &JSCell<String>) {
{
let borrow = cell.borrow(no_gc);
} // borrow goes out of scope here
cell.set(no_gc, "Hello".to_string());
{
let borrow_mut = cell.borrow_mut(no_gc);
} // borrow_mut goes out of scope here
}§Non-Examples
These fail to compile:
ⓘ
use mozjs::context::NoGC;
use mozjs::cell::JSCell;
fn mut_borrow_after_borrow(no_gc: &mut NoGC, cell: &JSCell<String>) {
let borrow = cell.borrow(no_gc);
cell.set(no_gc, "Hello".to_string());
drop(borrow); // otherwise compiler can shorten borrow's lifetime
}ⓘ
use mozjs::context::NoGC;
use mozjs::cell::JSCell;
fn mut_borrow_after_mut_borrow(no_gc: &mut NoGC, cell: &JSCell<String>) {
let borrow_mut = cell.borrow_mut(no_gc);
cell.set(no_gc, "Hello".to_string());
drop(borrow_mut); // otherwise compiler can shorten borrow_mut's lifetime
}ⓘ
use mozjs::context::NoGC;
use mozjs::cell::JSCell;
fn borrow_after_mut_borrow(no_gc: &mut NoGC, cell: &JSCell<String>) {
let borrow_mut = cell.borrow_mut(no_gc);
let borrow = cell.borrow(no_gc);
drop(borrow_mut); // otherwise compiler can shorten borrow_mut's lifetime
}Fields§
§inner: UnsafeCell<T>Implementations§
Trait Implementations§
Auto Trait Implementations§
impl<T> !Freeze for JSCell<T>
impl<T> !RefUnwindSafe for JSCell<T>
impl<T> Send for JSCell<T>where
T: Send,
impl<T> !Sync for JSCell<T>
impl<T> Unpin for JSCell<T>where
T: Unpin,
impl<T> UnsafeUnpin for JSCell<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for JSCell<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Filterable for T
impl<T> Filterable for T
Source§fn filterable(
self,
filter_name: &'static str,
) -> RequestFilterDataProvider<T, fn(DataRequest<'_>) -> bool>
fn filterable( self, filter_name: &'static str, ) -> RequestFilterDataProvider<T, fn(DataRequest<'_>) -> bool>
Creates a filterable data provider with the given name for debugging. Read more