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