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>
impl<Params, Return> TypedFunc<Params, Return>
Sourcepub unsafe fn new_unchecked(func: Func) -> TypedFunc<Params, Return>
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.
Sourcepub fn call(
&self,
store: impl AsContextMut,
params: Params,
) -> Result<Return, Error>
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
storerequires usingSelf::call_asyncinstead, 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.
Sourcepub async fn call_async(
&self,
store: impl AsContextMut,
params: Params,
) -> Result<Return, Error>
pub async fn call_async( &self, store: impl AsContextMut, params: Params, ) -> Result<Return, Error>
Exactly like Self::call, except for invoking WebAssembly
asynchronously.
§Panics
Panics if store does not own this function.
Sourcepub async fn call_concurrent(
self,
accessor: impl AsAccessor,
params: Params,
) -> Result<(Return, TaskExit), Error>
pub async fn call_concurrent( self, accessor: impl AsAccessor, params: Params, ) -> Result<(Return, TaskExit), Error>
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§
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>
impl<Params, Return> Sync for TypedFunc<Params, Return>
impl<Params, Return> Unpin for TypedFunc<Params, Return>
impl<Params, Return> UnwindSafe for TypedFunc<Params, Return>where
Params: UnwindSafe,
Return: UnwindSafe,
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<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
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