diff --git a/src/api.rs b/src/api.rs index 374fa48c..64f28b42 100644 --- a/src/api.rs +++ b/src/api.rs @@ -44,19 +44,19 @@ impl ObjectSetCallback for F {} #[cfg(feature = "sync")] pub trait IteratorCallback: - Fn(&Dynamic) -> Box> + Send + Sync + 'static + Fn(Dynamic) -> Box> + Send + Sync + 'static { } #[cfg(feature = "sync")] -impl Box> + Send + Sync + 'static> IteratorCallback +impl Box> + Send + Sync + 'static> IteratorCallback for F { } #[cfg(not(feature = "sync"))] -pub trait IteratorCallback: Fn(&Dynamic) -> Box> + 'static {} +pub trait IteratorCallback: Fn(Dynamic) -> Box> + 'static {} #[cfg(not(feature = "sync"))] -impl Box> + 'static> IteratorCallback for F {} +impl Box> + 'static> IteratorCallback for F {} /// Engine public API impl Engine { diff --git a/src/engine.rs b/src/engine.rs index aa6a8baa..b3ed775d 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -49,9 +49,9 @@ pub type FnAny = pub type FnAny = dyn Fn(&mut FnCallArgs, Position) -> Result>; #[cfg(feature = "sync")] -pub type IteratorFn = dyn Fn(&Dynamic) -> Box> + Send + Sync; +pub type IteratorFn = dyn Fn(Dynamic) -> Box> + Send + Sync; #[cfg(not(feature = "sync"))] -pub type IteratorFn = dyn Fn(&Dynamic) -> Box>; +pub type IteratorFn = dyn Fn(Dynamic) -> Box>; #[cfg(debug_assertions)] pub const MAX_CALL_STACK_DEPTH: usize = 28; @@ -1608,8 +1608,7 @@ impl Engine { .find(|pkg| pkg.type_iterators.contains_key(&tid)) .and_then(|pkg| pkg.type_iterators.get(&tid)) }) { - // Add the loop variable - variable name is copied - // TODO - avoid copying variable name + // Add the loop variable scope.push(name.clone(), ()); let entry = ScopeSource { @@ -1618,7 +1617,7 @@ impl Engine { typ: ScopeEntryType::Normal, }; - for a in iter_fn(&arr) { + for a in iter_fn(arr) { *scope.get_mut(entry) = a; match self.eval_stmt(scope, fn_lib, body, level) { diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index b00f012c..ca69f3fc 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -116,8 +116,8 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, { // Register array iterator lib.type_iterators.insert( TypeId::of::(), - Box::new(|a: &Dynamic| { - Box::new(a.downcast_ref::().unwrap().clone().into_iter()) + Box::new(|a: Dynamic| { + Box::new(a.cast::().into_iter()) as Box> }), ); diff --git a/src/packages/iter_basic.rs b/src/packages/iter_basic.rs index af2e6f53..9672d61c 100644 --- a/src/packages/iter_basic.rs +++ b/src/packages/iter_basic.rs @@ -19,14 +19,9 @@ where { lib.type_iterators.insert( TypeId::of::>(), - Box::new(|source: &Dynamic| { - Box::new( - source - .downcast_ref::>() - .cloned() - .unwrap() - .map(|x| x.into_dynamic()), - ) as Box> + Box::new(|source: Dynamic| { + Box::new(source.cast::>().map(|x| x.into_dynamic())) + as Box> }), ); } @@ -64,14 +59,9 @@ where { lib.type_iterators.insert( TypeId::of::>(), - Box::new(|source: &Dynamic| { - Box::new( - source - .downcast_ref::>() - .cloned() - .unwrap() - .map(|x| x.into_dynamic()), - ) as Box> + Box::new(|source: Dynamic| { + Box::new(source.cast::>().map(|x| x.into_dynamic())) + as Box> }), ); }