Allocate packages with higher functions capacity.

This commit is contained in:
Stephen Chung
2020-05-13 19:39:34 +08:00
parent 30e5e2f034
commit 5a02548ebc
6 changed files with 24 additions and 10 deletions

View File

@@ -94,6 +94,24 @@ impl Module {
Default::default()
}
/// Create a new module with a specified capacity for native Rust functions.
///
/// # Examples
///
/// ```
/// use rhai::Module;
///
/// let mut module = Module::new();
/// module.set_var("answer", 42_i64);
/// assert_eq!(module.get_var_value::<i64>("answer").unwrap(), 42);
/// ```
pub fn new_with_capacity(capacity: usize) -> Self {
Self {
functions: HashMap::with_capacity(capacity),
..Default::default()
}
}
/// Does a variable exist in the module?
///
/// # Examples

View File

@@ -17,7 +17,7 @@ fn ins<T: Variant + Clone>(list: &mut Array, position: INT, item: T) -> FuncRetu
if position <= 0 {
list.insert(0, Dynamic::from(item));
} else if (position as usize) >= list.len() - 1 {
push(list, item);
push(list, item)?;
} else {
list.insert(position as usize, Dynamic::from(item));
}
@@ -26,7 +26,7 @@ fn ins<T: Variant + Clone>(list: &mut Array, position: INT, item: T) -> FuncRetu
fn pad<T: Variant + Clone>(list: &mut Array, len: INT, item: T) -> FuncReturn<()> {
if len >= 0 {
while list.len() < len as usize {
push(list, item.clone());
push(list, item.clone())?;
}
}
Ok(())

View File

@@ -129,7 +129,7 @@ macro_rules! def_package {
impl $package {
pub fn new() -> Self {
let mut module = $root::Module::new();
let mut module = $root::Module::new_with_capacity(512);
<Self as $root::packages::Package>::init(&mut module);
Self(module.into())
}