Struct zerogc::Gc [−][src]
#[repr(transparent)]pub struct Gc<'gc, T: GcSafe + ?Sized + 'gc, Id: CollectorId> { /* fields omitted */ }
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.
Implementations
Create a GC pointer from a raw ID/pointer pair
Safety
Undefined behavior if the underlying pointer is not valid and associated with the collector corresponding to the id.
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.
pub fn create_handle<'a>(
&self
) -> <Id::System as GcHandleSystem<'gc, 'a, T>>::Handle where
Id::System: GcHandleSystem<'gc, 'a, T>,
T: GcErase<'a, Id> + 'a,
<T as GcErase<'a, Id>>::Erased: GcSafe + 'a,
pub fn create_handle<'a>(
&self
) -> <Id::System as GcHandleSystem<'gc, 'a, T>>::Handle where
Id::System: GcHandleSystem<'gc, 'a, T>,
T: GcErase<'a, Id> + 'a,
<T as GcErase<'a, Id>>::Erased: GcSafe + 'a,
Create a handle to this object, which can be used without a context
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’.
Get a reference to the collector’s id
The underlying collector it points to is not necessarily always valid
Trait Implementations
impl<'gc, O, V, Id> GcDirectBarrier<'gc, Gc<'gc, O, Id>> for Gc<'gc, V, Id> where
O: GcSafe + 'gc,
V: GcSafe + 'gc,
Id: CollectorId,
impl<'gc, O, V, Id> GcDirectBarrier<'gc, Gc<'gc, O, Id>> for Gc<'gc, V, Id> where
O: GcSafe + 'gc,
V: GcSafe + 'gc,
Id: CollectorId,
Trigger a write barrier, before writing to one of the owning object’s managed fields Read more
Rebrand
impl<'gc, T: ?Sized, U: ?Sized, Id> CoerceUnsized<Gc<'gc, U, Id>> for Gc<'gc, T, Id> where
T: ?Sized + GcSafe + Unsize<U>,
U: ?Sized + GcSafe,
Id: CollectorId,
Double-indirection is completely safe
impl<'gc, T: ?Sized, Id> Send for Gc<'gc, T, Id> where
T: GcSafe + ?Sized + Sync,
Id: CollectorId + Sync,
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
impl<'gc, T: ?Sized, Id> Sync for Gc<'gc, T, Id> where
T: GcSafe + ?Sized + Sync,
Id: CollectorId + 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 implenentation should be as well.
Auto Trait Implementations
impl<'gc, T: ?Sized, Id> RefUnwindSafe for Gc<'gc, T, Id> where
Id: RefUnwindSafe,
T: RefUnwindSafe,
impl<'gc, T: ?Sized, Id> UnwindSafe for Gc<'gc, T, Id> where
Id: UnwindSafe,
T: RefUnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more