Skip to main content

Gc

Struct Gc 

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

Source

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}
Source

pub fn heap(&self) -> &Heap

The heap this value lives on.

Source

pub fn strong_count(&self) -> usize

Number of strong handles (including internal edges).

Source

pub fn ptr_eq(&self, other: &Gc<T>) -> bool

Whether two handles point at the same box.

Source

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

Source

pub fn downgrade(&self) -> WeakGc<T>

Create a non-owning handle.

Trait Implementations§

Source§

impl<T: Trace + 'static> Clone for Gc<T>

Source§

fn clone(&self) -> Gc<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Trace + 'static> Debug for Gc<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Trace + 'static> Deref for Gc<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T: Trace + 'static> Drop for Gc<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T: Trace + 'static> Trace for Gc<T>

Source§

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.

Source§

fn trace(&self, tracer: &mut Tracer<'_>)

Report every Gc owned by self to tracer.

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>
where *const GcBox<T> is !null: Freeze,

§

impl<T> Move for Gc<T>

§

impl<T> Unpin for Gc<T>
where *const GcBox<T> is !null: Unpin,

§

impl<T> UnsafeUnpin for Gc<T>
where *const GcBox<T> is !null: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.