Struct gc_arena::Gc

source ·
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: Collect + 'gc> Gc<'gc, T>

source

pub fn new(mc: &Mutation<'gc>, t: T) -> Gc<'gc, T>

source§

impl<'gc, T: 'gc> Gc<'gc, T>

source

pub unsafe fn cast<U: 'gc>(this: Gc<'gc, T>) -> Gc<'gc, U>

Cast the internal pointer to a different type.

SAFETY: It must be valid to dereference a *mut U that has come from casting a *mut T.

source

pub unsafe fn from_ptr(ptr: *const T) -> Gc<'gc, T>

Retrieve a Gc from a raw pointer obtained from Gc::as_ptr

SAFETY: The provided pointer must have been obtained from Gc::as_ptr, and the pointer must not have been collected yet.

source§

impl<'gc, T: Unlock + ?Sized + 'gc> Gc<'gc, T>

source

pub fn unlock(self, mc: &Mutation<'gc>) -> &'gc T::Unlocked

Shorthand for Gc::write(mc, self).unlock().

source§

impl<'gc, T: ?Sized + 'gc> Gc<'gc, T>

source

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.

source

pub fn downgrade(this: Gc<'gc, T>) -> GcWeak<'gc, T>

source

pub fn write(mc: &Mutation<'gc>, gc: Self) -> &'gc Write<T>

Triggers a write barrier on this Gc, allowing for further safe mutation.

source

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.

source

pub fn as_ptr(gc: Gc<'gc, T>) -> *const T

source

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.

source

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: Copy + 'gc> Gc<'gc, Lock<T>>

source

pub fn get(self) -> T

source

pub fn set(self, mc: &Mutation<'gc>, t: T)

source§

impl<'gc, T: ?Sized + 'gc> Gc<'gc, RefLock<T>>

source

pub fn borrow(self) -> Ref<'gc, T>

source

pub fn try_borrow(self) -> Result<Ref<'gc, T>, BorrowError>

source

pub fn borrow_mut(self, mc: &Mutation<'gc>) -> RefMut<'gc, T>

source

pub fn try_borrow_mut( self, mc: &Mutation<'gc> ) -> Result<RefMut<'gc, T>, BorrowMutError>

Trait Implementations§

source§

impl<'gc, T: ?Sized + 'gc> AsRef<T> for Gc<'gc, T>

source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<'gc, T: ?Sized + 'gc> Clone for Gc<'gc, T>

source§

fn clone(&self) -> Gc<'gc, T>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<'gc, T: ?Sized + 'gc> Collect for Gc<'gc, T>

source§

fn trace(&self, cc: &Collection)

Must call 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() -> bool
where Self: Sized,

As an optimization, if this type can never hold a 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.
source§

impl<'gc, T: Debug + ?Sized + 'gc> Debug for Gc<'gc, T>

source§

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

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

impl<'gc, T: ?Sized + 'gc> Deref for Gc<'gc, T>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &T

Dereferences the value.
source§

impl<'gc, T: Display + ?Sized + 'gc> Display for Gc<'gc, T>

source§

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

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

impl<'gc, T: Hash + ?Sized + 'gc> Hash for Gc<'gc, T>

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl<'gc, T: Ord + ?Sized + 'gc> Ord for Gc<'gc, T>

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<'gc, T: PartialEq + ?Sized + 'gc> PartialEq for Gc<'gc, T>

source§

fn eq(&self, other: &Self) -> bool

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

fn ne(&self, other: &Self) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'gc, T: PartialOrd + ?Sized + 'gc> PartialOrd for Gc<'gc, T>

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

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

fn le(&self, other: &Self) -> bool

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

fn lt(&self, other: &Self) -> bool

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

fn ge(&self, other: &Self) -> bool

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

fn gt(&self, other: &Self) -> bool

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

impl<'gc, T: ?Sized + 'gc> Pointer for Gc<'gc, T>

source§

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

Formats the value using the given formatter.
source§

impl<'gc, T: ?Sized + 'gc> Copy for Gc<'gc, T>

source§

impl<'gc, T: Eq + ?Sized + 'gc> Eq for Gc<'gc, T>

Auto Trait Implementations§

§

impl<'gc, T> Freeze for Gc<'gc, T>
where T: ?Sized,

§

impl<'gc, T> !RefUnwindSafe for Gc<'gc, T>

§

impl<'gc, T> !Send for Gc<'gc, T>

§

impl<'gc, T> !Sync for Gc<'gc, T>

§

impl<'gc, T> Unpin for Gc<'gc, T>
where T: ?Sized,

§

impl<'gc, T> !UnwindSafe for Gc<'gc, T>

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> 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<T> ToOwned for T
where T: Clone,

§

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.