wasmtime

Enum Ref

Source
pub enum Ref {
    Func(Option<Func>),
    Extern(Option<Rooted<ExternRef>>),
    Any(Option<Rooted<AnyRef>>),
}
Available on crate feature runtime only.
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 u8)

🔬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
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

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.