pub struct StructRef { /* private fields */ }Expand description
A reference to a GC-managed struct instance.
WebAssembly structs are static, fixed-length, ordered sequences of
fields. Fields are named by index, not by identifier; in this way, they are
similar to Rust’s tuples. Each field is mutable or constant and stores
unpacked Vals or packed 8-/16-bit integers.
Like all WebAssembly references, these are opaque and unforgeable to Wasm:
they cannot be faked and Wasm cannot, for example, cast the integer
0x12345678 into a reference, pretend it is a valid structref, and trick
the host into dereferencing it and segfaulting or worse.
Note that you can also use Rooted<StructRef> and
OwnedRooted<StructRef> as a type parameter with
Func::typed- and
Func::wrap-style APIs.
§Example
use wasmtime::*;
let mut config = Config::new();
config.wasm_function_references(true);
config.wasm_gc(true);
let engine = Engine::new(&config)?;
let mut store = Store::new(&engine, ());
// Define a struct type.
let struct_ty = StructType::new(
store.engine(),
[FieldType::new(Mutability::Var, StorageType::I8)],
)?;
// Create an allocator for the struct type.
let allocator = StructRefPre::new(&mut store, struct_ty);
{
let mut scope = RootScope::new(&mut store);
// Allocate an instance of the struct type.
let my_struct = StructRef::new(&mut scope, &allocator, &[Val::I32(42)])?;
// That instance's field should have the expected value.
let val = my_struct.field(&mut scope, 0)?.unwrap_i32();
assert_eq!(val, 42);
// And we can update the field's value because it is a mutable field.
my_struct.set_field(&mut scope, 0, Val::I32(36))?;
let new_val = my_struct.field(&mut scope, 0)?.unwrap_i32();
assert_eq!(new_val, 36);
}Implementations§
Source§impl StructRef
impl StructRef
Sourcepub fn new(
store: impl AsContextMut,
allocator: &StructRefPre,
fields: &[Val],
) -> Result<Rooted<StructRef>>
Available on crate features runtime and gc only.
pub fn new( store: impl AsContextMut, allocator: &StructRefPre, fields: &[Val], ) -> Result<Rooted<StructRef>>
runtime and gc only.Synchronously allocate a new struct and get a reference to it.
§Automatic Garbage Collection
If the GC heap is at capacity, and there isn’t room for allocating this new struct, then this method will automatically trigger a synchronous collection in an attempt to free up space in the GC heap.
§Errors
If the given fields values’ types do not match the field types of the
allocator’s struct type, an error is returned.
If the allocation cannot be satisfied because the GC heap is currently
out of memory, then a GcHeapOutOfMemory<()>
error is returned. The allocation might succeed on a second attempt if
you drop some rooted GC references and try again.
§Panics
Panics if your engine is configured for async; use
StructRef::new_async to perform
synchronous allocation instead.
Panics if the allocator, or any of the field values, is not associated with the given store.
Sourcepub async fn new_async(
store: impl AsContextMut,
allocator: &StructRefPre,
fields: &[Val],
) -> Result<Rooted<StructRef>>
Available on crate features runtime and gc and async only.
pub async fn new_async( store: impl AsContextMut, allocator: &StructRefPre, fields: &[Val], ) -> Result<Rooted<StructRef>>
runtime and gc and async only.Asynchronously allocate a new struct and get a reference to it.
§Automatic Garbage Collection
If the GC heap is at capacity, and there isn’t room for allocating this new struct, then this method will automatically trigger a synchronous collection in an attempt to free up space in the GC heap.
§Errors
If the given fields values’ types do not match the field types of the
allocator’s struct type, an error is returned.
If the allocation cannot be satisfied because the GC heap is currently
out of memory, then a GcHeapOutOfMemory<()>
error is returned. The allocation might succeed on a second attempt if
you drop some rooted GC references and try again.
§Panics
Panics if the allocator, or any of the field values, is not associated with the given store.
Sourcepub fn ty(&self, store: impl AsContext) -> Result<StructType>
Available on crate features runtime and gc only.
pub fn ty(&self, store: impl AsContext) -> Result<StructType>
runtime and gc only.Sourcepub fn matches_ty(&self, store: impl AsContext, ty: &StructType) -> Result<bool>
Available on crate features runtime and gc only.
pub fn matches_ty(&self, store: impl AsContext, ty: &StructType) -> Result<bool>
runtime and gc only.Sourcepub fn fields<'a, T: 'static>(
&'a self,
store: impl Into<StoreContextMut<'a, T>>,
) -> Result<impl ExactSizeIterator<Item = Val> + 'a>
Available on crate features runtime and gc only.
pub fn fields<'a, T: 'static>( &'a self, store: impl Into<StoreContextMut<'a, T>>, ) -> Result<impl ExactSizeIterator<Item = Val> + 'a>
runtime and gc only.Sourcepub fn field(&self, store: impl AsContextMut, index: usize) -> Result<Val>
Available on crate features runtime and gc only.
pub fn field(&self, store: impl AsContextMut, index: usize) -> Result<Val>
runtime and gc only.Sourcepub fn set_field(
&self,
store: impl AsContextMut,
index: usize,
value: Val,
) -> Result<()>
Available on crate features runtime and gc only.
pub fn set_field( &self, store: impl AsContextMut, index: usize, value: Val, ) -> Result<()>
runtime and gc only.Set this struct’s indexth field.
§Errors
Returns an error in the following scenarios:
-
When given a value of the wrong type, such as trying to set an
f32field to ani64value. -
When the field is not mutable.
-
When this struct does not have an
indexth field, i.e.indexis out of bounds. -
When
valueis a GC reference that has since been unrooted.
§Panics
Panics if this reference is associated with a different store.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for StructRef
impl RefUnwindSafe for StructRef
impl Send for StructRef
impl Sync for StructRef
impl Unpin for StructRef
impl UnwindSafe for StructRef
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<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more