Skip to main content

JSRefCell

Struct JSRefCell 

Source
pub struct JSRefCell<T> {
    value: RefCell<T>,
}
Expand description

std::cell::RefCell that statically prevents borrow_mut panics from occurring by borrowing due to GC using the NoGC marker.

If dynamic borrow checking is not needed, one should use JSCell instead. If inner type is Copy one should use std::cell::Cell instead.

Fields§

§value: RefCell<T>

Implementations§

Source§

impl<T> JSRefCell<T>

Source

pub fn new(value: T) -> JSRefCell<T>

Create a new JSRefCell containing value.

Source

pub fn as_refcell(&self) -> &RefCell<T>

Returns a reference to the underlying RefCell.

This is not strictly unsafe, but it is not recommended to use this method unless you know what you are doing.

Mutable borrows from it can cause panics if a GC happens while the borrow is active, which is why JSRefCell::borrow_mut should be used instead.

Source

pub fn borrow(&self) -> Ref<'_, T>

Immutably borrows the wrapped value.

The borrow lasts until the returned Ref exits scope. Multiple immutable borrows can be taken out at the same time.

This is exactly the same as std::cell::RefCell::borrow.

§Panics

Panics if the value is currently mutably borrowed.

Source

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

Mutably borrows the wrapped value.

The borrow lasts until the returned RefMut exits scope. The value cannot be borrowed while this borrow is active.

This is similar to std::cell::RefCell::borrow_mut, but it requires a &NoGC token to ensure that no GC can happen while the borrow is active, which would otherwise cause a panic when tracing (which calls borrow).

§Examples

In simple cases one can use &NoGC to statically ensure no GC can happen in the whole function:

use mozjs::context::{JSContext, NoGC};
use mozjs::cell::JSRefCell;
fn f(no_gc: &NoGC, cell: &JSRefCell<String>) {
    let mut mutably_borrowed = cell.borrow_mut(no_gc);
}

But in more complex cases, a method might trigger a GC, and thus require a &mut JSContext. In that case &JSContext can be used in place of &NoGC, which will make RefMut bounded to the lifetime of the &JSContext and thus prevent any GC from happening while it is alive.

use mozjs::context::{JSContext, NoGC};
use mozjs::cell::JSRefCell;
fn GC(cx: &mut JSContext) {}

fn f(cx: &mut JSContext, cell: &JSRefCell<String>) {
    {
        let mut mutably_borrowed = cell.borrow_mut(cx);
        // do something with mutably_borrowed

        // only &JSContext is available here
    } // mutably_borrowed goes out of scope here
    // so one can now use &mut JSContext
    GC(cx);
}
§Non-Example
use mozjs::context::{JSContext, NoGC};
use mozjs::cell::JSRefCell;
fn GC(cx: &mut JSContext) {}

fn f(cx: &mut JSContext, cell: &JSRefCell<String>) {
    {
        let mut mutably_borrowed = cell.borrow_mut(cx);
        // do something with mutably_borrowed

        // here one cannot use anything that might trigger a GC
        // as that would require &mut JSContext
        // but there is already existing &JSContext bounded at RefMut
        GC(cx);
    } // mutably_borrowed goes out of scope here
}
§Panics

Panics if the value is currently borrowed.

Source§

impl<T: Default> JSRefCell<T>

Source

pub fn take(&self) -> T

Takes the wrapped value, leaving Default::default() in its place.

§Panics

Panics if the value is currently borrowed.

Trait Implementations§

Source§

impl<T: Clone> Clone for JSRefCell<T>

Source§

fn clone(&self) -> JSRefCell<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for JSRefCell<T>

Source§

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

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

impl<T: Default> Default for JSRefCell<T>

Source§

fn default() -> JSRefCell<T>

Returns the “default value” for a type. Read more
Source§

impl<T: Traceable> Traceable for JSRefCell<T>

Source§

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

Trace self.

Auto Trait Implementations§

§

impl<T> !Freeze for JSRefCell<T>

§

impl<T> !RefUnwindSafe for JSRefCell<T>

§

impl<T> Send for JSRefCell<T>
where T: Send,

§

impl<T> !Sync for JSRefCell<T>

§

impl<T> Unpin for JSRefCell<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for JSRefCell<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for JSRefCell<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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