Skip to main content

define_async_function

Macro define_async_function 

Source
macro_rules! define_async_function {
    (
		$struct_name:ident,
		$func_name:literal,
		() -> $ret:ident,
		$impl_fn:expr
	) => { ... };
    (
		$struct_name:ident,
		$func_name:literal,
		($arg_name:ident : $arg_type:ident) -> $ret:ident,
		$impl_fn:expr
	) => { ... };
    (
		$struct_name:ident,
		$func_name:literal,
		($arg1_name:ident : $arg1_type:ident, $arg2_name:ident : $arg2_type:ident) -> $ret:ident,
		$impl_fn:expr
	) => { ... };
    (
		$struct_name:ident,
		$func_name:literal,
		($arg1_name:ident : $arg1_type:ident, ? $arg2_name:ident : $arg2_type:ident) -> $ret:ident,
		$impl_fn:expr
	) => { ... };
    (
		$struct_name:ident,
		$func_name:literal,
		($arg1_name:ident : $arg1_type:ident, ? $arg2_name:ident : $arg2_type:ident, ? $arg3_name:ident : $arg3_type:ident) -> $ret:ident,
		$impl_fn:expr
	) => { ... };
}
Expand description

Define an async scalar function that needs access to EvalContext.

Async functions are used for:

  • I/O-bound operations (HTTP requests)
  • CPU-intensive operations (crypto hashing)
  • Timer-based operations (sleep)

§Usage

// Async function with one argument
define_async_function!(
    Sleep,                          // Struct name
    "sleep",                        // Function name
    (duration: Duration) -> None,   // Signature
    sleep_impl                      // Async implementation function
);

// Async function with two arguments
define_async_function!(
    HttpGet,
    "http::get",
    (url: String, ?opts: Object) -> Any,
    http_get_impl
);