diff --git a/src/api.rs b/src/api.rs index 14fb4083..8018e447 100644 --- a/src/api.rs +++ b/src/api.rs @@ -177,10 +177,12 @@ impl Engine { /// Register an iterator adapter for an iterable type with the `Engine`. /// This is an advanced feature. #[inline(always)] - pub fn register_iterator, U: Variant + Clone>( - &mut self, - ) -> &mut Self { - self.global_module.set_iterable::(); + pub fn register_iterator(&mut self) -> &mut Self + where + T: Variant + Clone + Iterator, + ::Item: Variant + Clone, + { + self.global_module.set_iterable::(); self } diff --git a/src/module/mod.rs b/src/module/mod.rs index ed135f71..d7cc40d9 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -1469,18 +1469,22 @@ impl Module { } /// Set a type iterator into the module. - pub fn set_iterable, U: Variant + Clone>( - &mut self, - ) -> &mut Self { + pub fn set_iterable(&mut self) -> &mut Self + where + T: Variant + Clone + IntoIterator, + ::Item: Variant + Clone, + { self.set_iter(TypeId::of::(), |obj: Dynamic| { Box::new(obj.cast::().into_iter().map(Dynamic::from)) }) } /// Set an iterator type into the module as a type iterator. - pub fn set_iterator, U: Variant + Clone>( - &mut self, - ) -> &mut Self { + pub fn set_iterator(&mut self) -> &mut Self + where + T: Variant + Clone + Iterator, + ::Item: Variant + Clone, + { self.set_iter(TypeId::of::(), |obj: Dynamic| { Box::new(obj.cast::().map(Dynamic::from)) }) diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index 43543ad8..de5bdfcc 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -86,7 +86,7 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, { combine_with_exported_module!(lib, "array", array_functions); // Register array iterator - lib.set_iterable::(); + lib.set_iterable::(); }); #[export_module] diff --git a/src/packages/iter_basic.rs b/src/packages/iter_basic.rs index eaf74fe0..c4832c95 100644 --- a/src/packages/iter_basic.rs +++ b/src/packages/iter_basic.rs @@ -1,18 +1,10 @@ use crate::any::Variant; use crate::def_package; -use crate::module::{FuncReturn, Module}; +use crate::module::FuncReturn; use crate::parser::INT; use crate::stdlib::ops::{Add, Range}; -// Register range function -fn reg_range(lib: &mut Module) -where - Range: Iterator, -{ - lib.set_iterator::, T>(); -} - fn get_range(from: T, to: T) -> FuncReturn> { Ok(from..to) } @@ -42,15 +34,6 @@ where } } -fn reg_step(lib: &mut Module) -where - for<'a> &'a T: Add<&'a T, Output = T>, - T: Variant + Clone + PartialOrd, - StepRange: Iterator, -{ - lib.set_iterator::, T>(); -} - fn get_step_range(from: T, to: T, step: T) -> FuncReturn> where for<'a> &'a T: Add<&'a T, Output = T>, @@ -60,14 +43,15 @@ where } def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, { - reg_range::(lib); + lib.set_iterator::>(); + lib.set_fn_2("range", get_range::); if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) { macro_rules! reg_range { ($lib:expr, $x:expr, $( $y:ty ),*) => ( $( - reg_range::<$y>($lib); + $lib.set_iterator::>(); $lib.set_fn_2($x, get_range::<$y>); )* ) @@ -80,14 +64,14 @@ def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, { } } - reg_step::(lib); + lib.set_iterator::>(); lib.set_fn_3("range", get_step_range::); if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) { macro_rules! reg_step { ($lib:expr, $x:expr, $( $y:ty ),*) => ( $( - reg_step::<$y>($lib); + $lib.set_iterator::>(); $lib.set_fn_3($x, get_step_range::<$y>); )* ) diff --git a/src/packages/map_basic.rs b/src/packages/map_basic.rs index 099f546c..26fc3fe3 100644 --- a/src/packages/map_basic.rs +++ b/src/packages/map_basic.rs @@ -6,7 +6,8 @@ use crate::engine::Map; use crate::parser::{ImmutableString, INT}; use crate::plugin::*; -use crate::stdlib::vec::Vec; +#[cfg(not(feature = "no_index"))] +use crate::engine::Array; def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, { combine_with_exported_module!(lib, "map", map_functions); @@ -47,10 +48,10 @@ mod map_functions { #[cfg(not(feature = "no_index"))] pub mod indexing { - pub fn keys(map: &mut Map) -> Vec { + pub fn keys(map: &mut Map) -> Array { map.iter().map(|(k, _)| k.clone().into()).collect() } - pub fn values(map: &mut Map) -> Vec { + pub fn values(map: &mut Map) -> Array { map.iter().map(|(_, v)| v.clone()).collect() } }