Revise package terminology.
This commit is contained in:
@@ -23,10 +23,10 @@ Built-In Packages
|
||||
| `StandardPackage` | standard library (default for `Engine::new`) | no | yes |
|
||||
|
||||
|
||||
Load the `CorePackage`
|
||||
---------------------
|
||||
`CorePackage`
|
||||
-------------
|
||||
|
||||
If only minimal functionalities is required, load the `CorePackage` instead:
|
||||
If only minimal functionalities are required, register the `CorePackage` instead:
|
||||
|
||||
```rust
|
||||
use rhai::Engine;
|
||||
@@ -35,6 +35,6 @@ use rhai::packages::{Package, CorePackage};
|
||||
let mut engine = Engine::new_raw();
|
||||
let package = CorePackage::new();
|
||||
|
||||
// Register the package into the Engine by converting it into a shared module.
|
||||
// Register the package into the 'Engine' by converting it into a shared module.
|
||||
engine.register_global_module(package.as_shared_module());
|
||||
```
|
||||
|
@@ -3,33 +3,31 @@ Create a Custom Package
|
||||
|
||||
{{#include ../../links.md}}
|
||||
|
||||
Sometimes specific functionalities are needed, so custom packages can be created.
|
||||
|
||||
A custom package is a convenient means to gather up a number of functions for later use.
|
||||
An [`Engine`] only needs to `Engine::register_global_module` the custom package once to gain access
|
||||
to the entire set of functions within.
|
||||
The macro `def_package!` can be used to create a custom [package].
|
||||
|
||||
Registering a package into an [`Engine`] is functionally equivalent to calling `Engine::register_fn` etc.
|
||||
on _each_ of the functions inside the package. But because packages are _shared_, using a package is
|
||||
_much_ cheaper than registering all the functions one by one.
|
||||
|
||||
The macro `rhai::def_package!` can be used to create a new custom package.
|
||||
A custom package can aggregate many other packages into a single self-contained unit.
|
||||
More functions can be added on top of others.
|
||||
|
||||
|
||||
Macro Parameters
|
||||
---------------
|
||||
`def_package!`
|
||||
--------------
|
||||
|
||||
`def_package!(root:package_name:description, variable, block)`
|
||||
> `def_package!(root:package_name:description, variable, block)`
|
||||
|
||||
* `root` - root namespace, usually `"rhai"`.
|
||||
where:
|
||||
|
||||
* `package_name` - name of the package, usually ending in `Package`.
|
||||
| Parameter | Description |
|
||||
| :------------: | ----------------------------------------------------------------------------------------------- |
|
||||
| `root` | root namespace, usually `rhai` |
|
||||
| `package_name` | name of the package, usually ending in `...Package` |
|
||||
| `description` | doc-comment for the package |
|
||||
| `variable` | a variable name holding a reference to the [module] (`&mut Module`) that is to form the package |
|
||||
| `block` | a code block that initializes the package |
|
||||
|
||||
* `description` - doc comment for the package.
|
||||
|
||||
* `variable` - a variable name holding a reference to the [module] that is to form the package.
|
||||
|
||||
* `block` - a code block that initializes the package.
|
||||
Examples
|
||||
--------
|
||||
|
||||
```rust
|
||||
// Import necessary types and traits.
|
||||
@@ -43,7 +41,7 @@ use rhai::{
|
||||
|
||||
// Define the package 'MyPackage'.
|
||||
def_package!(rhai:MyPackage:"My own personal super package", module, {
|
||||
// Aggregate existing packages simply by calling 'init' on each.
|
||||
// Aggregate other packages simply by calling 'init' on each.
|
||||
ArithmeticPackage::init(module);
|
||||
LogicPackage::init(module);
|
||||
BasicArrayPackage::init(module);
|
||||
@@ -64,14 +62,14 @@ def_package!(rhai:MyPackage:"My own personal super package", module, {
|
||||
Create a Custom Package from a Plugin Module
|
||||
-------------------------------------------
|
||||
|
||||
By far the easiest way to create a custom module is to call `rhai::plugin::combine_with_exported_module!`
|
||||
from within `rhai::def_package!` which simply merges in all the functions defined within a [plugin module].
|
||||
By far the easiest way to create a custom module is to call `plugin::combine_with_exported_module!`
|
||||
from within `def_package!` which simply merges in all the functions defined within a [plugin module].
|
||||
|
||||
In fact, this exactly is how Rhai's built-in packages, such as `BasicMathPackage`, are implemented.
|
||||
|
||||
Because of the specific requirements of a [package], all sub-modules are _flattened_
|
||||
(i.e. all functions defined within sub-modules are pulled up and registered at the top level instead)
|
||||
and so there will not be any sub-modules added to the package.
|
||||
Due to specific requirements of a [package], `plugin::combine_with_exported_module!`
|
||||
_flattens_ all sub-modules (i.e. all functions and [type iterators] defined within sub-modules
|
||||
are pulled up to the top level instead) and so there will not be any sub-modules added to the package.
|
||||
|
||||
Variables in the [plugin module] are ignored.
|
||||
|
||||
@@ -107,7 +105,7 @@ mod my_module {
|
||||
|
||||
// Define the package 'MyPackage'.
|
||||
def_package!(rhai:MyPackage:"My own personal super package", module, {
|
||||
// Aggregate existing packages simply by calling 'init' on each.
|
||||
// Aggregate other packages simply by calling 'init' on each.
|
||||
ArithmeticPackage::init(module);
|
||||
LogicPackage::init(module);
|
||||
BasicArrayPackage::init(module);
|
||||
|
@@ -3,40 +3,56 @@ Packages
|
||||
|
||||
{{#include ../../links.md}}
|
||||
|
||||
Standard built-in Rhai features are provided in various _packages_ that can be registered into the
|
||||
The built-in library of Rhai is provided as various _packages_ that can be
|
||||
turned into _shared_ [modules], which in turn can be registered into the
|
||||
_global namespace_ of an [`Engine`] via `Engine::register_global_module`.
|
||||
|
||||
Packages reside under `rhai::packages::*` and the trait `rhai::packages::Package` must be loaded in order for
|
||||
packages to be used.
|
||||
Packages reside under `rhai::packages::*` and the trait `rhai::packages::Package`
|
||||
must be loaded in order for packages to be used.
|
||||
|
||||
### Packages _are_ Modules
|
||||
|
||||
Internally, a _package_ is a _newtype_ wrapping a pre-defined [module],
|
||||
with some conveniences to make it easier to define and use as a standard
|
||||
_library_ for an [`Engine`].
|
||||
|
||||
Packages typically contain Rust functions that are callable within a Rhai script.
|
||||
All _top-level_ functions in a package are available under the _global namespace_
|
||||
(i.e. they're available without namespace qualifiers).
|
||||
|
||||
Once a package is created (e.g. via `Package::new`), it can be _shared_ (via `Package::as_shared_module`)
|
||||
among multiple instances of [`Engine`], even across threads (under [`sync`]).
|
||||
Therefore, a package only has to be created _once_.
|
||||
Sub-modules and variables are ignored in packages.
|
||||
|
||||
|
||||
Share a Package Among Multiple `Engine`'s
|
||||
----------------------------------------
|
||||
|
||||
`Engine::register_global_module` and `Engine::register_static_module` both require _shared_ [modules].
|
||||
|
||||
Once a package is created (e.g. via `Package::new`), it can create _shared_ [modules]
|
||||
(via `Package::as_shared_module`) and register them into multiple instances of [`Engine`],
|
||||
even across threads (under the [`sync`] feature).
|
||||
|
||||
Therefore, a package only has to be created _once_ and essentially shared among multiple
|
||||
[`Engine`] instances. This is particular useful when spawning large number of [raw `Engine`'s][raw `Engine`].
|
||||
|
||||
```rust
|
||||
use rhai::Engine;
|
||||
use rhai::packages::Package // load the 'Package' trait to use packages
|
||||
use rhai::packages::CorePackage; // the 'core' package contains basic functionalities (e.g. arithmetic)
|
||||
|
||||
// Create a 'raw' Engine
|
||||
let mut engine = Engine::new_raw();
|
||||
|
||||
// Create a package - can be shared among multiple `Engine` instances
|
||||
// Create a package - can be shared among multiple 'Engine' instances
|
||||
let package = CorePackage::new();
|
||||
|
||||
// Register the package into the global namespace.
|
||||
// 'Package::as_shared_module' converts the package into a shared module.
|
||||
engine.register_global_module(package.as_shared_module());
|
||||
let mut engines_collection: Vec<Engine> = Vec::new();
|
||||
|
||||
// Create 100 'raw' Engines
|
||||
for _ in 0..100 {
|
||||
let mut engine = Engine::new_raw();
|
||||
|
||||
// Register the package into the global namespace.
|
||||
// 'Package::as_shared_module' converts the package into a shared module.
|
||||
engine.register_global_module(package.as_shared_module());
|
||||
|
||||
engines_collection.push(engine);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Share a Package Among `Engine`s
|
||||
------------------------------
|
||||
|
||||
`Engine::register_global_module` consumes the input shared module.
|
||||
|
||||
However, `Package::as_shared_module` can be called multiple times for multiple instances of [`Engine`].
|
||||
|
Reference in New Issue
Block a user