pub struct I31(/* private fields */);
gc
and runtime
only.Expand description
A 31-bit integer.
Represents WebAssembly’s (ref i31)
and (ref null i31)
(aka i31ref
)
references.
You can convert this into any of the (ref i31)
supertypes such as (ref eq)
or (ref any)
, and their nullable equivalents. After conversion, the
resulting reference does not actually point at a GC object in the heap,
instead it is a 31-bit integer that is stored unboxed/inline in the
reference itself.
§Example
// Enable the Wasm GC proposal for Wasm to use i31 references.
let mut config = Config::new();
config.wasm_gc(true);
let engine = Engine::new(&config)?;
let mut store = Store::new(&engine, ());
// A Wasm module that exports a function that increments an i31.
let module = Module::new(&engine, r#"
(module
(func (export "inc_i31") (param (ref i31)) (result (ref i31))
local.get 0
i31.get_u
i32.const 1
i32.add
ref.i31
)
"#)?;
// Instantiate the module.
let instance = Instance::new(&mut store, &module, &[])?;
// Get the exported `inc_i31` function.
let inc_i31 = instance.get_func(&mut store, "inc_i31").unwrap();
// Call the function using the untyped functions API, meaning we need to
// pack our `I31` argument into an `AnyRef` that is packed into a `Val`, and
// then we need to do the opposite unpacking to extract the result.
let i31 = I31::wrapping_u32(0x1234);
let anyref = AnyRef::from_i31(&mut store, i31);
let val = Val::AnyRef(Some(anyref));
let mut results = [Val::null_any_ref()];
inc_i31.call(&mut store, &[val], &mut results)?;
let nullable_anyref = results[0].unwrap_anyref();
let anyref = nullable_anyref.unwrap();
let i31 = anyref.unwrap_i31(&store)?;
assert_eq!(i31.get_u32(), 0x1235);
// Alternatively, we can use the typed function API to make this all a lot
// more ergonomic.
let inc_i31 = inc_i31.typed::<I31, I31>(&mut store)?;
let i31 = I31::wrapping_u32(0x5678);
let result = inc_i31.call(&mut store, i31)?;
assert_eq!(result.get_u32(), 0x5679);
Implementations§
Source§impl I31
impl I31
Sourcepub fn new_u32(value: u32) -> Option<Self>
pub fn new_u32(value: u32) -> Option<Self>
Construct a new I31
from the given unsigned value.
Returns None
if the value does not fit in the bottom 31 bits.
§Example
// This value does not fit into 31 bits.
assert!(I31::new_u32(0x8000_0000).is_none());
// This value does fit into 31 bits.
let x = I31::new_u32(5).unwrap();
assert_eq!(x.get_u32(), 5);
Sourcepub fn new_i32(value: i32) -> Option<Self>
pub fn new_i32(value: i32) -> Option<Self>
Construct a new I31
from the given signed value.
Returns None
if the value does not fit in the bottom 31 bits.
§Example
// This value does not fit into 31 bits.
assert!(I31::new_i32(-2147483648).is_none());
// This value does fit into 31 bits.
let x = I31::new_i32(-5).unwrap();
assert_eq!(x.get_i32(), -5);
Sourcepub fn wrapping_u32(value: u32) -> Self
pub fn wrapping_u32(value: u32) -> Self
Construct a new I31
from the given unsigned value.
If the value doesn’t fit in the bottom 31 bits, it is wrapped such that the wrapped value does.
§Example
// Values that fit in 31 bits are preserved.
let x = I31::wrapping_u32(5);
assert_eq!(x.get_u32(), 5);
// Values that do not fit in 31 bits are wrapped to 31 bits.
let y = I31::wrapping_u32(0xffff_ffff);
assert_eq!(y.get_u32(), 0x7fff_ffff);
Sourcepub fn wrapping_i32(value: i32) -> Self
pub fn wrapping_i32(value: i32) -> Self
Construct a new I31
from the given signed value.
If the value doesn’t fit in the bottom 31 bits, it is wrapped such that the wrapped value does.
§Example
// Values that fit in 31 bits are preserved.
let x = I31::wrapping_i32(-5);
assert_eq!(x.get_i32(), -5);
// Values that do not fit in 31 bits are wrapped to 31 bits.
let y = I31::wrapping_i32(-1073741825); // 0xbfffffff
assert_eq!(y.get_i32(), 1073741823); // 0x3fffffff
Trait Implementations§
impl Copy for I31
impl Eq for I31
impl StructuralPartialEq for I31
impl WasmTy for I31
Auto Trait Implementations§
impl Freeze for I31
impl RefUnwindSafe for I31
impl Send for I31
impl Sync for I31
impl Unpin for I31
impl UnwindSafe for I31
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
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