Struct wasmtime::component::Func

source ·
pub struct Func(/* private fields */);
Available on crate features runtime and component-model only.
Expand description

A WebAssembly component function which can be called.

This type is the dual of wasmtime::Func for component functions. An instance of Func represents a component function from a component Instance. Like with wasmtime::Func it’s possible to call functions either synchronously or asynchronously and either typed or untyped.

Implementations§

source§

impl Func

source

pub fn typed<Params, Return>( &self, store: impl AsContext ) -> Result<TypedFunc<Params, Return>>
where Params: ComponentNamedList + Lower, Return: ComponentNamedList + Lift,

Attempt to cast this Func to a statically typed TypedFunc with the provided Params and Return.

This function will perform a type-check at runtime that the Func takes Params as parameters and returns Return. If the type-check passes then a TypedFunc will be returned which can be used to invoke the function in an efficient, statically-typed, and ergonomic manner.

The Params type parameter here is a tuple of the parameters to the function. A function which takes no arguments should use (), a function with one argument should use (T,), etc. Note that all Params must also implement the Lower trait since they’re going into wasm.

The Return type parameter is the return value of this function. A return value of () means that there’s no return (similar to a Rust unit return) and otherwise a type T can be specified. Note that the Return must also implement the Lift trait since it’s coming from wasm.

Types specified here must implement the ComponentType trait. This trait is implemented for built-in types to Rust such as integer primitives, floats, Option<T>, Result<T, E>, strings, Vec<T>, and more. As parameters you’ll be passing native Rust types.

See the documentation for ComponentType for more information about supported types.

§Errors

If the function does not actually take Params as its parameters or return Return then an error will be returned.

§Panics

This function will panic if self is not owned by the store specified.

§Examples

Calling a function which takes no parameters and has no return value:

let typed = func.typed::<(), ()>(&store)?;
typed.call(store, ())?;

Calling a function which takes one string parameter and returns a string:

let typed = func.typed::<(&str,), (String,)>(&store)?;
let ret = typed.call(&mut store, ("Hello, ",))?.0;
println!("returned string was: {}", ret);

Calling a function which takes multiple parameters and returns a boolean:

let typed = func.typed::<(u32, Option<&str>, &[u8]), (bool,)>(&store)?;
let ok: bool = typed.call(&mut store, (1, Some("hello"), b"bytes!"))?.0;
println!("return value was: {ok}");
source

pub fn params(&self, store: impl AsContext) -> Box<[Type]>

Get the parameter types for this function.

source

pub fn results(&self, store: impl AsContext) -> Box<[Type]>

Get the result types for this function.

source

pub fn call( &self, store: impl AsContextMut, params: &[Val], results: &mut [Val] ) -> Result<()>

Invokes this function with the params given and returns the result.

The params provided must match the parameters that this function takes in terms of their types and the number of parameters. Results will be written to the results slice provided if the call completes successfully. The initial types of the values in results are ignored and values are overwritten to write the result. It’s required that the size of results exactly matches the number of results that this function produces.

Note that after a function is invoked the embedder needs to invoke Func::post_return to execute any final cleanup required by the guest. This function call is required to either call the function again or to call another function.

For more detailed information see the documentation of TypedFunc::call.

§Errors

Returns an error in situations including but not limited to:

  • params is not the right size or if the values have the wrong type
  • results is not the right size
  • A trap occurs while executing the function
  • The function calls a host function which returns an error

See TypedFunc::call for more information in addition to wasmtime::Func::call.

§Panics

Panics if this is called on a function in an asyncronous store. This only works with functions defined within a synchronous store. Also panics if store does not own this function.

source

pub async fn call_async<T>( &self, store: impl AsContextMut<Data = T>, params: &[Val], results: &mut [Val] ) -> Result<()>
where T: Send,

Available on crate feature async only.

Exactly like Self::call except for use on async stores.

Note that after this Func::post_return_async will be used instead of the synchronous version at Func::post_return.

§Panics

Panics if this is called on a function in a synchronous store. This only works with functions defined within an asynchronous store. Also panics if store does not own this function.

source

pub fn post_return(&self, store: impl AsContextMut) -> Result<()>

Invokes the post-return canonical ABI option, if specified, after a Func::call has finished.

This function is a required method call after a Func::call completes successfully. After the embedder has finished processing the return value then this function must be invoked.

§Errors

This function will return an error in the case of a WebAssembly trap happening during the execution of the post-return function, if specified.

§Panics

This function will panic if it’s not called under the correct conditions. This can only be called after a previous invocation of Func::call completes successfully, and this function can only be called for the same Func that was call’d.

If this function is called when Func::call was not previously called, then it will panic. If a different Func for the same component instance was invoked then this function will also panic because the post-return needs to happen for the other function.

Panics if this is called on a function in an asynchronous store. This only works with functions defined within a synchronous store.

source

pub async fn post_return_async<T: Send>( &self, store: impl AsContextMut<Data = T> ) -> Result<()>

Available on crate feature async only.

Exactly like Self::post_return except for use on async stores.

§Panics

Panics if this is called on a function in a synchronous store. This only works with functions defined within an asynchronous store.

Trait Implementations§

source§

impl Clone for Func

source§

fn clone(&self) -> Func

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 Func

source§

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

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

impl Copy for Func

Auto Trait Implementations§

§

impl Freeze for Func

§

impl RefUnwindSafe for Func

§

impl Send for Func

§

impl Sync for Func

§

impl Unpin for Func

§

impl UnwindSafe for Func

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