diff --git a/src/engine.rs b/src/engine.rs index 29d3e103..93b667a1 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -558,7 +558,7 @@ pub struct FnResolutionCacheEntry { } /// A function resolution cache. -pub type FnResolutionCache = BTreeMap>; +pub type FnResolutionCache = BTreeMap>>; /// _(INTERNALS)_ A type that holds all the current states of the [`Engine`]. /// Exported under the `internals` feature only. @@ -776,7 +776,7 @@ pub struct Engine { pub(crate) module_resolver: Box, /// A map mapping type names to pretty-print names. - pub(crate) type_names: BTreeMap, + pub(crate) type_names: BTreeMap>, /// An empty [`ImmutableString`] for cloning purposes. pub(crate) empty_string: ImmutableString, @@ -786,7 +786,7 @@ pub struct Engine { /// A map containing custom keywords and precedence to recognize. pub(crate) custom_keywords: BTreeMap>, /// Custom syntax. - pub(crate) custom_syntax: BTreeMap, + pub(crate) custom_syntax: BTreeMap>, /// Callback closure for resolving variable access. pub(crate) resolve_var: Option, diff --git a/src/engine_api.rs b/src/engine_api.rs index 1722dd76..006103f6 100644 --- a/src/engine_api.rs +++ b/src/engine_api.rs @@ -265,7 +265,8 @@ impl Engine { #[inline(always)] pub fn register_type_with_name(&mut self, name: &str) -> &mut Self { // Add the pretty-print type name into the map - self.type_names.insert(type_name::().into(), name.into()); + self.type_names + .insert(type_name::().into(), Box::new(name.into())); self } /// Register an type iterator for an iterable type with the [`Engine`]. diff --git a/src/fn_call.rs b/src/fn_call.rs index fff4dfb6..e99a4a20 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -161,7 +161,7 @@ impl Engine { args: Option<&mut FnCallArgs>, allow_dynamic: bool, is_op_assignment: bool, - ) -> &'s Option { + ) -> &'s Option> { let mut hash = if let Some(ref args) = args { let hash_params = calc_fn_params_hash(args.iter().map(|a| a.type_id())); combine_hashes(hash_script, hash_params) @@ -222,7 +222,7 @@ impl Engine { match func { // Specific version found - Some(f) => return Some(f), + Some(f) => return Some(Box::new(f)), // Stop when all permutations are exhausted None if bitmask >= max_bitmask => { @@ -250,6 +250,7 @@ impl Engine { }, ) } + .map(Box::new) }); } @@ -311,7 +312,8 @@ impl Engine { is_op_assignment, ); - if let Some(FnResolutionCacheEntry { func, source }) = func { + if let Some(f) = func { + let FnResolutionCacheEntry { func, source } = f.as_ref(); assert!(func.is_native()); // Calling pure function but the first argument is a reference? @@ -717,10 +719,11 @@ impl Engine { }; #[cfg(not(feature = "no_function"))] - if let Some(FnResolutionCacheEntry { func, source }) = hash_script.and_then(|hash| { + if let Some(f) = hash_script.and_then(|hash| { self.resolve_function(mods, state, lib, fn_name, hash, None, false, false) .clone() }) { + let FnResolutionCacheEntry { func, source } = *f; // Script function call assert!(func.is_script()); diff --git a/src/module/mod.rs b/src/module/mod.rs index 5f2868f8..278731fc 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -48,7 +48,7 @@ impl Default for FnNamespace { #[derive(Debug, Clone)] pub struct FuncInfo { /// Function instance. - pub func: CallableFunction, + pub func: Shared, /// Function namespace. pub namespace: FnNamespace, /// Function access mode. @@ -140,7 +140,7 @@ pub struct Module { functions: BTreeMap>, /// Flattened collection of all external Rust functions, native or scripted. /// including those in sub-modules. - all_functions: BTreeMap, + all_functions: BTreeMap>, /// Iterator functions, keyed by the type producing the iterator. type_iterators: BTreeMap, /// Flattened collection of iterator functions, including those in sub-modules. @@ -497,7 +497,7 @@ impl Module { param_types: Default::default(), #[cfg(feature = "metadata")] param_names, - func: fn_def.into(), + func: Into::::into(fn_def).into(), }), ); self.indexed = false; @@ -721,7 +721,7 @@ impl Module { param_types, #[cfg(feature = "metadata")] param_names, - func, + func: func.into(), }), ); @@ -1119,7 +1119,7 @@ impl Module { /// The [`u64`] hash is returned by the [`set_native_fn`][Module::set_native_fn] call. #[inline(always)] pub(crate) fn get_fn(&self, hash_fn: u64) -> Option<&CallableFunction> { - self.functions.get(&hash_fn).map(|f| &f.func) + self.functions.get(&hash_fn).map(|f| f.func.as_ref()) } /// Does the particular namespace-qualified function exist in the [`Module`]? @@ -1135,7 +1135,9 @@ impl Module { /// The [`u64`] hash is calculated by [`build_index`][Module::build_index]. #[inline(always)] pub(crate) fn get_qualified_fn(&self, hash_qualified_fn: u64) -> Option<&CallableFunction> { - self.all_functions.get(&hash_qualified_fn) + self.all_functions + .get(&hash_qualified_fn) + .map(|f| f.as_ref()) } /// Combine another [`Module`] into this [`Module`]. @@ -1486,7 +1488,7 @@ impl Module { module: &'a Module, path: &mut Vec<&'a str>, variables: &mut BTreeMap, - functions: &mut BTreeMap, + functions: &mut BTreeMap>, type_iterators: &mut BTreeMap, ) -> bool { let mut contains_indexed_global_functions = false; diff --git a/src/syntax.rs b/src/syntax.rs index 7d951cea..309d1c74 100644 --- a/src/syntax.rs +++ b/src/syntax.rs @@ -230,13 +230,14 @@ impl Engine { new_vars: isize, func: impl Fn(&mut EvalContext, &[Expression]) -> RhaiResult + SendSync + 'static, ) -> &mut Self { - let syntax = CustomSyntax { - parse: Box::new(parse), - func: (Box::new(func) as Box).into(), - scope_delta: new_vars, - }; - - self.custom_syntax.insert(key.into(), syntax); + self.custom_syntax.insert( + key.into(), + Box::new(CustomSyntax { + parse: Box::new(parse), + func: (Box::new(func) as Box).into(), + scope_delta: new_vars, + }), + ); self } }