diff --git a/src/api.rs b/src/api.rs index 874aaa4e..48052e85 100644 --- a/src/api.rs +++ b/src/api.rs @@ -4,9 +4,7 @@ use crate::any::{Dynamic, Variant}; use crate::engine::{make_getter, make_setter, Engine, State, FUNC_INDEXER}; use crate::error::ParseError; use crate::fn_call::FuncArgs; -use crate::fn_native::{ - IteratorCallback, ObjectGetCallback, ObjectIndexerCallback, ObjectSetCallback, -}; +use crate::fn_native::{IteratorFn, ObjectGetCallback, ObjectIndexerCallback, ObjectSetCallback}; use crate::fn_register::RegisterFn; use crate::optimize::{optimize_into_ast, OptimizationLevel}; use crate::parser::{parse, parse_global_expr, AST}; @@ -123,8 +121,8 @@ impl Engine { /// Register an iterator adapter for a type with the `Engine`. /// This is an advanced feature. - pub fn register_iterator(&mut self, f: F) { - self.global_module.set_iter(TypeId::of::(), Box::new(f)); + pub fn register_iterator(&mut self, f: IteratorFn) { + self.global_module.set_iter(TypeId::of::(), f); } /// Register a getter function for a member of a registered type with the `Engine`. diff --git a/src/fn_native.rs b/src/fn_native.rs index 619fb102..262bad7c 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -11,10 +11,7 @@ pub type FnAny = dyn Fn(&mut FnCallArgs) -> Result> #[cfg(not(feature = "sync"))] pub type FnAny = dyn Fn(&mut FnCallArgs) -> Result>; -#[cfg(feature = "sync")] -pub type IteratorFn = dyn Fn(Dynamic) -> Box> + Send + Sync; -#[cfg(not(feature = "sync"))] -pub type IteratorFn = dyn Fn(Dynamic) -> Box>; +pub type IteratorFn = fn(Dynamic) -> Box>; #[cfg(feature = "sync")] pub type PrintCallback = dyn Fn(&str) + Send + Sync + 'static; @@ -57,32 +54,11 @@ pub trait ObjectIndexerCallback: Fn(&mut T, X) -> U + 'static {} #[cfg(not(feature = "sync"))] impl U + 'static, T, X, U> ObjectIndexerCallback for F {} -#[cfg(feature = "sync")] -pub trait IteratorCallback: - Fn(Dynamic) -> Box> + Send + Sync + 'static -{ -} -#[cfg(feature = "sync")] -impl Box> + Send + Sync + 'static> IteratorCallback - for F -{ -} - -#[cfg(not(feature = "sync"))] -pub trait IteratorCallback: Fn(Dynamic) -> Box> + 'static {} -#[cfg(not(feature = "sync"))] -impl Box> + 'static> IteratorCallback for F {} - #[cfg(not(feature = "sync"))] pub type SharedNativeFunction = Rc; #[cfg(feature = "sync")] pub type SharedNativeFunction = Arc; -#[cfg(not(feature = "sync"))] -pub type SharedIteratorFn = Rc; -#[cfg(feature = "sync")] -pub type SharedIteratorFn = Arc; - #[cfg(feature = "sync")] pub type SharedFnDef = Arc; #[cfg(not(feature = "sync"))] @@ -97,7 +73,7 @@ pub enum CallableFunction { /// and the rest passed by value. Method(SharedNativeFunction), /// An iterator function. - Iterator(SharedIteratorFn), + Iterator(IteratorFn), /// A script-defined function. Script(SharedFnDef), } @@ -158,9 +134,9 @@ impl CallableFunction { /// # Panics /// /// Panics if the `CallableFunction` is not `Iterator`. - pub fn get_iter_fn(&self) -> &IteratorFn { + pub fn get_iter_fn(&self) -> IteratorFn { match self { - Self::Iterator(f) => f.as_ref(), + Self::Iterator(f) => *f, Self::Pure(_) | Self::Method(_) | Self::Script(_) => panic!(), } } diff --git a/src/module.rs b/src/module.rs index 5e751df8..48e6e936 100644 --- a/src/module.rs +++ b/src/module.rs @@ -3,7 +3,7 @@ use crate::any::{Dynamic, Variant}; use crate::calc_fn_hash; use crate::engine::{Engine, FunctionsLib}; -use crate::fn_native::{CallableFunction as CF, FnCallArgs, IteratorFn, SharedIteratorFn}; +use crate::fn_native::{CallableFunction as CF, FnCallArgs, IteratorFn}; use crate::parser::{ FnAccess, FnAccess::{Private, Public}, @@ -53,7 +53,7 @@ pub struct Module { fn_lib: FunctionsLib, /// Iterator functions, keyed by the type producing the iterator. - type_iterators: HashMap, + type_iterators: HashMap, /// Flattened collection of all external Rust functions, native or scripted, /// including those in sub-modules. @@ -659,13 +659,13 @@ impl Module { } /// Set a type iterator into the module. - pub fn set_iter(&mut self, typ: TypeId, func: Box) { - self.type_iterators.insert(typ, func.into()); + pub fn set_iter(&mut self, typ: TypeId, func: IteratorFn) { + self.type_iterators.insert(typ, func); } /// Get the specified type iterator. - pub fn get_iter(&self, id: TypeId) -> Option<&IteratorFn> { - self.type_iterators.get(&id).map(|v| v.as_ref()) + pub fn get_iter(&self, id: TypeId) -> Option { + self.type_iterators.get(&id).cloned() } } diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index bdfd03aa..eb56bbf4 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -120,8 +120,6 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, { // Register array iterator lib.set_iter( TypeId::of::(), - Box::new(|arr| Box::new( - arr.cast::().into_iter()) as Box> - ), + |arr| Box::new(arr.cast::().into_iter()) as Box>, ); }); diff --git a/src/packages/iter_basic.rs b/src/packages/iter_basic.rs index 553873e7..ebf55992 100644 --- a/src/packages/iter_basic.rs +++ b/src/packages/iter_basic.rs @@ -14,13 +14,10 @@ fn reg_range(lib: &mut Module) where Range: Iterator, { - lib.set_iter( - TypeId::of::>(), - Box::new(|source| { - Box::new(source.cast::>().map(|x| x.into_dynamic())) - as Box> - }), - ); + lib.set_iter(TypeId::of::>(), |source| { + Box::new(source.cast::>().map(|x| x.into_dynamic())) + as Box> + }); } fn get_range(from: T, to: T) -> FuncReturn> { @@ -58,13 +55,10 @@ where T: Variant + Clone + PartialOrd, StepRange: Iterator, { - lib.set_iter( - TypeId::of::>(), - Box::new(|source| { - Box::new(source.cast::>().map(|x| x.into_dynamic())) - as Box> - }), - ); + lib.set_iter(TypeId::of::>(), |source| { + Box::new(source.cast::>().map(|x| x.into_dynamic())) + as Box> + }); } fn get_step_range(from: T, to: T, step: T) -> FuncReturn> diff --git a/src/packages/mod.rs b/src/packages/mod.rs index 62df57a6..54b6ecea 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -83,7 +83,7 @@ impl PackagesCollection { self.packages.iter().any(|p| p.contains_iter(id)) } /// Get the specified TypeId iterator. - pub fn get_iter(&self, id: TypeId) -> Option<&IteratorFn> { + pub fn get_iter(&self, id: TypeId) -> Option { self.packages .iter() .map(|p| p.get_iter(id))