Enum wasmtime::Ref

source ·
pub enum Ref {
    Func(Option<Func>),
    Extern(Option<Rooted<ExternRef>>),
    Any(Option<Rooted<AnyRef>>),
}
Expand description

A reference.

References come in three broad flavors:

  1. Function references. These are references to a function that can be invoked.

  2. External references. These are references to data that is external and opaque to the Wasm guest, provided by the host.

  3. 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 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.

Implementations§

source§

impl Ref

source

pub fn null(heap_type: &HeapType) -> Self

Create a null reference to the given heap type.

source

pub fn is_null(&self) -> bool

Is this a null reference?

source

pub fn is_non_null(&self) -> bool

Is this a non-null reference?

source

pub fn is_extern(&self) -> bool

Is this an extern reference?

source

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.

source

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.

source

pub fn is_any(&self) -> bool

Is this an any reference?

source

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.

source

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.

source

pub fn is_func(&self) -> bool

Is this a func reference?

source

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.

source

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.

source

pub fn ty(&self, store: impl AsContext) -> Result<RefType>

Get the type of this reference.

§Errors

Return an error if this reference has been unrooted.

§Panics

Panics if this reference is associated with a different store.

source

pub fn matches_ty(&self, store: impl AsContext, ty: &RefType) -> Result<bool>

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§

source§

impl Clone for Ref

source§

fn clone(&self) -> Ref

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Ref

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<Func> for Ref

source§

fn from(f: Func) -> Ref

Converts to this type from the input type.
source§

impl From<Option<Func>> for Ref

source§

fn from(f: Option<Func>) -> Ref

Converts to this type from the input type.
source§

impl From<Option<Rooted<AnyRef>>> for Ref

source§

fn from(e: Option<Rooted<AnyRef>>) -> Ref

Converts to this type from the input type.
source§

impl From<Option<Rooted<ArrayRef>>> for Ref

source§

fn from(e: Option<Rooted<ArrayRef>>) -> Ref

Converts to this type from the input type.
source§

impl From<Option<Rooted<ExternRef>>> for Ref

source§

fn from(e: Option<Rooted<ExternRef>>) -> Ref

Converts to this type from the input type.
source§

impl From<Option<Rooted<StructRef>>> for Ref

source§

fn from(e: Option<Rooted<StructRef>>) -> Ref

Converts to this type from the input type.
source§

impl From<Ref> for Val

source§

fn from(val: Ref) -> Val

Converts to this type from the input type.
source§

impl From<Rooted<AnyRef>> for Ref

source§

fn from(e: Rooted<AnyRef>) -> Ref

Converts to this type from the input type.
source§

impl From<Rooted<ArrayRef>> for Ref

source§

fn from(e: Rooted<ArrayRef>) -> Ref

Converts to this type from the input type.
source§

impl From<Rooted<ExternRef>> for Ref

source§

fn from(e: Rooted<ExternRef>) -> Ref

Converts to this type from the input type.
source§

impl From<Rooted<StructRef>> for Ref

source§

fn from(e: Rooted<StructRef>) -> Ref

Converts to this type from the input type.

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
source§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

source§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.