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 ty(&self, store: impl AsContext) -> Result<HeapType>
Available on crate features runtime and gc only.
pub fn ty(&self, store: impl AsContext) -> Result<HeapType>
runtime and gc only.Sourcepub fn matches_ty(&self, store: impl AsContext, ty: &HeapType) -> Result<bool>
Available on crate features runtime and gc only.
pub fn matches_ty(&self, store: impl AsContext, ty: &HeapType) -> Result<bool>
runtime and gc only.Sourcepub fn from_i31(store: impl AsContextMut, value: I31) -> Rooted<Self>
Available on crate features runtime and gc only.
pub fn from_i31(store: impl AsContextMut, value: I31) -> Rooted<Self>
runtime and gc only.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);Sourcepub fn is_i31(&self, store: impl AsContext) -> Result<bool>
Available on crate features runtime and gc only.
pub fn is_i31(&self, store: impl AsContext) -> Result<bool>
runtime and gc only.Sourcepub fn as_i31(&self, store: impl AsContext) -> Result<Option<I31>>
Available on crate features runtime and gc only.
pub fn as_i31(&self, store: impl AsContext) -> Result<Option<I31>>
runtime and gc only.Sourcepub fn unwrap_i31(&self, store: impl AsContext) -> Result<I31>
Available on crate features runtime and gc only.
pub fn unwrap_i31(&self, store: impl AsContext) -> Result<I31>
runtime and gc only.Sourcepub fn is_struct(&self, store: impl AsContext) -> Result<bool>
Available on crate features runtime and gc only.
pub fn is_struct(&self, store: impl AsContext) -> Result<bool>
runtime and gc only.Sourcepub fn as_struct(
&self,
store: impl AsContext,
) -> Result<Option<Rooted<StructRef>>>
Available on crate features runtime and gc only.
pub fn as_struct( &self, store: impl AsContext, ) -> Result<Option<Rooted<StructRef>>>
runtime and gc only.Sourcepub fn unwrap_struct(&self, store: impl AsContext) -> Result<Rooted<StructRef>>
Available on crate features runtime and gc only.
pub fn unwrap_struct(&self, store: impl AsContext) -> Result<Rooted<StructRef>>
runtime and gc only.Sourcepub fn is_array(&self, store: impl AsContext) -> Result<bool>
Available on crate features runtime and gc only.
pub fn is_array(&self, store: impl AsContext) -> Result<bool>
runtime and gc only.Sourcepub fn as_array(
&self,
store: impl AsContext,
) -> Result<Option<Rooted<ArrayRef>>>
Available on crate features runtime and gc only.
pub fn as_array( &self, store: impl AsContext, ) -> Result<Option<Rooted<ArrayRef>>>
runtime and gc only.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