pub enum Ref {
Func(Option<Func>),
Extern(Option<Rooted<ExternRef>>),
Any(Option<Rooted<AnyRef>>),
}
runtime
only.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.
Implementations§
Source§impl Ref
impl Ref
Sourcepub fn is_non_null(&self) -> bool
pub fn is_non_null(&self) -> bool
Is this a non-null reference?
Sourcepub fn as_extern(&self) -> Option<Option<&Rooted<ExternRef>>>
pub fn as_extern(&self) -> Option<Option<&Rooted<ExternRef>>>
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>>
pub fn unwrap_extern(&self) -> Option<&Rooted<ExternRef>>
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>>>
pub fn as_any(&self) -> Option<Option<&Rooted<AnyRef>>>
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>>
pub fn unwrap_any(&self) -> Option<&Rooted<AnyRef>>
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_func(&self) -> Option<Option<&Func>>
pub fn as_func(&self) -> Option<Option<&Func>>
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>
pub fn unwrap_func(&self) -> Option<&Func>
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.
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