Simplify iterator registration.

This commit is contained in:
Stephen Chung 2020-10-15 22:11:18 +08:00
parent ea9ef1091a
commit 3b99b8f166
5 changed files with 27 additions and 36 deletions

View File

@ -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<T: Variant + Clone + IntoIterator<Item = U>, U: Variant + Clone>(
&mut self,
) -> &mut Self {
self.global_module.set_iterable::<T, U>();
pub fn register_iterator<T>(&mut self) -> &mut Self
where
T: Variant + Clone + Iterator,
<T as Iterator>::Item: Variant + Clone,
{
self.global_module.set_iterable::<T>();
self
}

View File

@ -1469,18 +1469,22 @@ impl Module {
}
/// Set a type iterator into the module.
pub fn set_iterable<T: Variant + Clone + IntoIterator<Item = U>, U: Variant + Clone>(
&mut self,
) -> &mut Self {
pub fn set_iterable<T>(&mut self) -> &mut Self
where
T: Variant + Clone + IntoIterator,
<T as IntoIterator>::Item: Variant + Clone,
{
self.set_iter(TypeId::of::<T>(), |obj: Dynamic| {
Box::new(obj.cast::<T>().into_iter().map(Dynamic::from))
})
}
/// Set an iterator type into the module as a type iterator.
pub fn set_iterator<T: Variant + Clone + Iterator<Item = U>, U: Variant + Clone>(
&mut self,
) -> &mut Self {
pub fn set_iterator<T>(&mut self) -> &mut Self
where
T: Variant + Clone + Iterator,
<T as Iterator>::Item: Variant + Clone,
{
self.set_iter(TypeId::of::<T>(), |obj: Dynamic| {
Box::new(obj.cast::<T>().map(Dynamic::from))
})

View File

@ -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::<Array, Dynamic>();
lib.set_iterable::<Array>();
});
#[export_module]

View File

@ -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<T: Variant + Clone>(lib: &mut Module)
where
Range<T>: Iterator<Item = T>,
{
lib.set_iterator::<Range<T>, T>();
}
fn get_range<T: Variant + Clone>(from: T, to: T) -> FuncReturn<Range<T>> {
Ok(from..to)
}
@ -42,15 +34,6 @@ where
}
}
fn reg_step<T>(lib: &mut Module)
where
for<'a> &'a T: Add<&'a T, Output = T>,
T: Variant + Clone + PartialOrd,
StepRange<T>: Iterator<Item = T>,
{
lib.set_iterator::<StepRange<T>, T>();
}
fn get_step_range<T>(from: T, to: T, step: T) -> FuncReturn<StepRange<T>>
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::<INT>(lib);
lib.set_iterator::<Range<INT>>();
lib.set_fn_2("range", get_range::<INT>);
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::<Range<$y>>();
$lib.set_fn_2($x, get_range::<$y>);
)*
)
@ -80,14 +64,14 @@ def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, {
}
}
reg_step::<INT>(lib);
lib.set_iterator::<StepRange<INT>>();
lib.set_fn_3("range", get_step_range::<INT>);
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::<StepRange<$y>>();
$lib.set_fn_3($x, get_step_range::<$y>);
)*
)

View File

@ -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<Dynamic> {
pub fn keys(map: &mut Map) -> Array {
map.iter().map(|(k, _)| k.clone().into()).collect()
}
pub fn values(map: &mut Map) -> Vec<Dynamic> {
pub fn values(map: &mut Map) -> Array {
map.iter().map(|(_, v)| v.clone()).collect()
}
}