Macro def_package
macro_rules! def_package {
($($(#[$outer:meta])* $mod:vis $package:ident($lib:ident)
$( : $($(#[$base_meta:meta])* $base_pkg:ty),+ )?
$block:block
$( |> | $engine:ident | $init_engine:block )?
)+) => { ... };
($($(#[$outer:meta])* $root:ident :: $package:ident => | $lib:ident | $block:block)+) => { ... };
($root:ident : $package:ident : $comment:expr , $lib:ident , $block:stmt) => { ... };
}
Expand description
Macro that makes it easy to define a package (which is basically a shared module) and register functions into it.
Functions can be added to the package using Module::set_native_fn
.
ยงExample
Define a package named MyPackage
with a single function named my_add
:
use rhai::{Dynamic, EvalAltResult};
use rhai::def_package;
fn add(x: i64, y: i64) -> Result<i64, Box<EvalAltResult>> { Ok(x + y) }
def_package! {
/// My super-duper package.
pub MyPackage(module) {
// Load a native Rust function.
module.set_native_fn("my_add", add);
}
}