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 eqref
s, include structref
s, arrayref
s, and
i31ref
s. funcref
s, exnref
s, and externref
s cannot be tested for
equality by Wasm, and are not eqref
s.
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
impl EqRef
sourcepub fn unwrap_i31(&self, store: impl AsContext) -> Result<I31>
pub fn unwrap_i31(&self, store: impl AsContext) -> Result<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
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