Skip to main content

JSCell

Struct JSCell 

Source
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§

Source§

impl<T> JSCell<T>

Source

pub fn new(val: T) -> Self

Source

pub fn set<'a, 'cx>(&'a self, _exclusive: &'cx mut NoGC, val: T)

Sets the wrapped value.

By passing a &mut NoGC we statically ensure no other borrows are alive at the same time.

For more information see JSCell documentation.

Source

pub fn borrow_mut<'a: 'r, 'cx: 'r, 'r>( &'a self, _exclusive: &'cx mut NoGC, ) -> &'r mut T

Mutably borrows the wrapped value.

By passing a &mut NoGC we statically ensure no other borrows are alive at the same time.

For more information see JSCell documentation.

Source

pub fn borrow<'a: 'r, 'no_cx: 'r, 'r>(&'a self, _no_gc: &'no_cx NoGC) -> &'r T

Immutably borrows the wrapped value.

By passing a &NoGC we statically ensure no mutable borrows are alive at the same time.

For more information see JSCell documentation.

Trait Implementations§

Source§

impl<T: Debug> Debug for JSCell<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Traceable> Traceable for JSCell<T>

Source§

unsafe fn trace(&self, trc: *mut JSTracer)

Trace self.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Filterable for T

Source§

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
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T