From e4769295975757ad18507a763ee0e674c7141125 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 27 Jan 2022 23:55:32 +0800 Subject: [PATCH] Remove Box on callback traits. --- src/api/events.rs | 3 +-- src/engine.rs | 10 +++---- src/eval/debugger.rs | 15 ++++------- src/eval/global_state.rs | 2 +- src/func/callable_function.rs | 13 +++------ src/func/native.rs | 26 +++++++++--------- src/module/mod.rs | 50 ++++++++++++++++++++++++++--------- 7 files changed, 66 insertions(+), 53 deletions(-) diff --git a/src/api/events.rs b/src/api/events.rs index 0f89a582..2a27310d 100644 --- a/src/api/events.rs +++ b/src/api/events.rs @@ -12,8 +12,7 @@ impl Engine { /// /// The callback function signature takes the following form: /// - /// > `Fn(name: &str, index: usize, context: &EvalContext)` - /// > ` -> Result, Box> + 'static` + /// > `Fn(name: &str, index: usize, context: &EvalContext) -> Result, Box>` /// /// where: /// * `index`: an offset from the bottom of the current [`Scope`][crate::Scope] that the diff --git a/src/engine.rs b/src/engine.rs index 3ff2c44e..a60e59f4 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -115,17 +115,17 @@ pub struct Engine { /// Custom syntax. pub(crate) custom_syntax: BTreeMap>, /// Callback closure for resolving variable access. - pub(crate) resolve_var: Option, + pub(crate) resolve_var: Option>, /// Callback closure to remap tokens during parsing. pub(crate) token_mapper: Option>, /// Callback closure for implementing the `print` command. - pub(crate) print: Option, + pub(crate) print: Option>, /// Callback closure for implementing the `debug` command. - pub(crate) debug: Option, + pub(crate) debug: Option>, /// Callback closure for progress reporting. #[cfg(not(feature = "unchecked"))] - pub(crate) progress: Option, + pub(crate) progress: Option>, /// Optimize the [`AST`][crate::AST] after compilation. #[cfg(not(feature = "no_optimize"))] @@ -140,7 +140,7 @@ pub struct Engine { /// Callback closure for debugging. #[cfg(feature = "debugging")] - pub(crate) debugger: Option, + pub(crate) debugger: Option>, } impl fmt::Debug for Engine { diff --git a/src/eval/debugger.rs b/src/eval/debugger.rs index 13128000..75215ecb 100644 --- a/src/eval/debugger.rs +++ b/src/eval/debugger.rs @@ -10,18 +10,13 @@ use std::prelude::v1::*; /// A standard callback function for debugging. #[cfg(not(feature = "sync"))] -pub type OnDebuggerCallback = Box< - dyn Fn(&mut EvalContext, ASTNode, Option<&str>, Position) -> RhaiResultOf - + 'static, ->; +pub type OnDebuggerCallback = + dyn Fn(&mut EvalContext, ASTNode, Option<&str>, Position) -> RhaiResultOf; /// A standard callback function for debugging. #[cfg(feature = "sync")] -pub type OnDebuggerCallback = Box< - dyn Fn(&mut EvalContext, ASTNode, Option<&str>, Position) -> RhaiResultOf - + Send - + Sync - + 'static, ->; +pub type OnDebuggerCallback = dyn Fn(&mut EvalContext, ASTNode, Option<&str>, Position) -> RhaiResultOf + + Send + + Sync; /// A command for the debugger on the next iteration. #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] diff --git a/src/eval/global_state.rs b/src/eval/global_state.rs index 2c06b88f..8e67096c 100644 --- a/src/eval/global_state.rs +++ b/src/eval/global_state.rs @@ -178,7 +178,7 @@ impl GlobalRuntimeState<'_> { /// [modules][Module]. #[inline] #[must_use] - pub fn get_iter(&self, id: TypeId) -> Option { + pub fn get_iter(&self, id: TypeId) -> Option<&IteratorFn> { self.modules .iter() .rev() diff --git a/src/func/callable_function.rs b/src/func/callable_function.rs index 0402ba87..3febb448 100644 --- a/src/func/callable_function.rs +++ b/src/func/callable_function.rs @@ -17,7 +17,7 @@ pub enum CallableFunction { /// and the rest passed by value. Method(Shared), /// An iterator function. - Iterator(IteratorFn), + Iterator(Shared), /// A plugin function, Plugin(Shared), /// A script-defined function. @@ -177,9 +177,9 @@ impl CallableFunction { /// Get a reference to an iterator function. #[inline] #[must_use] - pub fn get_iter_fn(&self) -> Option { + pub fn get_iter_fn(&self) -> Option<&IteratorFn> { match self { - Self::Iterator(f) => Some(*f), + Self::Iterator(f) => Some(f.as_ref()), Self::Pure(_) | Self::Method(_) | Self::Plugin(_) => None, #[cfg(not(feature = "no_function"))] @@ -218,13 +218,6 @@ impl CallableFunction { } } -impl From for CallableFunction { - #[inline(always)] - fn from(func: IteratorFn) -> Self { - Self::Iterator(func) - } -} - #[cfg(not(feature = "no_function"))] impl From for CallableFunction { #[inline(always)] diff --git a/src/func/native.rs b/src/func/native.rs index 3f83c742..29ac8a0a 100644 --- a/src/func/native.rs +++ b/src/func/native.rs @@ -376,7 +376,11 @@ pub type FnAny = dyn Fn(NativeCallContext, &mut FnCallArgs) -> RhaiResult + Send pub type FnBuiltin = fn(NativeCallContext, &mut FnCallArgs) -> RhaiResult; /// A standard function that gets an iterator from a type. -pub type IteratorFn = fn(Dynamic) -> Box>; +#[cfg(not(feature = "sync"))] +pub type IteratorFn = dyn Fn(Dynamic) -> Box>; +/// A standard function that gets an iterator from a type. +#[cfg(feature = "sync")] +pub type IteratorFn = dyn Fn(Dynamic) -> Box> + Send + Sync; #[cfg(not(feature = "sync"))] pub type FnPlugin = dyn PluginFunction; @@ -386,39 +390,37 @@ pub type FnPlugin = dyn PluginFunction + Send + Sync; /// A standard callback function for progress reporting. #[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "sync"))] -pub type OnProgressCallback = Box Option + 'static>; +pub type OnProgressCallback = dyn Fn(u64) -> Option; /// A standard callback function for progress reporting. #[cfg(not(feature = "unchecked"))] #[cfg(feature = "sync")] -pub type OnProgressCallback = Box Option + Send + Sync + 'static>; +pub type OnProgressCallback = dyn Fn(u64) -> Option + Send + Sync; /// A standard callback function for printing. #[cfg(not(feature = "sync"))] -pub type OnPrintCallback = Box; +pub type OnPrintCallback = dyn Fn(&str); /// A standard callback function for printing. #[cfg(feature = "sync")] -pub type OnPrintCallback = Box; +pub type OnPrintCallback = dyn Fn(&str) + Send + Sync; /// A standard callback function for debugging. #[cfg(not(feature = "sync"))] -pub type OnDebugCallback = Box, Position) + 'static>; +pub type OnDebugCallback = dyn Fn(&str, Option<&str>, Position); /// A standard callback function for debugging. #[cfg(feature = "sync")] -pub type OnDebugCallback = Box, Position) + Send + Sync + 'static>; +pub type OnDebugCallback = dyn Fn(&str, Option<&str>, Position) + Send + Sync; /// A standard callback function for mapping tokens during parsing. #[cfg(not(feature = "sync"))] pub type OnParseTokenCallback = dyn Fn(Token, Position, &TokenizeState) -> Token; /// A standard callback function for mapping tokens during parsing. #[cfg(feature = "sync")] -pub type OnParseTokenCallback = - dyn Fn(Token, Position, &TokenizeState) -> Token + Send + Sync + 'static; +pub type OnParseTokenCallback = dyn Fn(Token, Position, &TokenizeState) -> Token + Send + Sync; /// A standard callback function for variable access. #[cfg(not(feature = "sync"))] -pub type OnVarCallback = - Box RhaiResultOf> + 'static>; +pub type OnVarCallback = dyn Fn(&str, usize, &EvalContext) -> RhaiResultOf>; /// A standard callback function for variable access. #[cfg(feature = "sync")] pub type OnVarCallback = - Box RhaiResultOf> + Send + Sync + 'static>; + dyn Fn(&str, usize, &EvalContext) -> RhaiResultOf> + Send + Sync; diff --git a/src/module/mod.rs b/src/module/mod.rs index 155b89b9..1cca5dfa 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -244,9 +244,9 @@ pub struct Module { /// including those in sub-modules. all_functions: BTreeMap>, /// Iterator functions, keyed by the type producing the iterator. - type_iterators: BTreeMap, + type_iterators: BTreeMap>, /// Flattened collection of iterator functions, including those in sub-modules. - all_type_iterators: BTreeMap, + all_type_iterators: BTreeMap>, /// Is the [`Module`] indexed? indexed: bool, /// Does the [`Module`] contain indexed functions that have been exposed to the global namespace? @@ -1432,8 +1432,8 @@ impl Module { other.functions.iter().for_each(|(&k, v)| { self.functions.entry(k).or_insert_with(|| v.clone()); }); - other.type_iterators.iter().for_each(|(&k, &v)| { - self.type_iterators.entry(k).or_insert(v); + other.type_iterators.iter().for_each(|(&k, v)| { + self.type_iterators.entry(k).or_insert(v.clone()); }); self.all_functions.clear(); self.all_variables.clear(); @@ -1483,7 +1483,8 @@ impl Module { .map(|(&k, v)| (k, v.clone())), ); - self.type_iterators.extend(other.type_iterators.iter()); + self.type_iterators + .extend(other.type_iterators.iter().map(|(&k, v)| (k, v.clone()))); self.all_functions.clear(); self.all_variables.clear(); self.all_type_iterators.clear(); @@ -1768,7 +1769,7 @@ impl Module { path: &mut Vec<&'a str>, variables: &mut BTreeMap, functions: &mut BTreeMap>, - type_iterators: &mut BTreeMap, + type_iterators: &mut BTreeMap>, ) -> bool { let mut contains_indexed_global_functions = false; @@ -1789,7 +1790,7 @@ impl Module { // Index type iterators module.type_iterators.iter().for_each(|(&type_id, func)| { - type_iterators.insert(type_id, *func); + type_iterators.insert(type_id, func.clone()); contains_indexed_global_functions = true; }); @@ -1868,10 +1869,33 @@ impl Module { } /// Set a type iterator into the [`Module`]. + #[cfg(not(feature = "sync"))] #[inline] - pub fn set_iter(&mut self, type_id: TypeId, func: IteratorFn) -> &mut Self { + pub fn set_iter( + &mut self, + type_id: TypeId, + func: impl Fn(Dynamic) -> Box> + 'static, + ) -> &mut Self { + let func = Shared::new(func); if self.indexed { - self.all_type_iterators.insert(type_id, func); + self.all_type_iterators.insert(type_id, func.clone()); + self.contains_indexed_global_functions = true; + } + self.type_iterators.insert(type_id, func); + self + } + + /// Set a type iterator into the [`Module`]. + #[cfg(feature = "sync")] + #[inline] + pub fn set_iter( + &mut self, + type_id: TypeId, + func: impl Fn(Dynamic) -> Box> + SendSync + 'static, + ) -> &mut Self { + let func = Shared::new(func); + if self.indexed { + self.all_type_iterators.insert(type_id, func.clone()); self.contains_indexed_global_functions = true; } self.type_iterators.insert(type_id, func); @@ -1905,15 +1929,15 @@ impl Module { /// Get the specified type iterator. #[inline] #[must_use] - pub(crate) fn get_qualified_iter(&self, id: TypeId) -> Option { - self.all_type_iterators.get(&id).cloned() + pub(crate) fn get_qualified_iter(&self, id: TypeId) -> Option<&IteratorFn> { + self.all_type_iterators.get(&id).map(|f| f.as_ref()) } /// Get the specified type iterator. #[inline] #[must_use] - pub(crate) fn get_iter(&self, id: TypeId) -> Option { - self.type_iterators.get(&id).cloned() + pub(crate) fn get_iter(&self, id: TypeId) -> Option<&IteratorFn> { + self.type_iterators.get(&id).map(|f| f.as_ref()) } }