pub enum Ref {
Func(Option<Func>),
Extern(Option<Rooted<ExternRef>>),
Any(Option<Rooted<AnyRef>>),
Exn(Option<Rooted<ExnRef>>),
}
Expand description
A reference.
References come in three broad flavors:
-
Function references. These are references to a function that can be invoked.
-
External references. These are references to data that is external and opaque to the Wasm guest, provided by the host.
-
Internal references. These are references to allocations inside the Wasm’s heap, such as structs and arrays. These are part of the GC proposal, and not yet implemented in Wasmtime.
At the Wasm level, there are nullable and non-nullable variants of each type
of reference. Both variants are represented with Ref
at the Wasmtime API
level. For example, values of both (ref extern)
and (ref null extern)
types will be represented as Ref::Extern(Option<ExternRef>)
in the
Wasmtime API. Nullable references are represented as Option<Ref>
where
null references are represented as None
. Wasm can construct null
references via the ref.null <heap-type>
instruction.
References are non-forgable: Wasm cannot create invalid references, for
example, by claiming that the integer 0xbad1bad2
is actually a reference.
Variants§
Func(Option<Func>)
A first-class reference to a WebAssembly function.
The host, or the Wasm guest, can invoke this function.
The host can create function references via Func::new
or
Func::wrap
.
The Wasm guest can create non-null function references via the
ref.func
instruction, or null references via the ref.null func
instruction.
Extern(Option<Rooted<ExternRef>>)
A reference to an value outside of the Wasm heap.
These references are opaque to the Wasm itself. Wasm can’t create non-null external references, nor do anything with them accept pass them around as function arguments and returns and place them into globals and tables.
Wasm can create null external references via the ref.null extern
instruction.
Any(Option<Rooted<AnyRef>>)
An internal reference.
The AnyRef
type represents WebAssembly anyref
values. These can be
references to struct
s and array
s or inline/unboxed 31-bit
integers.
Unlike externref
, Wasm guests can directly allocate anyref
s, and
does not need to rely on the host to do that.
Exn(Option<Rooted<ExnRef>>)
An exception-object reference.
The ExnRef
type represents WebAssembly exnref
values. These are references to exception objects as caught by
catch_ref
clauses on try_table
instructions, or as
allocated via the host API.
Implementations§
Source§impl Ref
impl Ref
Sourcepub fn null(heap_type: &HeapType) -> Self
Available on crate feature runtime
only.
pub fn null(heap_type: &HeapType) -> Self
runtime
only.Create a null reference to the given heap type.
Sourcepub fn is_null(&self) -> bool
Available on crate feature runtime
only.
pub fn is_null(&self) -> bool
runtime
only.Is this a null reference?
Sourcepub fn is_non_null(&self) -> bool
Available on crate feature runtime
only.
pub fn is_non_null(&self) -> bool
runtime
only.Is this a non-null reference?
Sourcepub fn is_extern(&self) -> bool
Available on crate feature runtime
only.
pub fn is_extern(&self) -> bool
runtime
only.Is this an extern
reference?
Sourcepub fn as_extern(&self) -> Option<Option<&Rooted<ExternRef>>>
Available on crate feature runtime
only.
pub fn as_extern(&self) -> Option<Option<&Rooted<ExternRef>>>
runtime
only.Get the underlying extern
reference, if any.
Returns None
if this Ref
is not an extern
reference, eg it is a
func
reference.
Returns Some(None)
if this Ref
is a null extern
reference.
Returns Some(Some(_))
if this Ref
is a non-null extern
reference.
Sourcepub fn unwrap_extern(&self) -> Option<&Rooted<ExternRef>>
Available on crate feature runtime
only.
pub fn unwrap_extern(&self) -> Option<&Rooted<ExternRef>>
runtime
only.Get the underlying extern
reference, panicking if this is a different
kind of reference.
Returns None
if this Ref
is a null extern
reference.
Returns Some(_)
if this Ref
is a non-null extern
reference.
Sourcepub fn as_any(&self) -> Option<Option<&Rooted<AnyRef>>>
Available on crate feature runtime
only.
pub fn as_any(&self) -> Option<Option<&Rooted<AnyRef>>>
runtime
only.Get the underlying any
reference, if any.
Returns None
if this Ref
is not an any
reference, eg it is a
func
reference.
Returns Some(None)
if this Ref
is a null any
reference.
Returns Some(Some(_))
if this Ref
is a non-null any
reference.
Sourcepub fn unwrap_any(&self) -> Option<&Rooted<AnyRef>>
Available on crate feature runtime
only.
pub fn unwrap_any(&self) -> Option<&Rooted<AnyRef>>
runtime
only.Get the underlying any
reference, panicking if this is a different
kind of reference.
Returns None
if this Ref
is a null any
reference.
Returns Some(_)
if this Ref
is a non-null any
reference.
Sourcepub fn as_exn(&self) -> Option<Option<&Rooted<ExnRef>>>
Available on crate feature runtime
only.
pub fn as_exn(&self) -> Option<Option<&Rooted<ExnRef>>>
runtime
only.Get the underlying exn
reference, if any.
Returns None
if this Ref
is not an exn
reference, eg it is a
func
reference.
Returns Some(None)
if this Ref
is a null exn
reference.
Returns Some(Some(_))
if this Ref
is a non-null exn
reference.
Sourcepub fn unwrap_exn(&self) -> Option<&Rooted<ExnRef>>
Available on crate feature runtime
only.
pub fn unwrap_exn(&self) -> Option<&Rooted<ExnRef>>
runtime
only.Get the underlying exn
reference, panicking if this is a different
kind of reference.
Returns None
if this Ref
is a null exn
reference.
Returns Some(_)
if this Ref
is a non-null exn
reference.
Sourcepub fn is_func(&self) -> bool
Available on crate feature runtime
only.
pub fn is_func(&self) -> bool
runtime
only.Is this a func
reference?
Sourcepub fn as_func(&self) -> Option<Option<&Func>>
Available on crate feature runtime
only.
pub fn as_func(&self) -> Option<Option<&Func>>
runtime
only.Get the underlying func
reference, if any.
Returns None
if this Ref
is not an func
reference, eg it is an
extern
reference.
Returns Some(None)
if this Ref
is a null func
reference.
Returns Some(Some(_))
if this Ref
is a non-null func
reference.
Sourcepub fn unwrap_func(&self) -> Option<&Func>
Available on crate feature runtime
only.
pub fn unwrap_func(&self) -> Option<&Func>
runtime
only.Get the underlying func
reference, panicking if this is a different
kind of reference.
Returns None
if this Ref
is a null func
reference.
Returns Some(_)
if this Ref
is a non-null func
reference.
Sourcepub fn ty(&self, store: impl AsContext) -> Result<RefType>
Available on crate feature runtime
only.
pub fn ty(&self, store: impl AsContext) -> Result<RefType>
runtime
only.Sourcepub fn matches_ty(&self, store: impl AsContext, ty: &RefType) -> Result<bool>
Available on crate feature runtime
only.
pub fn matches_ty(&self, store: impl AsContext, ty: &RefType) -> Result<bool>
runtime
only.Does this reference value match the given type?
Returns an error if the underlying Rooted
has been unrooted.
§Panics
Panics if this reference is not associated with the given store.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Ref
impl RefUnwindSafe for Ref
impl Send for Ref
impl Sync for Ref
impl Unpin for Ref
impl UnwindSafe for Ref
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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