Enable capacity on packages.
This commit is contained in:
@@ -53,6 +53,14 @@ pub trait Package {
|
||||
#[allow(unused_variables)]
|
||||
fn init_engine(engine: &mut Engine) {}
|
||||
|
||||
/// Number of functions expected by the package.
|
||||
///
|
||||
/// This capacity only acts as a hint. It may not be precise.
|
||||
#[allow(unused_variables)]
|
||||
fn capacity() -> usize {
|
||||
16
|
||||
}
|
||||
|
||||
/// Register the package with an [`Engine`].
|
||||
///
|
||||
/// # Example
|
||||
@@ -112,15 +120,17 @@ pub trait Package {
|
||||
///
|
||||
/// def_package! {
|
||||
/// /// My super-duper package.
|
||||
/// pub MyPackage(module) {
|
||||
/// pub MyPackage(module @ 10) {
|
||||
/// // Load a native Rust function.
|
||||
/// module.set_native_fn("my_add", add);
|
||||
/// } |> |engine| {
|
||||
/// engine.set_custom_operator("@", 160);
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! def_package {
|
||||
($($(#[$outer:meta])* $mod:vis $package:ident($lib:ident)
|
||||
($($(#[$outer:meta])* $mod:vis $package:ident($lib:ident $(@ $capacity:expr)?)
|
||||
$( : $($(#[$base_meta:meta])* $base_pkg:ty),+ )?
|
||||
$block:block
|
||||
$( |> | $engine:ident | $init_engine:block )?
|
||||
@@ -136,7 +146,7 @@ macro_rules! def_package {
|
||||
#[inline]
|
||||
fn init($lib: &mut $crate::Module) {
|
||||
$($(
|
||||
$(#[$base_meta])* { <$base_pkg>::init($lib); }
|
||||
$(#[$base_meta])* <$base_pkg>::init($lib);
|
||||
)*)*
|
||||
|
||||
$block
|
||||
@@ -144,7 +154,7 @@ macro_rules! def_package {
|
||||
#[inline]
|
||||
fn init_engine(_engine: &mut $crate::Engine) {
|
||||
$($(
|
||||
$(#[$base_meta])* { <$base_pkg>::init_engine(_engine); }
|
||||
$(#[$base_meta])* <$base_pkg>::init_engine(_engine);
|
||||
)*)*
|
||||
|
||||
$(
|
||||
@@ -152,6 +162,15 @@ macro_rules! def_package {
|
||||
$init_engine
|
||||
)*
|
||||
}
|
||||
#[inline(always)]
|
||||
fn capacity() -> usize {
|
||||
let mut _capacity = 16;
|
||||
$(_capacity = $capacity;)?
|
||||
$($(
|
||||
$(#[$base_meta])* { _capacity += <$base_pkg>::capacity(); }
|
||||
)*)*
|
||||
_capacity
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for $package {
|
||||
@@ -166,7 +185,7 @@ macro_rules! def_package {
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn new() -> Self {
|
||||
let mut module = $crate::Module::new();
|
||||
let mut module = $crate::Module::with_capacity(<Self as $crate::packages::Package>::capacity());
|
||||
<Self as $crate::packages::Package>::init(&mut module);
|
||||
module.build_index();
|
||||
Self(module.into())
|
||||
|
Reference in New Issue
Block a user