mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-23 05:31:32 +00:00
doc(runtime-core) Improve documentation of func!
.
This patch explains that `func!` can consume closures.
This commit is contained in:
@ -45,6 +45,44 @@ macro_rules! trace {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Helper macro to create a new `Func` object using the provided function pointer.
|
/// Helper macro to create a new `Func` object using the provided function pointer.
|
||||||
|
///
|
||||||
|
/// # Usage
|
||||||
|
///
|
||||||
|
/// Function pointers or closures are supported. Closures can capture
|
||||||
|
/// their environment (with `move). The first parameter is likely to
|
||||||
|
/// be of kind `vm::Ctx`, though it can be optional.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use wasmer_runtime_core::{imports, func};
|
||||||
|
/// # use wasmer_runtime_core::vm;
|
||||||
|
///
|
||||||
|
/// // A function that has access to `vm::Ctx`.
|
||||||
|
/// fn func_with_vmctx(_: &mut vm::Ctx, n: i32) -> i32 {
|
||||||
|
/// n
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// // A function that cannot access `vm::Ctx`.
|
||||||
|
/// fn func(n: i32) -> i32 {
|
||||||
|
/// n
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// let i = 7;
|
||||||
|
///
|
||||||
|
/// let import_object = imports! {
|
||||||
|
/// "env" => {
|
||||||
|
/// "foo" => func!(func_with_vmctx),
|
||||||
|
/// "bar" => func!(func),
|
||||||
|
/// // A closure with a captured environment, and an access to `vm::Ctx`.
|
||||||
|
/// "baz" => func!(move |_: &mut vm::Ctx, n: i32| -> i32 {
|
||||||
|
/// n + i
|
||||||
|
/// }),
|
||||||
|
/// // A closure without a captured environment, and no access to `vm::Ctx`.
|
||||||
|
/// "qux" => func!(|n: i32| -> i32 {
|
||||||
|
/// n
|
||||||
|
/// }),
|
||||||
|
/// },
|
||||||
|
/// };
|
||||||
|
/// ```
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! func {
|
macro_rules! func {
|
||||||
($func:expr) => {{
|
($func:expr) => {{
|
||||||
|
Reference in New Issue
Block a user