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,
impl<C> RootScope<C>where
C: AsContextMut,
pub fn new(store: C) -> RootScope<C>
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)
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,
impl<T> AsContext for RootScope<T>where
T: AsContextMut,
§impl<T> AsContextMut for RootScope<T>where
T: AsContextMut,
impl<T> AsContextMut for RootScope<T>where
T: AsContextMut,
§fn as_context_mut(
&mut self,
) -> StoreContextMut<'_, <RootScope<T> as AsContext>::Data>
fn as_context_mut( &mut self, ) -> StoreContextMut<'_, <RootScope<T> as AsContext>::Data>
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> 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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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