Struct wasmtime::I31

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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
§

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

§

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

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

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

§

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

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

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

§

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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

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>,

§

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>,

§

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,

§

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

source§

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

source§

fn non_i31_gc_refs_count(&self) -> usize

source§

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

source§

unsafe fn invoke<R>( func: NonNull<VMNativeCallFunction>, vmctx1: *mut VMOpaqueContext, vmctx2: *mut VMContext, abi: <T as WasmParams>::Abi ) -> <R as WasmResults>::ResultAbi
where R: WasmResults,

source§

impl<T> WasmResults for T
where T: WasmTy, (<T as WasmTy>::Abi,): HostAbi,

§

type ResultAbi = <(T,) as WasmResults>::ResultAbi

source§

unsafe fn from_abi( store: &mut AutoAssertNoGc<'_>, abi: <T as WasmResults>::ResultAbi ) -> T

source§

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

§

type Abi = <T as WasmTy>::Abi

§

type Retptr = ()

§

type Fallible = Result<T, Error>

source§

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

source§

unsafe fn into_abi_for_ret( self, store: &mut AutoAssertNoGc<'_>, _retptr: () ) -> Result<<T as WasmRet>::Abi, Error>

source§

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

source§

unsafe fn wrap_trampoline( ptr: *mut ValRaw, f: impl FnOnce(<T as WasmRet>::Retptr) -> <T as WasmRet>::Abi )

source§

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

source§

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