pub struct EqRef { /* private fields */ }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 OwnedRooted<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
impl EqRef
Sourcepub fn from_i31(store: impl AsContextMut, value: I31) -> Rooted<EqRef>
pub fn from_i31(store: impl AsContextMut, value: I31) -> Rooted<EqRef>
Construct an eqref from an i31.
§Example
let mut store = Store::<()>::default();
// Create an `i31`.
let i31 = I31::wrapping_u32(999);
// Convert it into an `eqref`.
let eqref = EqRef::from_i31(&mut store, i31);Trait Implementations§
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> 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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> 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