wasmtime

Struct EqRef

source
pub struct EqRef { /* private fields */ }
Available on crate features gc and runtime only.
Expand description

A reference to a GC-managed object that can be tested for equality.

The WebAssembly reference types that can be tested for equality, and therefore are eqrefs, include structrefs, arrayrefs, and i31refs. funcrefs, exnrefs, and externrefs cannot be tested for equality by Wasm, and are not eqrefs.

Use the Rooted::ref_eq method to actually test two references for equality.

Like all WebAssembly references, these are opaque to and unforgeable by Wasm: they cannot be faked and Wasm cannot, for example, cast the integer 0x12345678 into a reference, pretend it is a valid eqref, and trick the host into dereferencing it and segfaulting or worse.

Note that you can also use Rooted<EqRef> and ManuallyRooted<EqRef> as a type parameter with Func::typed- and Func::wrap-style APIs.

§Example

use wasmtime::*;

let mut config = Config::new();
config.wasm_function_references(true);
config.wasm_gc(true);

let engine = Engine::new(&config)?;
let mut store = Store::new(&engine, ());

// Define a module that exports a function that returns a new `eqref` each
// time it is invoked.
let module = Module::new(&engine, r#"
    (module
        (global $g (mut i32) (i32.const 0))
        (func (export "new-eqref") (result (ref eq))
            ;; Increment $g.
            global.get $g
            i32.const 1
            i32.add
            global.set $g

            ;; Create an `i31ref`, which is a kind of `eqref`, from $g.
            global.get $g
            ref.i31
        )
    )
"#)?;

// Instantiate the module.
let instance = Instance::new(&mut store, &module, &[])?;

// Get the exported function.
let new_eqref = instance.get_typed_func::<(), Rooted<EqRef>>(&mut store, "new-eqref")?;

{
    let mut scope = RootScope::new(&mut store);

    // Call the function to get an `eqref`.
    let x = new_eqref.call(&mut scope, ())?;

    // `x` is equal to itself!
    assert!(Rooted::ref_eq(&scope, &x, &x)?);

    // Call the function again to get a new, different `eqref`.
    let y = new_eqref.call(&mut scope, ())?;

    // `x` is not equal to `y`!
    assert!(!Rooted::ref_eq(&scope, &x, &y)?);
}

Implementations§

source§

impl EqRef

source

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

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: &HeapType) -> Result<bool>

Does this eqref match the given type?

That is, is this object’s type a subtype of the given type?

§Errors

Return an error if this reference has been unrooted.

§Panics

Panics if this reference is associated with a different store.

source

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

Is this eqref an i31?

§Errors

Return an error if this reference has been unrooted.

§Panics

Panics if this reference is associated with a different store.

source

pub fn as_i31(&self, store: impl AsContext) -> Result<Option<I31>>

Downcast this eqref to an i31.

If this eqref is an i31, then Some(_) is returned.

If this eqref is not an i31, then None is returned.

§Errors

Return an error if this reference has been unrooted.

§Panics

Panics if this reference is associated with a different store.

source

pub fn unwrap_i31(&self, store: impl AsContext) -> Result<I31>

Downcast this eqref to an i31, panicking if this eqref is not an i31.

§Errors

Return an error if this reference has been unrooted.

§Panics

Panics if this reference is associated with a different store, or if this eqref is not an i31.

source

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

Is this eqref a structref?

§Errors

Return an error if this reference has been unrooted.

§Panics

Panics if this reference is associated with a different store.

source

pub fn as_struct( &self, store: impl AsContext, ) -> Result<Option<Rooted<StructRef>>>

Downcast this eqref to a structref.

If this eqref is a structref, then Some(_) is returned.

If this eqref is not a structref, then None is returned.

§Errors

Return an error if this reference has been unrooted.

§Panics

Panics if this reference is associated with a different store.

source

pub fn unwrap_struct(&self, store: impl AsContext) -> Result<Rooted<StructRef>>

Downcast this eqref to a structref, panicking if this eqref is not a structref.

§Errors

Return an error if this reference has been unrooted.

§Panics

Panics if this reference is associated with a different store, or if this eqref is not a struct.

source

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

Is this eqref an arrayref?

§Errors

Return an error if this reference has been unrooted.

§Panics

Panics if this reference is associated with a different store.

source

pub fn as_array( &self, store: impl AsContext, ) -> Result<Option<Rooted<ArrayRef>>>

Downcast this eqref to an arrayref.

If this eqref is an arrayref, then Some(_) is returned.

If this eqref is not an arrayref, then None is returned.

§Errors

Return an error if this reference has been unrooted.

§Panics

Panics if this reference is associated with a different store.

source

pub fn unwrap_array(&self, store: impl AsContext) -> Result<Rooted<ArrayRef>>

Downcast this eqref to an arrayref, panicking if this eqref is not an arrayref.

§Errors

Return an error if this reference has been unrooted.

§Panics

Panics if this reference is associated with a different store, or if this eqref is not an array.

Trait Implementations§

source§

impl Debug for EqRef

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for EqRef

§

impl RefUnwindSafe for EqRef

§

impl Send for EqRef

§

impl Sync for EqRef

§

impl Unpin for EqRef

§

impl UnwindSafe for EqRef

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> 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, 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.
source§

impl<T> GcRef for T
where T: GcRefImpl,