pub struct Gc<T>where
T: Trace + 'static,{ /* private fields */ }Expand description
A strong, reference-counted handle to a heap value.
Gc<T> is a pointer-sized handle: cloning is cheap, Deref gives &T,
and the value is released as soon as the last handle is gone (cycles need
Heap::collect).
Implementations§
Source§impl<T> Gc<T>where
T: Trace + 'static,
impl<T> Gc<T>where
T: Trace + 'static,
Sourcepub fn strong_count(&self) -> usize
pub fn strong_count(&self) -> usize
Number of strong handles (including internal edges).
Sourcepub fn get_mut(&mut self) -> Option<&mut T>
pub fn get_mut(&mut self) -> Option<&mut T>
A mutable borrow of the value, when this handle is its only owner.
This is what lets a string grow in place: s += "abc" in a loop is a copy of the
whole string per iteration without it, and an amortised append with it. The
reference does the same thing under the same condition (JS_ConcatStringInPlace
checks ref_count == 1), so the two engines make the same trade.
None whenever anyone else could be looking:
strong > 1— anotherGchandle, or an edge from a live object.weak > 0— aWeakGccan callWeakGc::upgradethrough a shared borrow, so a strong count of one is not enough to rule out a handle appearing while the returned borrow is alive. This is the conditionRc::get_mutgets for free and a split strong/weak count does not.
The caller must also not run the collector — or allocate, which can trigger one —
while the borrow is alive: marking calls Trace::trace through a shared borrow.
Every use in the engine is a Vec append that allocates no box, which is why the
rule is stated rather than enforced.
Sourcepub fn upgrades_to(&self, other: &WeakGc<T>) -> bool
pub fn upgrades_to(&self, other: &WeakGc<T>) -> bool
Whether this handle points at the box other refers to, expired or not.
A WeakMap stores weak handles but compares against live keys, so the comparison
has to span both handle types without upgrading (which would resurrect the key).
Trait Implementations§
Source§impl<T> Trace for Gc<T>where
T: Trace + 'static,
impl<T> Trace for Gc<T>where
T: Trace + 'static,
Source§fn external_bytes(&self) -> usize
fn external_bytes(&self) -> usize
What the pointee owns, which is counted against the pointee’s own box.
Forwarded so that gc.external_bytes() is not a silent zero, but a container must
never add this to its own total: it would count the child’s payload twice, once here
and once when the child’s box is registered.