pub struct RawValueRootGuard { /* private fields */ }Expand description
RAII guard for one or more heap JSVal slots registered with the GC
extra-roots table (AddRawValueRoot) — the general form of
PersistentGlobal’s rooting, for values that must survive across an
async window (pending fetch promises, fs/crypto callbacks, …).
§Why the slots live in a Box<[JSVal]>
AddRawValueRoot registers the slot’s address: the GC scans and
updates that memory in place for as long as the root is registered, and
RemoveRawValueRoot removes by pointer — the registered address must
therefore stay both valid and identical until removal. Rooting a stack
local whose frame then returns leaves the GC tracing dead stack memory,
and removing with a different address is a silent no-op (the root table
is keyed by pointer), leaking the dangling root. The Box pins the
slots at a stable heap address for the guard’s whole life; moving the
guard only moves the Box pointer, never the rooted memory.
§Drop semantics (liveness-guarded, leak-on-foreign)
On drop, when the current thread’s Runtime::get() still resolves to
the captured context, every slot is unrooted and the values freed.
Otherwise (dropped from a foreign thread while the context is alive, or
after the runtime went away) the rooted slots are leaked, never freed —
freeing memory the root table may still point at would leave the GC a
dangling scan address, which is strictly worse than a bounded leak.
Implementations§
Source§impl RawValueRootGuard
impl RawValueRootGuard
Sourcepub unsafe fn new(
cx: *mut JSContext,
vals: &[Value],
name: &'static CStr,
) -> Option<RawValueRootGuard>
pub unsafe fn new( cx: *mut JSContext, vals: &[Value], name: &'static CStr, ) -> Option<RawValueRootGuard>
Root vals for an async window that spans ticks/frames.
Returns None if any registration failed (OOM): slots registered
before the failure are unrooted before returning, so a None carries
no leaked roots and the caller keeps its own rooting (the
pre-existing degraded path at the call sites).
§Safety
cxmust be a liveJSContexton the current thread.- Each value must be a valid
JSVal. After this call the guard’s slots are the live copies — read them viaSelf::get, not via pre-existing snapshots.
Sourcepub fn get(&self, i: usize) -> Value
pub fn get(&self, i: usize) -> Value
The live (GC-updated) value at i. After any window in which a GC
may have run, this — not a snapshot taken at spawn time — is the
value to use (a moving GC updates the guard’s slot in place).
Sourcepub fn into_inner(self) -> Option<Box<[Value]>>
pub fn into_inner(self) -> Option<Box<[Value]>>
Release the roots and take ownership of the values (explicit ownership transfer before the guard’s natural drop site).
Returns None when the roots could not be released (foreign thread
or dead runtime): the rooted memory is then leaked by the guard —
the caller must NOT receive memory the GC root table still points
at.
Trait Implementations§
Source§impl Drop for RawValueRootGuard
impl Drop for RawValueRootGuard
Auto Trait Implementations§
impl !Send for RawValueRootGuard
impl !Sync for RawValueRootGuard
impl Freeze for RawValueRootGuard
impl RefUnwindSafe for RawValueRootGuard
impl Unpin for RawValueRootGuard
impl UnsafeUnpin for RawValueRootGuard
impl UnwindSafe for RawValueRootGuard
Blanket Implementations§
Source§impl<T> AsCtxPtr for Twhere
T: ?Sized,
impl<T> AsCtxPtr for Twhere
T: ?Sized,
Source§fn as_ctx_ptr(&self) -> *mut Selfwhere
Self: Sized,
fn as_ctx_ptr(&self) -> *mut Selfwhere
Self: Sized,
self’s address as *mut Self for deferred-task / scopeguard /
ref_guard ctx slots. The closures/trampolines deref it as shared
(&*p) — every method they reach is &self post-R-2, so no write
provenance is required; the *mut spelling is purely to match the
existing DerefOnDrop / HasAutoFlush / RefCount ABI.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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Filterable for T
impl<T> Filterable for T
Source§fn filterable(
self,
filter_name: &'static str,
) -> RequestFilterDataProvider<T, fn(DataRequest<'_>) -> bool>
fn filterable( self, filter_name: &'static str, ) -> RequestFilterDataProvider<T, fn(DataRequest<'_>) -> bool>
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