Revert "Enable capacity on packages."

This reverts commit 85ca6ec4a1.
This commit is contained in:
Stephen Chung 2022-09-14 15:17:36 +08:00
parent 72244b74cd
commit 640471f865
14 changed files with 16 additions and 38 deletions

View File

@ -12,11 +12,6 @@ New features
* [`Engine::register_custom_syntax_with_state_raw`] is added. The custom syntax parser and implementation functions take on an additional parameter that holds a user-defined custom _state_ which should substantially simplify writing some custom parsers. * [`Engine::register_custom_syntax_with_state_raw`] is added. The custom syntax parser and implementation functions take on an additional parameter that holds a user-defined custom _state_ which should substantially simplify writing some custom parsers.
* [`Engine::register_custom_syntax_raw`] is deprecated. * [`Engine::register_custom_syntax_raw`] is deprecated.
### Initial capacity for packages
* For packages that register a large number of functions, a syntax extension for `def_package!` allows the specification of the initial _capacity_ of the package to reduce reallocation costs.
* A `capacity` method is added to the `Package` trait.
Version 1.10.0 Version 1.10.0
============== ==============

View File

@ -191,7 +191,7 @@ macro_rules! reg_functions {
def_package! { def_package! {
/// Basic arithmetic package. /// Basic arithmetic package.
pub ArithmeticPackage(lib @ 128) { pub ArithmeticPackage(lib) {
lib.standard = true; lib.standard = true;
combine_with_exported_module!(lib, "int", int_functions); combine_with_exported_module!(lib, "int", int_functions);

View File

@ -13,7 +13,7 @@ use std::{any::TypeId, cmp::Ordering, mem};
def_package! { def_package! {
/// Package of basic array utilities. /// Package of basic array utilities.
pub BasicArrayPackage(lib @ 64) { pub BasicArrayPackage(lib) {
lib.standard = true; lib.standard = true;
combine_with_exported_module!(lib, "array", array_functions); combine_with_exported_module!(lib, "array", array_functions);

View File

@ -9,7 +9,7 @@ use std::prelude::v1::*;
def_package! { def_package! {
/// Package of basic bit-field utilities. /// Package of basic bit-field utilities.
pub BitFieldPackage(lib @ 16) { pub BitFieldPackage(lib) {
lib.standard = true; lib.standard = true;
combine_with_exported_module!(lib, "bit_field", bit_field_functions); combine_with_exported_module!(lib, "bit_field", bit_field_functions);

View File

@ -15,7 +15,7 @@ use crate::{FLOAT, FLOAT_BYTES};
def_package! { def_package! {
/// Package of basic BLOB utilities. /// Package of basic BLOB utilities.
pub BasicBlobPackage(lib @ 64) { pub BasicBlobPackage(lib) {
lib.standard = true; lib.standard = true;
combine_with_exported_module!(lib, "blob", blob_functions); combine_with_exported_module!(lib, "blob", blob_functions);

View File

@ -326,7 +326,7 @@ macro_rules! reg_range {
def_package! { def_package! {
/// Package of basic range iterators /// Package of basic range iterators
pub BasicIteratorPackage(lib @ 64) { pub BasicIteratorPackage(lib) {
lib.standard = true; lib.standard = true;
reg_range!(lib | "range" => INT); reg_range!(lib | "range" => INT);

View File

@ -37,7 +37,7 @@ macro_rules! reg_functions {
def_package! { def_package! {
/// Package of basic logic operators. /// Package of basic logic operators.
pub LogicPackage(lib @ 128) { pub LogicPackage(lib) {
lib.standard = true; lib.standard = true;
#[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i32"))]

View File

@ -53,7 +53,7 @@ macro_rules! reg_functions {
def_package! { def_package! {
/// Basic mathematical package. /// Basic mathematical package.
pub BasicMathPackage(lib @ 128) { pub BasicMathPackage(lib) {
lib.standard = true; lib.standard = true;
// Integer functions // Integer functions

View File

@ -53,14 +53,6 @@ pub trait Package {
#[allow(unused_variables)] #[allow(unused_variables)]
fn init_engine(engine: &mut Engine) {} 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`]. /// Register the package with an [`Engine`].
/// ///
/// # Example /// # Example
@ -128,7 +120,7 @@ pub trait Package {
/// ``` /// ```
#[macro_export] #[macro_export]
macro_rules! def_package { macro_rules! def_package {
($($(#[$outer:meta])* $mod:vis $package:ident($lib:ident $(@ $capacity:expr)?) ($($(#[$outer:meta])* $mod:vis $package:ident($lib:ident)
$( : $($(#[$base_meta:meta])* $base_pkg:ty),+ )? $( : $($(#[$base_meta:meta])* $base_pkg:ty),+ )?
$block:block $block:block
$( |> | $engine:ident | $init_engine:block )? $( |> | $engine:ident | $init_engine:block )?
@ -144,7 +136,7 @@ macro_rules! def_package {
#[inline] #[inline]
fn init($lib: &mut $crate::Module) { fn init($lib: &mut $crate::Module) {
$($( $($(
$(#[$base_meta])* <$base_pkg>::init($lib); $(#[$base_meta])* { <$base_pkg>::init($lib); }
)*)* )*)*
$block $block
@ -152,7 +144,7 @@ macro_rules! def_package {
#[inline] #[inline]
fn init_engine(_engine: &mut $crate::Engine) { fn init_engine(_engine: &mut $crate::Engine) {
$($( $($(
$(#[$base_meta])* <$base_pkg>::init_engine(_engine); $(#[$base_meta])* { <$base_pkg>::init_engine(_engine); }
)*)* )*)*
$( $(
@ -160,15 +152,6 @@ macro_rules! def_package {
$init_engine $init_engine
)* )*
} }
#[inline(always)]
fn capacity() -> usize {
let mut _capacity = 16;
$(_capacity = $capacity;)?
$($(
$(#[$base_meta])* { _capacity += <$base_pkg>::capacity(); }
)*)*
_capacity
}
} }
impl Default for $package { impl Default for $package {
@ -183,7 +166,7 @@ macro_rules! def_package {
#[inline] #[inline]
#[must_use] #[must_use]
pub fn new() -> Self { pub fn new() -> Self {
let mut module = $crate::Module::with_capacity(<Self as $crate::packages::Package>::capacity()); let mut module = $crate::Module::new();
<Self as $crate::packages::Package>::init(&mut module); <Self as $crate::packages::Package>::init(&mut module);
module.build_index(); module.build_index();
Self(module.into()) Self(module.into())

View File

@ -15,7 +15,7 @@ def_package! {
/// * [`BasicIteratorPackage`][super::BasicIteratorPackage] /// * [`BasicIteratorPackage`][super::BasicIteratorPackage]
/// * [`BasicFnPackage`][super::BasicFnPackage] /// * [`BasicFnPackage`][super::BasicFnPackage]
/// * [`DebuggingPackage`][super::DebuggingPackage] /// * [`DebuggingPackage`][super::DebuggingPackage]
pub CorePackage(lib @ 0) : pub CorePackage(lib) :
LanguageCorePackage, LanguageCorePackage,
ArithmeticPackage, ArithmeticPackage,
BasicStringPackage, BasicStringPackage,

View File

@ -18,7 +18,7 @@ def_package! {
/// * [`BasicMapPackage`][super::BasicMapPackage] /// * [`BasicMapPackage`][super::BasicMapPackage]
/// * [`BasicTimePackage`][super::BasicTimePackage] /// * [`BasicTimePackage`][super::BasicTimePackage]
/// * [`MoreStringPackage`][super::MoreStringPackage] /// * [`MoreStringPackage`][super::MoreStringPackage]
pub StandardPackage(lib @ 0) : pub StandardPackage(lib) :
CorePackage, CorePackage,
BitFieldPackage, BitFieldPackage,
LogicPackage, LogicPackage,

View File

@ -16,7 +16,7 @@ pub const FUNC_TO_DEBUG: &str = "to_debug";
def_package! { def_package! {
/// Package of basic string utilities (e.g. printing) /// Package of basic string utilities (e.g. printing)
pub BasicStringPackage(lib @ 128) { pub BasicStringPackage(lib) {
lib.standard = true; lib.standard = true;
combine_with_exported_module!(lib, "print_debug", print_debug_functions); combine_with_exported_module!(lib, "print_debug", print_debug_functions);

View File

@ -11,7 +11,7 @@ use super::string_basic::{print_with_func, FUNC_TO_STRING};
def_package! { def_package! {
/// Package of additional string utilities over [`BasicStringPackage`][super::BasicStringPackage] /// Package of additional string utilities over [`BasicStringPackage`][super::BasicStringPackage]
pub MoreStringPackage(lib @ 64) { pub MoreStringPackage(lib) {
lib.standard = true; lib.standard = true;
combine_with_exported_module!(lib, "string", string_functions); combine_with_exported_module!(lib, "string", string_functions);

View File

@ -7,7 +7,7 @@ use rhai::{def_package, Engine, EvalAltResult, Module, Scope, INT};
fn test_packages() -> Result<(), Box<EvalAltResult>> { fn test_packages() -> Result<(), Box<EvalAltResult>> {
def_package! { def_package! {
/// My custom package. /// My custom package.
MyPackage(m @ 32) : SSS { MyPackage(m) : SSS {
m.set_native_fn("hello", |x: INT| Ok(x + 1)); m.set_native_fn("hello", |x: INT| Ok(x + 1));
m.set_native_fn("@", |x: INT, y: INT| Ok(x * x + y * y)); m.set_native_fn("@", |x: INT, y: INT| Ok(x * x + y * y));
} |> |engine| { } |> |engine| {