Add combine_with_exported_module.

This commit is contained in:
Stephen Chung
2020-09-13 22:12:11 +08:00
committed by J Henry Waugh
parent aed70db303
commit 848bdf3f01
16 changed files with 64 additions and 17 deletions

View File

@@ -24,6 +24,9 @@ use rhai::plugins::*; // a "prelude" import for macros
#[export_module]
mod my_module {
// This constant will be registered as a the constant variable 'SOME_NUMBER'.
pub const SOME_NUMBER: i64 = 42;
// This function will be registered as 'greet'.
pub fn greet(name: &str) -> String {
format!("hello, {}!", name)

View File

@@ -63,8 +63,8 @@ By far the easiest way to create a custom module is to call `Module::merge_flatt
In fact, this exactly is how Rhai's built-in packages, such as `BasicMathPackage`, are implemented.
`rhai::plugins::exported_module!` generates a module from the [plugins][plugin module] definition,
and `Module::merge_flatten` consumes its, adding all its registered functions into the package itself.
`rhai::plugins::combine_with_exported_module!` adds all functions and constants from the
[plugins][plugin module] definition into the package itself.
```rust
// Import necessary types and traits.
@@ -94,7 +94,10 @@ def_package!(rhai:MyPackage:"My own personal super package", module, {
BasicArrayPackage::init(module);
BasicMapPackage::init(module);
// Merge the plugin module into the custom package.
module.merge_flatten(exported_module!(my_module));
// Merge all registered functions and constants from the plugin module into the custom package.
//
// The text string name in the middle parameter can be anything and is reserved for future use;
// it is recommended to be an ID string that uniquely identifies the module.
combine_with_exported_module!(module, "my-functions", my_module));
});
```