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?
examples/cycle.rs (line 21)
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§
Auto Trait Implementations§
impl<T> !RefUnwindSafe for Gc<T>
impl<T> !Send for Gc<T>
impl<T> !Sync for Gc<T>
impl<T> !UnwindSafe for Gc<T>
impl<T> Forget for Gc<T>
impl<T> Freeze for Gc<T>
impl<T> Move for Gc<T>
impl<T> Unpin for Gc<T>
impl<T> UnsafeUnpin for Gc<T>where
NonNull<GcBox<T>>: UnsafeUnpin,
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
Mutably borrows from an owned value. Read more