Struct zerogc::Gc

source · []
#[repr(transparent)]
pub struct Gc<'gc, T: ?Sized, Id: CollectorId> { /* private fields */ }
Expand description

A garbage collected pointer to a value.

This is the equivalent of a garbage collected smart-pointer. It’s so smart, you can even coerce it to a reference bound to the lifetime of the GarbageCollectorRef. However, all those references are invalidated by the borrow checker as soon as your reference to the collector reaches a safepoint. The objects can only survive garbage collection if they live in this smart-pointer.

The smart pointer is simply a guarantee to the garbage collector that this points to a garbage collected object with the correct header, and not some arbitrary bits that you’ve decided to heap allocate.

Safety

A Gc can be safely transmuted back and forth from its corresponding pointer.

Unsafe code can rely on a pointer always dereferencing to the same value in between safepoints. This is true even for copying/moving collectors.

Lifetime

The borrow does not refer to the value &'gc T. Instead, it refers to the context &'gc Id::Context

This is necessary because T may have borrowed interior data with a shorter lifetime 'a < 'gc, making &'gc T invalid (because that would imply ’gc: ’a, which is false).

This ownership can be thought of in terms of the following (simpler) system.

struct GcContext {
    values: Vec<Box<dyn GcSafe>>
}
struct Gc<'gc, T: GcSafe> {
    index: usize,
    marker: PhantomData<T>,
    ctx: &'gc GcContext
}

In this system, safepoints can be thought of mutations that remove dead values from the Vec.

This ownership equivalency is also the justification for why the 'gc lifetime can be covariant

The only difference is that the real Gc structure uses pointers instead of indices.

Implementations

Create a GC pointer from a raw pointer

Safety

Undefined behavior if the underlying pointer is not valid and doesn’t correspond to the appropriate id.

Create a GcHandle referencing this object, allowing it to be used without a context and referenced across safepoints.

Requires that the collector supports handles

Get a reference to the system

Safety

This is based on the assumption that a GcSystem must outlive all of the pointers it owns. Although it could be restricted to the lifetime of the CollectorId (in theory that may have an internal pointer) it will still live for ‘&self’.

The value of the underlying pointer

Cast this reference to a raw pointer

Safety

It’s undefined behavior to mutate the value. The pointer is only valid as long as the reference is.

Get a reference to the collector’s id

The underlying collector it points to is not necessarily always valid

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

The resulting type after dereferencing.

Dereferences the value.

Formats the value using the given formatter. Read more

Trigger a write barrier, before writing to one of the owning object’s managed fields Read more

Rebrand

This type with all garbage collected lifetimes changed to 'new_gc Read more

Trace this object behind a Gc pointer. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Whether this type needs to be traced by the garbage collector. Read more

If this type needs a destructor run. Read more

Trace each field in this type. Read more

In order to send references between threads, the underlying type must be sync.

This is the same reason that Arc<T>: Send requires T: Sync

If the underlying type is Sync, it’s safe to share garbage collected references between threads.

The safety of the collector itself depends on whether CollectorId is Sync. If it is, the whole garbage collection implementation should be as well.

Double-indirection is completely safe

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.