Struct wasmtime_runtime::VMGcRef
source · pub struct VMGcRef(/* private fields */);
Expand description
A raw, unrooted GC reference.
A VMGcRef
is either:
-
A reference to some kind of object on the GC heap, but we don’t know exactly which kind without further reflection. Furthermore, this is not actually a pointer, but a compact index into a Wasm GC heap.
-
An
i31ref
: it doesn’t actually reference an object in the GC heap, but is instead an inline, unboxed 31-bit integer.
§VMGcRef
and GC Barriers
Depending on the garbage collector in use, cloning, writing, and dropping a
VMGcRef
may require invoking GC barriers (little snippets of code provided
by the collector to ensure it is correctly tracking all GC references).
Therefore, to encourage correct usage of GC barriers, this type does NOT
implement Clone
or Copy
. Use GcStore::clone_gc_ref
,
GcStore::write_gc_ref
, and GcStore::drop_gc_ref
to clone, write, and
drop VMGcRef
s respectively.
As an escape hatch, if you really need to copy a VMGcRef
without invoking
GC barriers and you understand why that will not lead to GC bugs in this
particular case, you can use the unchecked_copy
method.
Implementations§
source§impl VMGcRef
impl VMGcRef
sourcepub const ONLY_EXTERN_REF_AND_I31: bool = true
pub const ONLY_EXTERN_REF_AND_I31: bool = true
The only type of valid VMGcRef
is currently VMExternRef
.
Assert on this anywhere you are making that assumption, so that we know all the places to update when it no longer holds true.
sourcepub const I31_REF_DISCRIMINANT: u32 = 1u32
pub const I31_REF_DISCRIMINANT: u32 = 1u32
If this bit is set on a GC reference, then the GC reference is actually an
unboxed i31
.
Must be kept in sync with wasmtime_cranelift::I31_REF_DISCRIMINANT
.
sourcepub fn from_raw_u32(raw: u32) -> Option<Self>
pub fn from_raw_u32(raw: u32) -> Option<Self>
Create a new VMGcRef
from the given raw u32 value.
Does not discriminate between indices into a GC heap and i31ref
s.
Returns None
for zero values.
The given index should point to a valid GC-managed object within this reference’s associated heap. Failure to uphold this will be memory safe, but will lead to general failures such as panics or incorrect results.
sourcepub fn from_heap_index(index: NonZeroU32) -> Option<Self>
pub fn from_heap_index(index: NonZeroU32) -> Option<Self>
Create a new VMGcRef
from the given index into a GC heap.
The given index should point to a valid GC-managed object within this reference’s associated heap. Failure to uphold this will be memory safe, but will lead to general failures such as panics or incorrect results.
Returns None
when the index is not 2-byte aligned and therefore
conflicts with the i31ref
discriminant.
sourcepub fn from_raw_non_zero_u32(raw: NonZeroU32) -> Self
pub fn from_raw_non_zero_u32(raw: NonZeroU32) -> Self
Create a new VMGcRef
from the given raw value.
Does not discriminate between indices into a GC heap and i31ref
s.
sourcepub fn from_r64(raw: u64) -> Result<Option<Self>>
pub fn from_r64(raw: u64) -> Result<Option<Self>>
Create a new VMGcRef
from a raw r64
value from Cranelift.
Returns an error if raw
cannot be losslessly converted from a u64
into a u32
.
Returns Ok(None)
if raw
is zero (aka a “null” VMGcRef
).
This method only exists because we can’t currently use Cranelift’s r32
type on 64-bit platforms. We should instead have a from_r32
method.
sourcepub fn unchecked_copy(&self) -> Self
pub fn unchecked_copy(&self) -> Self
Copy this VMGcRef
without running the GC’s clone barriers.
Prefer calling clone(&mut GcStore)
instead! This is mostly an internal
escape hatch for collector implementations.
Failure to run GC barriers when they would otherwise be necessary can lead to leaks, panics, and wrong results. It cannot lead to memory unsafety, however.
sourcepub fn as_heap_index(&self) -> Option<NonZeroU32>
pub fn as_heap_index(&self) -> Option<NonZeroU32>
Get this GC reference as a u32 index into its GC heap.
Returns None
for i31ref
s.
sourcepub fn as_raw_u32(&self) -> u32
pub fn as_raw_u32(&self) -> u32
Get this GC refererence as a raw u32 value, regardless whether it is
actually a reference to a GC object or is an i31ref
.
sourcepub fn into_r64(self) -> u64
pub fn into_r64(self) -> u64
Get this GC reference as a raw r64
value for passing to Cranelift.
This method only exists because we can’t currently use Cranelift’s r32
type on 64-bit platforms. We should instead be able to pass VMGcRef
into compiled code directly.
sourcepub fn as_r64(&self) -> u64
pub fn as_r64(&self) -> u64
Get this GC reference as a raw r64
value for passing to Cranelift.
This method only exists because we can’t currently use Cranelift’s r32
type on 64-bit platforms. We should instead be able to pass VMGcRef
into compiled code directly.
sourcepub fn into_typed<T>(self, gc_heap: &impl GcHeap) -> Result<TypedGcRef<T>, Self>where
T: GcHeapObject,
pub fn into_typed<T>(self, gc_heap: &impl GcHeap) -> Result<TypedGcRef<T>, Self>where
T: GcHeapObject,
Creates a typed GC reference from self
, checking that self
actually
is a T
.
If this is not a GC reference to a T
, then Err(self)
is returned.
sourcepub fn into_typed_unchecked<T>(self) -> TypedGcRef<T>where
T: GcHeapObject,
pub fn into_typed_unchecked<T>(self) -> TypedGcRef<T>where
T: GcHeapObject,
Creates a typed GC reference without actually checking that self
is a
T
.
self
should point to a T
object. Failure to uphold this invariant is
memory safe, but will lead to general incorrectness such as panics or
wrong results.
sourcepub fn as_typed<T>(&self, gc_heap: &impl GcHeap) -> Option<&TypedGcRef<T>>where
T: GcHeapObject,
pub fn as_typed<T>(&self, gc_heap: &impl GcHeap) -> Option<&TypedGcRef<T>>where
T: GcHeapObject,
Borrow self
as a typed GC reference, checking that self
actually is
a T
.
sourcepub fn as_typed_unchecked<T>(&self) -> &TypedGcRef<T>where
T: GcHeapObject,
pub fn as_typed_unchecked<T>(&self) -> &TypedGcRef<T>where
T: GcHeapObject,
Creates a typed GC reference without actually checking that self
is a
T
.
self
should point to a T
object. Failure to uphold this invariant is
memory safe, but will lead to general incorrectness such as panics or
wrong results.
sourcepub fn gc_header<'a>(&self, gc_heap: &'a dyn GcHeap) -> Option<&'a VMGcHeader>
pub fn gc_header<'a>(&self, gc_heap: &'a dyn GcHeap) -> Option<&'a VMGcHeader>
Get a reference to the GC header that this GC reference is pointing to.
Returns None
when this is an i31ref
and doesn’t actually point to a
GC header.
sourcepub fn is_i31(&self) -> bool
pub fn is_i31(&self) -> bool
Is this VMGcRef
actually an unboxed 31-bit integer, and not actually a
GC reference?
sourcepub fn unwrap_i31(&self) -> I31
pub fn unwrap_i31(&self) -> I31
Get the underlying i31
, panicking if this is not an i31
.
sourcepub fn is_extern_ref(&self) -> bool
pub fn is_extern_ref(&self) -> bool
Is this VMGcRef
a VMExternRef
?
Trait Implementations§
source§impl<T> From<TypedGcRef<T>> for VMGcRef
impl<T> From<TypedGcRef<T>> for VMGcRef
source§fn from(value: TypedGcRef<T>) -> Self
fn from(value: TypedGcRef<T>) -> Self
source§impl From<VMGcRef> for TableElement
impl From<VMGcRef> for TableElement
source§fn from(x: VMGcRef) -> TableElement
fn from(x: VMGcRef) -> TableElement
source§impl PartialEq for VMGcRef
impl PartialEq for VMGcRef
impl Eq for VMGcRef
impl StructuralPartialEq for VMGcRef
Auto Trait Implementations§
impl Freeze for VMGcRef
impl RefUnwindSafe for VMGcRef
impl Send for VMGcRef
impl Sync for VMGcRef
impl Unpin for VMGcRef
impl UnwindSafe for VMGcRef
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.