pub struct Gc<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: Trace + 'static> Gc<T>
impl<T: Trace + 'static> Gc<T>
Sourcepub fn new(heap: &Heap, value: T) -> Gc<T>
pub fn new(heap: &Heap, value: T) -> Gc<T>
Allocate value on heap and return a handle to it.
Examples found in repository?
19fn main() {
20 let heap = Heap::new();
21 let a = Gc::new(&heap, Node::default());
22 let b = Gc::new(&heap, Node::default());
23 *a.next.borrow_mut() = Some(b.clone());
24 *b.next.borrow_mut() = Some(a.clone()); // a cycle
25 drop((a, b));
26
27 let stats = heap.collect();
28 println!("collected={} live={}", stats.collected, stats.after);
29 assert_eq!(stats.collected, 2, "the cycle should be collected");
30}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 + 'static> Trace for Gc<T>
impl<T: Trace + 'static> Trace for Gc<T>
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.