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,