pub struct Gc<'gc, T: ?Sized + 'gc> { /* private fields */ }Expand description
A garbage collected pointer to a type T. Implements Copy, and is implemented as a plain machine
pointer. You can only allocate Gc pointers through a &Mutation<'gc> inside an arena type,
and through “generativity” such Gc pointers may not escape the arena they were born in or
be stored inside TLS. This, combined with correct Collect implementations, means that Gc
pointers will never be dangling and are always safe to access.
Implementations§
Source§impl<'gc, T: 'static> Gc<'gc, T>
impl<'gc, T: 'static> Gc<'gc, T>
Sourcepub fn new_static(mc: &Mutation<'gc>, t: T) -> Gc<'gc, T>
pub fn new_static(mc: &Mutation<'gc>, t: T) -> Gc<'gc, T>
Create a new Gc pointer from a static value.
This method does not require that the type T implement Collect. This uses Static
internally to automatically provide a trivial Collect impl and is equivalent to the
following code:
struct MyStaticStruct;
let p = Gc::new(mc, Static(MyStaticStruct));
// This is allowed because `Static` is `#[repr(transparent)]`
let p: Gc<MyStaticStruct> = unsafe { Gc::cast(p) };Source§impl<'gc, T: ?Sized + 'gc> Gc<'gc, T>
impl<'gc, T: ?Sized + 'gc> Gc<'gc, T>
Sourcepub unsafe fn cast<U: 'gc>(this: Gc<'gc, T>) -> Gc<'gc, U>
pub unsafe fn cast<U: 'gc>(this: Gc<'gc, T>) -> Gc<'gc, U>
Cast a Gc pointer to a different type.
SAFETY:
It must be valid to dereference a *mut U that has come from casting a *mut T.
Source§impl<'gc, T: ?Sized + 'gc> Gc<'gc, T>
impl<'gc, T: ?Sized + 'gc> Gc<'gc, T>
Sourcepub fn as_ref(self: Gc<'gc, T>) -> &'gc T
pub fn as_ref(self: Gc<'gc, T>) -> &'gc T
Obtains a long-lived reference to the contents of this Gc.
Unlike AsRef or Deref, the returned reference isn’t bound to the Gc itself, and
will stay valid for the entirety of the current arena callback.
pub fn downgrade(this: Gc<'gc, T>) -> GcWeak<'gc, T>
Sourcepub fn write(mc: &Mutation<'gc>, gc: Self) -> &'gc Write<T>
pub fn write(mc: &Mutation<'gc>, gc: Self) -> &'gc Write<T>
Triggers a write barrier on this Gc, allowing for safe mutation.
This triggers an unrestricted backwards write barrier on this pointer, meaning that it is guaranteed that this pointer can safely adopt any arbitrary child pointers (until the next time that collection is triggered).
It returns a reference to the inner T wrapped in a Write marker to allow for
unrestricted mutation on the held type or any of its directly held fields.
Sourcepub fn ptr_eq(this: Gc<'gc, T>, other: Gc<'gc, T>) -> bool
pub fn ptr_eq(this: Gc<'gc, T>, other: Gc<'gc, T>) -> bool
Returns true if two Gcs point to the same allocation.
Similarly to Rc::ptr_eq and Arc::ptr_eq, this function ignores the metadata of dyn
pointers.
pub fn as_ptr(gc: Gc<'gc, T>) -> *const T
Sourcepub fn is_dead(_: &Finalization<'gc>, gc: Gc<'gc, T>) -> bool
pub fn is_dead(_: &Finalization<'gc>, gc: Gc<'gc, T>) -> bool
Returns true when a pointer is dead during finalization. This is equivalent to
GcWeak::is_dead for strong pointers.
Any strong pointer reachable from the root will never be dead, BUT there can be strong pointers reachable only through other weak pointers that can be dead.
Sourcepub fn resurrect(fc: &Finalization<'gc>, gc: Gc<'gc, T>)
pub fn resurrect(fc: &Finalization<'gc>, gc: Gc<'gc, T>)
Manually marks a dead Gc pointer as reachable and keeps it alive.
Equivalent to GcWeak::resurrect for strong pointers. Manually marks this pointer and
all transitively held pointers as reachable, thus keeping them from being dropped this
collection cycle.
Source§impl<'gc, T: ?Sized + 'gc> Gc<'gc, RefLock<T>>
impl<'gc, T: ?Sized + 'gc> Gc<'gc, RefLock<T>>
pub fn borrow(self) -> Ref<'gc, T>
pub fn try_borrow(self) -> Result<Ref<'gc, T>, BorrowError>
pub fn borrow_mut(self, mc: &Mutation<'gc>) -> RefMut<'gc, T>
pub fn try_borrow_mut( self, mc: &Mutation<'gc>, ) -> Result<RefMut<'gc, T>, BorrowMutError>
Trait Implementations§
Source§impl<'gc, T: ?Sized + 'gc> Collect for Gc<'gc, T>
impl<'gc, T: ?Sized + 'gc> Collect for Gc<'gc, T>
Source§fn trace(&self, cc: &Collection)
fn trace(&self, cc: &Collection)
Collect::trace on all held Gc pointers. If this type holds inner types that
implement Collect, a valid implementation would simply call Collect::trace on all the
held values to ensure this.Source§fn needs_trace() -> boolwhere
Self: Sized,
fn needs_trace() -> boolwhere
Self: Sized,
Gc pointer and trace is unnecessary
to call, you may implement this method and return false. The default implementation returns
true, signaling that Collect::trace must be called.