Struct wasmtime::RootScope

source ·
pub struct RootScope<C>
where C: AsContextMut,
{ /* private fields */ }
Available on crate feature runtime only.
Expand description

Nested rooting scopes.

RootScope allows the creation or nested rooting scopes for use with Rooted<T>. This allows for fine-grained control over how long a set of Rooted<T>s are strongly held alive, giving gives you the tools necessary to avoid holding onto GC objects longer than necessary. Rooted<T>s created within a RootScope are automatically unrooted when the RootScope is dropped. For more details on Rooted<T> lifetimes and their interaction with rooting scopes, see Rooted<T>’s documentation.

A RootScope<C> wraps a C: AsContextMut (that is, anything that represents exclusive access to a Store) and in turn implements AsContext and AsContextMut in terms of its underlying C. Therefore, RootScope<C> can be used anywhere you would use the underlying C, for example in the Global::get method. Any Rooted<T>s created by a method that a RootScope<C> was passed as context to are tied to the RootScope<C>’s scope and automatically unrooted when the scope is dropped.

§Example

let mut store = Store::<()>::default();

let a: Rooted<_>;
let b: Rooted<_>;
let c: Rooted<_>;

// Root `a` in the store's scope. It will be rooted for the duration of the
// store's lifetime.
a = ExternRef::new(&mut store, 42)?;

// `a` is rooted, so we can access its data successfully.
assert!(a.data(&store).is_ok());

{
    let mut scope1 = RootScope::new(&mut store);

    // Root `b` in `scope1`.
    b = ExternRef::new(&mut scope1, 36)?;

    // Both `a` and `b` are rooted.
    assert!(a.data(&scope1).is_ok());
    assert!(b.data(&scope1).is_ok());

    {
        let mut scope2 = RootScope::new(&mut scope1);

        // Root `c` in `scope2`.
        c = ExternRef::new(&mut scope2, 36)?;

        // All of `a`, `b`, and `c` are rooted.
        assert!(a.data(&scope2).is_ok());
        assert!(b.data(&scope2).is_ok());
        assert!(c.data(&scope2).is_ok());

        // Drop `scope2`.
    }

    // Now `a` and `b` are still rooted, but `c` was unrooted when we dropped
    // `scope2`.
    assert!(a.data(&scope1).is_ok());
    assert!(b.data(&scope1).is_ok());
    assert!(c.data(&scope1).is_err());

    // Drop `scope1`.
}

// And now only `a` is still rooted. Both `b` and `c` were unrooted when we
// dropped their respective rooting scopes.
assert!(a.data(&store).is_ok());
assert!(b.data(&store).is_err());
assert!(c.data(&store).is_err());

Implementations§

source§

impl<C> RootScope<C>
where C: AsContextMut,

source

pub fn new(store: C) -> Self

Construct a new scope for rooting GC objects.

§Example
let mut store = Store::<()>::default();

{
    let mut scope = RootScope::new(&mut store);

    // Temporarily root GC objects in this nested rooting scope...
}
source

pub fn reserve(&mut self, additional: usize)

Reserve enough capacity for additional GC roots in this scope.

§Example
let mut store = Store::<()>::default();

{
    let mut scope = RootScope::new(&mut store);

    // Ensure we have enough storage pre-allocated to root five GC
    // references inside this scope without any underlying reallocation.
    scope.reserve(5);

    // ...
}

Trait Implementations§

source§

impl<T> AsContext for RootScope<T>
where T: AsContextMut,

§

type Data = <T as AsContext>::Data

The host information associated with the Store, aka the T in Store<T>.
source§

fn as_context(&self) -> StoreContext<'_, Self::Data>

Returns the store context that this type provides access to.
source§

impl<T> AsContextMut for RootScope<T>
where T: AsContextMut,

source§

fn as_context_mut(&mut self) -> StoreContextMut<'_, Self::Data>

Returns the store context that this type provides access to.
source§

impl<C> Drop for RootScope<C>
where C: AsContextMut,

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<C> Freeze for RootScope<C>
where C: Freeze,

§

impl<C> RefUnwindSafe for RootScope<C>
where C: RefUnwindSafe,

§

impl<C> Send for RootScope<C>
where C: Send,

§

impl<C> Sync for RootScope<C>
where C: Sync,

§

impl<C> Unpin for RootScope<C>
where C: Unpin,

§

impl<C> UnwindSafe for RootScope<C>
where C: 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> 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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.