wiggle::wasmtime_crate

Struct RootScope

pub struct RootScope<C>
where C: AsContextMut,
{ /* private fields */ }
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§

§

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

pub fn new(store: C) -> RootScope<C>

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...
}

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§

§

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

fn as_context(&self) -> StoreContext<'_, <RootScope<T> as AsContext>::Data>

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

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

§

fn as_context_mut( &mut self, ) -> StoreContextMut<'_, <RootScope<T> as AsContext>::Data>

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

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

§

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

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> Pointee for T

Source§

type Pointer = u32

Source§

fn debug( pointer: <T as Pointee>::Pointer, f: &mut Formatter<'_>, ) -> Result<(), Error>

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more