Struct wasmedge_sdk::Func

source ·
pub struct Func { /* private fields */ }
Expand description

Defines a host function instance.

A WasmEdge Func is a wasm function instance, which is a “wrapper” of the original function (defined in either the host or the WebAssembly module) over the runtime module instance of its originating module.

Implementations§

source§

impl Func

source

pub fn wrap<Args, Rets, T>( real_func: impl Fn(CallingFrame, Vec<WasmValue>, *mut c_void) -> Result<Vec<WasmValue>, HostFuncError> + Send + Sync + 'static, data: Option<Box<T>> ) -> WasmEdgeResult<Self>where Args: WasmValTypeList, Rets: WasmValTypeList,

Creates a host function by wrapping a native function.

N.B. that this function can be used in thread-safe scenarios.

Arguments
  • real_func - The native function to be wrapped.

  • data - The host context data used in this function.

Error
source

pub fn wrap_with_type<T>( ty: FuncType, real_func: impl Fn(CallingFrame, Vec<WasmValue>, *mut c_void) -> Result<Vec<WasmValue>, HostFuncError> + Send + Sync + 'static, data: Option<Box<T>> ) -> WasmEdgeResult<Self>

Creates a host function with the given func type.

N.B. that this function can be used in thread-safe scenarios.

Arguments
  • ty - The function type.

  • real_func - The native function that will be wrapped as a host function.

  • data - The host context data used in this function.

Error
source

pub fn wrap_async_func<Args, Rets, T>( real_func: impl Fn(CallingFrame, Vec<WasmValue>, *mut c_void) -> Box<dyn Future<Output = Result<Vec<WasmValue>, HostFuncError>> + Send> + Send + Sync + 'static, data: Option<Box<T>> ) -> WasmEdgeResult<Self>where Args: WasmValTypeList, Rets: WasmValTypeList, T: Send + Sync,

Available on crate feature async and Linux only.

Creates an asynchronous host function by wrapping a native async function.

N.B. that this function can be used in thread-safe scenarios.

Arguments
  • real_func - The native function to be wrapped.

  • data - The host context data used in this function.

Error
source

pub fn wrap_async_func_with_type<T>( ty: FuncType, real_func: impl Fn(CallingFrame, Vec<WasmValue>, *mut c_void) -> Box<dyn Future<Output = Result<Vec<WasmValue>, HostFuncError>> + Send> + Send + Sync + 'static, data: Option<Box<T>> ) -> WasmEdgeResult<Self>where T: Send + Sync,

Available on crate feature async and Linux only.

Creates an asynchronous host function by wrapping a native async function with the given function type.

N.B. that this function can be used in thread-safe scenarios.

Arguments
  • ty - The function type.

  • real_func - The native function to be wrapped.

  • data - The host context data used in this function.

Error
source

pub fn name(&self) -> Option<&str>

Returns the exported name of this function.

Notice that this field is meaningful only if this host function is used as an exported instance.

source

pub fn mod_name(&self) -> Option<&str>

Returns the name of the module instance from which this function exports.

Notice that this field is meaningful only if this host function is used as an exported instance.

source

pub fn ty(&self) -> &FuncType

Returns a reference to the type of the host function.

source

pub fn as_ref(&self) -> FuncRef

Returns a reference to this function instance.

source

pub fn run( &self, executor: &Executor, args: impl IntoIterator<Item = WasmValue> ) -> WasmEdgeResult<Vec<WasmValue>>

Runs this host function and returns the result.

Arguments
  • executor - The Executor instance.

  • args - The arguments passed to the host function.

Error

If fail to run the host function, then an error is returned.

source

pub fn run_with_timeout( &self, executor: &Executor, args: impl IntoIterator<Item = WasmValue>, timeout: Duration ) -> WasmEdgeResult<Vec<WasmValue>>

Available on Linux and non-musl only.

Runs this host function with a timeout setting.

Arguments
  • executor - The Executor instance.

  • args - The arguments passed to the host function.

  • timeout - The maximum execution time of the function to be run.

Error

If fail to run the host function, then an error is returned.

source

pub async fn run_async( &self, async_state: &AsyncState, executor: &Executor, args: impl IntoIterator<Item = WasmValue> + Send ) -> WasmEdgeResult<Vec<WasmValue>>

Available on crate feature async and Linux only.

Asynchronously runs this host function and returns the result.

Arguments
  • executor - The Executor instance.

  • args - The arguments passed to the host function.

Error

If fail to run the host function, then an error is returned.

source

pub async fn run_async_with_timeout( &self, async_state: &AsyncState, executor: &Executor, args: impl IntoIterator<Item = WasmValue> + Send, timeout: Duration ) -> WasmEdgeResult<Vec<WasmValue>>

Available on crate feature async and Linux and non-musl only.

Asynchronously runs this host function with a timeout setting.

Arguments
  • async_state - Used to store asynchronous state at run time.

  • executor - The Executor instance.

  • args - The arguments passed to the host function.

  • timeout - The maximum execution time of the function to be run.

Error

If fail to run the host function, then an error is returned.

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

Auto Trait Implementations§

§

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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> ToOwned for Twhere 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 Twhere 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 Twhere 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.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V