Skip to main content

TypedFunc

Struct TypedFunc 

Source
pub struct TypedFunc<Params, Return> { /* private fields */ }
Expand description

A statically-typed version of Func which takes Params as input and returns Return.

This is an efficient way to invoke a WebAssembly component where if the inputs and output are statically known this can eschew the vast majority of machinery and checks when calling WebAssembly. This is the most optimized way to call a WebAssembly component.

Note that like Func this is a pointer within a Store and usage will panic if used with the wrong store.

This type is primarily created with the Func::typed API.

See ComponentType for more information about supported types.

Implementations§

Source§

impl<Params, Return> TypedFunc<Params, Return>
where Params: ComponentNamedList + Lower, Return: ComponentNamedList + Lift,

Source

pub unsafe fn new_unchecked(func: Func) -> TypedFunc<Params, Return>

Creates a new TypedFunc from the provided component Func, unsafely asserting that the underlying function takes Params as input and returns Return.

§Unsafety

This is an unsafe function because it does not verify that the Func provided actually implements this signature. It’s up to the caller to have performed some other sort of check to ensure that the signature is correct.

Source

pub fn func(&self) -> &Func

Returns the underlying un-typed Func that this TypedFunc references.

Source

pub fn call( &self, store: impl AsContextMut, params: Params, ) -> Result<Return, Error>

Calls the underlying WebAssembly component function using the provided params as input.

This method is used to enter into a component. Execution happens within the store provided. The params are copied into WebAssembly memory as appropriate and a core wasm function is invoked.

§Post-return

In the component model each function can have a “post return” specified which allows cleaning up the arguments returned to the host. For example if WebAssembly returns a string to the host then it might be a uniquely allocated string which, after the host finishes processing it, needs to be deallocated in the wasm instance’s own linear memory to prevent memory leaks in wasm itself. The post-return canonical abi option is used to configured this.

If a post-return function is present, it will be called automatically by this function.

§Errors

This function can return an error for a number of reasons:

  • If the wasm itself traps during execution.
  • If the wasm traps while copying arguments into memory.
  • If the wasm provides bad allocation pointers when copying arguments into memory.
  • If the wasm returns a value which violates the canonical ABI.
  • If this function’s instances cannot be entered, for example if the instance is currently calling a host function.
  • If store requires using Self::call_async instead, see crate documentation for more info.

In general there are many ways that things could go wrong when copying types in and out of a wasm module with the canonical ABI, and certain error conditions are specific to certain types. For example a WebAssembly module can’t return an invalid char. When allocating space for this host to copy a string into the returned pointer must be in-bounds in memory.

If an error happens then the error should contain detailed enough information to understand which part of the canonical ABI went wrong and what to inspect.

§Panics

Panics if store does not own this function.

Source

pub async fn call_async( &self, store: impl AsContextMut, params: Params, ) -> Result<Return, Error>
where Return: 'static, <impl AsContextMut as AsContext>::Data: Send,

Exactly like Self::call, except for invoking WebAssembly asynchronously.

§Panics

Panics if store does not own this function.

Source

pub async fn call_concurrent( self, accessor: impl AsAccessor, params: Params, ) -> Result<(Return, TaskExit), Error>
where Params: 'static, Return: 'static, <impl AsAccessor as AsAccessor>::Data: Send,

Start a concurrent call to this function.

Concurrency is achieved by relying on the Accessor argument, which can be obtained by calling StoreContextMut::run_concurrent.

Unlike Self::call and Self::call_async (both of which require exclusive access to the store until the completion of the call), calls made using this method may run concurrently with other calls to the same instance. In addition, the runtime will call the post-return function (if any) automatically when the guest task completes.

Besides the task’s return value, this returns a TaskExit representing the completion of the guest task and any transitive subtasks it might create.

This function will return an error if Config::concurrency_support is disabled.

§Progress and Cancellation

For more information about how to make progress on the wasm task or how to cancel the wasm task see the documentation for Func::call_concurrent.

§Panics

Panics if the store that the Accessor is derived from does not own this function.

§Example

Using StoreContextMut::run_concurrent to get an Accessor:

let my_typed_func = instance.get_typed_func::<(), ()>(&mut store, "my_typed_func")?;
store.run_concurrent(async |accessor| -> wasmtime::Result<_> {
   my_typed_func.call_concurrent(accessor, ()).await?;
   Ok(())
}).await??;

Trait Implementations§

Source§

impl<Params, Return> Clone for TypedFunc<Params, Return>

Source§

fn clone(&self) -> TypedFunc<Params, Return>

Returns a duplicate 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<Params, Return> Copy for TypedFunc<Params, Return>

Auto Trait Implementations§

§

impl<Params, Return> Freeze for TypedFunc<Params, Return>

§

impl<Params, Return> RefUnwindSafe for TypedFunc<Params, Return>
where Params: RefUnwindSafe, Return: RefUnwindSafe,

§

impl<Params, Return> Send for TypedFunc<Params, Return>
where Params: Send, Return: Send,

§

impl<Params, Return> Sync for TypedFunc<Params, Return>
where Params: Sync, Return: Sync,

§

impl<Params, Return> Unpin for TypedFunc<Params, Return>
where Params: Unpin, Return: Unpin,

§

impl<Params, Return> UnwindSafe for TypedFunc<Params, Return>
where Params: UnwindSafe, Return: UnwindSafe,

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, dest: *mut u8)

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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
§

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> Pointee for T

Source§

type Pointer = u32

Source§

fn debug( pointer: <T as Pointee>::Pointer, f: &mut Formatter<'_>, ) -> Result<(), Error>

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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more