Merge pull request #6 from schungx/master

Avoid copying iterator sources.
This commit is contained in:
Stephen Chung 2020-04-26 17:03:28 +08:00 committed by GitHub
commit 5da9bc0f98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 27 deletions

View File

@ -44,19 +44,19 @@ impl<F: Fn(&mut T, U) + 'static, T, U> ObjectSetCallback<T, U> for F {}
#[cfg(feature = "sync")]
pub trait IteratorCallback:
Fn(&Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + Send + Sync + 'static
Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + Send + Sync + 'static
{
}
#[cfg(feature = "sync")]
impl<F: Fn(&Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + Send + Sync + 'static> IteratorCallback
impl<F: Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + Send + Sync + 'static> IteratorCallback
for F
{
}
#[cfg(not(feature = "sync"))]
pub trait IteratorCallback: Fn(&Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + 'static {}
pub trait IteratorCallback: Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + 'static {}
#[cfg(not(feature = "sync"))]
impl<F: Fn(&Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + 'static> IteratorCallback for F {}
impl<F: Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + 'static> IteratorCallback for F {}
/// Engine public API
impl Engine {

View File

@ -49,9 +49,9 @@ pub type FnAny =
pub type FnAny = dyn Fn(&mut FnCallArgs, Position) -> Result<Dynamic, Box<EvalAltResult>>;
#[cfg(feature = "sync")]
pub type IteratorFn = dyn Fn(&Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + Send + Sync;
pub type IteratorFn = dyn Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + Send + Sync;
#[cfg(not(feature = "sync"))]
pub type IteratorFn = dyn Fn(&Dynamic) -> Box<dyn Iterator<Item = Dynamic>>;
pub type IteratorFn = dyn Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>>;
#[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) {

View File

@ -116,8 +116,8 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
// Register array iterator
lib.type_iterators.insert(
TypeId::of::<Array>(),
Box::new(|a: &Dynamic| {
Box::new(a.downcast_ref::<Array>().unwrap().clone().into_iter())
Box::new(|a: Dynamic| {
Box::new(a.cast::<Array>().into_iter())
as Box<dyn Iterator<Item = Dynamic>>
}),
);

View File

@ -19,14 +19,9 @@ where
{
lib.type_iterators.insert(
TypeId::of::<Range<T>>(),
Box::new(|source: &Dynamic| {
Box::new(
source
.downcast_ref::<Range<T>>()
.cloned()
.unwrap()
.map(|x| x.into_dynamic()),
) as Box<dyn Iterator<Item = Dynamic>>
Box::new(|source: Dynamic| {
Box::new(source.cast::<Range<T>>().map(|x| x.into_dynamic()))
as Box<dyn Iterator<Item = Dynamic>>
}),
);
}
@ -64,14 +59,9 @@ where
{
lib.type_iterators.insert(
TypeId::of::<StepRange<T>>(),
Box::new(|source: &Dynamic| {
Box::new(
source
.downcast_ref::<StepRange<T>>()
.cloned()
.unwrap()
.map(|x| x.into_dynamic()),
) as Box<dyn Iterator<Item = Dynamic>>
Box::new(|source: Dynamic| {
Box::new(source.cast::<StepRange<T>>().map(|x| x.into_dynamic()))
as Box<dyn Iterator<Item = Dynamic>>
}),
);
}