Enum Ref
pub enum Ref {
Func(Option<Func>),
Extern(Option<MaybeRooted<ExternRef>>),
Any(Option<MaybeRooted<AnyRef>>),
Exn(Option<MaybeRooted<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<MaybeRooted<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<MaybeRooted<AnyRef>>)
An internal reference.
The AnyRef type represents WebAssembly anyref values. These can be
references to structs and arrays or inline/unboxed 31-bit
integers.
Unlike externref, Wasm guests can directly allocate anyrefs, and
does not need to rely on the host to do that.
Exn(Option<MaybeRooted<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§
§impl Ref
impl Ref
pub fn is_non_null(&self) -> bool
pub fn is_non_null(&self) -> bool
Is this a non-null reference?
pub fn as_extern(&self) -> Option<Option<&MaybeRooted<ExternRef>>>
pub fn as_extern(&self) -> Option<Option<&MaybeRooted<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.
pub fn unwrap_extern(&self) -> Option<&MaybeRooted<ExternRef>>
pub fn unwrap_extern(&self) -> Option<&MaybeRooted<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.
pub fn as_any(&self) -> Option<Option<&MaybeRooted<AnyRef>>>
pub fn as_any(&self) -> Option<Option<&MaybeRooted<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.
pub fn unwrap_any(&self) -> Option<&MaybeRooted<AnyRef>>
pub fn unwrap_any(&self) -> Option<&MaybeRooted<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.
pub fn as_exn(&self) -> Option<Option<&MaybeRooted<ExnRef>>>
pub fn as_exn(&self) -> Option<Option<&MaybeRooted<ExnRef>>>
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.
pub fn unwrap_exn(&self) -> Option<&MaybeRooted<ExnRef>>
pub fn unwrap_exn(&self) -> Option<&MaybeRooted<ExnRef>>
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.
pub 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.
pub 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.
pub fn matches_ty(
&self,
store: impl AsContext,
ty: &RefType,
) -> Result<bool, Error>
pub fn matches_ty( &self, store: impl AsContext, ty: &RefType, ) -> Result<bool, Error>
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.
pub fn ensure_rooted(&mut self, store: impl AsContextMut) -> Result<(), Error>
pub fn ensure_rooted(&mut self, store: impl AsContextMut) -> Result<(), Error>
Root this reference, in place, if it is an unrooted GC reference; see
Val::ensure_rooted.
§Errors
Returns an error if this is an unrooted GC reference that is no longer valid.
§Panics
Panics if this reference is associated with a different store.
pub fn validate_rooted(&self) -> Result<(), Error>
pub fn validate_rooted(&self) -> Result<(), Error>
Return an error if this is a GC reference that is not rooted; see
Val::validate_rooted.
§Errors
Returns an error if this is an unrooted GC reference.
pub fn to_rooted(&self, store: impl AsContextMut) -> Result<Ref, Error>
pub fn to_rooted(&self, store: impl AsContextMut) -> Result<Ref, Error>
Get a copy of this reference that is rooted if it is a GC reference;
see Ref::ensure_rooted.
§Errors
Returns an error if this is an unrooted GC reference that is no longer valid.
§Panics
Panics if this reference is associated with a different store.
pub fn to_unrooted(&self, store: impl AsContext) -> Result<Ref, Error>
pub fn to_unrooted(&self, store: impl AsContext) -> Result<Ref, Error>
Get a copy of this reference that is unrooted if it is a GC reference;
see Val::to_unrooted.
§Errors
Returns an error if this is a GC reference that is no longer valid.
§Panics
Panics if this reference is associated with a different store.
Trait Implementations§
§impl From<MaybeRooted<AnyRef>> for Ref
impl From<MaybeRooted<AnyRef>> for Ref
§fn from(e: MaybeRooted<AnyRef>) -> Ref
fn from(e: MaybeRooted<AnyRef>) -> Ref
§impl From<MaybeRooted<ArrayRef>> for Ref
impl From<MaybeRooted<ArrayRef>> for Ref
§fn from(e: MaybeRooted<ArrayRef>) -> Ref
fn from(e: MaybeRooted<ArrayRef>) -> Ref
§impl From<MaybeRooted<EqRef>> for Ref
impl From<MaybeRooted<EqRef>> for Ref
§fn from(e: MaybeRooted<EqRef>) -> Ref
fn from(e: MaybeRooted<EqRef>) -> Ref
§impl From<MaybeRooted<ExnRef>> for Ref
impl From<MaybeRooted<ExnRef>> for Ref
§fn from(e: MaybeRooted<ExnRef>) -> Ref
fn from(e: MaybeRooted<ExnRef>) -> Ref
§impl From<MaybeRooted<ExternRef>> for Ref
impl From<MaybeRooted<ExternRef>> for Ref
§fn from(e: MaybeRooted<ExternRef>) -> Ref
fn from(e: MaybeRooted<ExternRef>) -> Ref
§impl From<MaybeRooted<StructRef>> for Ref
impl From<MaybeRooted<StructRef>> for Ref
§fn from(e: MaybeRooted<StructRef>) -> Ref
fn from(e: MaybeRooted<StructRef>) -> Ref
Auto Trait Implementations§
impl Forget for Ref
impl Freeze for Ref
impl Move for Ref
impl RefUnwindSafe for Ref
impl Send for Ref
impl Sync for Ref
impl Unpin for Ref
impl UnsafeUnpin 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,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
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