wasmtime

Struct I31

Source
pub struct I31(/* private fields */);
Available on crate features 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

Source

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);
Source

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);
Source

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);
Source

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
Source

pub fn get_u32(&self) -> u32

Get this I31’s value as an unsigned integer.

§Example
let x = I31::new_i32(-1).unwrap();
assert_eq!(x.get_u32(), 0x7fff_ffff);
Source

pub fn get_i32(&self) -> i32

Get this I31’s value as a signed integer.

§Example
let x = I31::new_u32(0x7fff_ffff).unwrap();
assert_eq!(x.get_i32(), -1);

Trait Implementations§

Source§

impl Clone for I31

Source§

fn clone(&self) -> I31

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for I31

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for I31

Source§

fn default() -> I31

Returns the “default value” for a type. Read more
Source§

impl Hash for I31

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for I31

Source§

fn eq(&self, other: &I31) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for I31

Source§

impl Eq for I31

Source§

impl StructuralPartialEq for I31

Source§

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> 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize = _

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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> WasmParams for T
where T: WasmTy,

Source§

type ValRawStorage = <(T,) as WasmParams>::ValRawStorage

Source§

fn typecheck( engine: &Engine, params: impl ExactSizeIterator<Item = ValType>, position: TypeCheckPosition, ) -> Result<(), Error>

Source§

fn vmgcref_pointing_to_object_count(&self) -> usize

Source§

fn store( self, store: &mut AutoAssertNoGc<'_>, func_ty: &FuncType, dst: &mut MaybeUninit<<T as WasmParams>::ValRawStorage>, ) -> Result<(), Error>

Source§

impl<T> WasmResults for T
where T: WasmTy,

Source§

unsafe fn load( store: &mut AutoAssertNoGc<'_>, abi: &<T as WasmParams>::ValRawStorage, ) -> T

Source§

impl<T> WasmRet for T
where T: WasmTy,

Source§

type Fallible = Result<T, Error>

Source§

fn compatible_with_store(&self, store: &StoreOpaque) -> bool

Source§

unsafe fn store( self, store: &mut AutoAssertNoGc<'_>, ptr: &mut [MaybeUninit<ValRaw>], ) -> Result<(), Error>

Source§

fn may_gc() -> bool

Source§

fn func_type(engine: &Engine, params: impl Iterator<Item = ValType>) -> FuncType

Source§

fn into_fallible(self) -> Result<T, Error>

Source§

fn fallible_from_error(error: Error) -> Result<T, Error>