Refactor iterators API.

This commit is contained in:
Stephen Chung
2020-10-14 23:22:10 +08:00
parent e0c39edff4
commit 707ece7e80
8 changed files with 41 additions and 33 deletions

View File

@@ -1,23 +1,16 @@
use crate::any::{Dynamic, Variant};
use crate::any::Variant;
use crate::def_package;
use crate::module::{FuncReturn, Module};
use crate::parser::INT;
use crate::stdlib::{
any::TypeId,
boxed::Box,
ops::{Add, Range},
};
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_iter(TypeId::of::<Range<T>>(), |source| {
Box::new(source.cast::<Range<T>>().map(|x| x.into_dynamic()))
as Box<dyn Iterator<Item = Dynamic>>
});
lib.set_iterator::<Range<T>, T>();
}
fn get_range<T: Variant + Clone>(from: T, to: T) -> FuncReturn<Range<T>> {
@@ -55,10 +48,7 @@ where
T: Variant + Clone + PartialOrd,
StepRange<T>: Iterator<Item = T>,
{
lib.set_iter(TypeId::of::<StepRange<T>>(), |source| {
Box::new(source.cast::<StepRange<T>>().map(|x| x.into_dynamic()))
as Box<dyn Iterator<Item = Dynamic>>
});
lib.set_iterator::<StepRange<T>, T>();
}
fn get_step_range<T>(from: T, to: T, step: T) -> FuncReturn<StepRange<T>>