2020-05-01 04:17:19 +02:00
|
|
|
use crate::any::{Dynamic, Variant};
|
2020-04-21 17:25:12 +02:00
|
|
|
use crate::def_package;
|
2020-05-13 13:21:42 +02:00
|
|
|
use crate::module::{FuncReturn, Module};
|
2020-04-20 18:11:25 +02:00
|
|
|
use crate::parser::INT;
|
|
|
|
|
|
|
|
use crate::stdlib::{
|
|
|
|
any::TypeId,
|
2020-04-24 06:39:24 +02:00
|
|
|
boxed::Box,
|
2020-04-21 17:01:10 +02:00
|
|
|
ops::{Add, Range},
|
2020-04-20 18:11:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Register range function
|
2020-05-13 13:21:42 +02:00
|
|
|
fn reg_range<T: Variant + Clone>(lib: &mut Module)
|
2020-04-20 18:11:25 +02:00
|
|
|
where
|
|
|
|
Range<T>: Iterator<Item = T>,
|
|
|
|
{
|
2020-05-20 13:27:23 +02:00
|
|
|
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>>
|
|
|
|
});
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
|
|
|
|
2020-05-13 13:21:42 +02:00
|
|
|
fn get_range<T: Variant + Clone>(from: T, to: T) -> FuncReturn<Range<T>> {
|
|
|
|
Ok(from..to)
|
|
|
|
}
|
|
|
|
|
2020-04-20 18:11:25 +02:00
|
|
|
// Register range function with step
|
|
|
|
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
|
|
|
|
struct StepRange<T>(T, T, T)
|
|
|
|
where
|
|
|
|
for<'a> &'a T: Add<&'a T, Output = T>,
|
|
|
|
T: Variant + Clone + PartialOrd;
|
|
|
|
|
|
|
|
impl<T> Iterator for StepRange<T>
|
|
|
|
where
|
|
|
|
for<'a> &'a T: Add<&'a T, Output = T>,
|
|
|
|
T: Variant + Clone + PartialOrd,
|
|
|
|
{
|
|
|
|
type Item = T;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<T> {
|
|
|
|
if self.0 < self.1 {
|
|
|
|
let v = self.0.clone();
|
|
|
|
self.0 = &v + &self.2;
|
|
|
|
Some(v)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 13:21:42 +02:00
|
|
|
fn reg_step<T>(lib: &mut Module)
|
2020-04-20 18:11:25 +02:00
|
|
|
where
|
|
|
|
for<'a> &'a T: Add<&'a T, Output = T>,
|
|
|
|
T: Variant + Clone + PartialOrd,
|
|
|
|
StepRange<T>: Iterator<Item = T>,
|
|
|
|
{
|
2020-05-20 13:27:23 +02:00
|
|
|
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>>
|
|
|
|
});
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
|
|
|
|
2020-05-13 13:21:42 +02:00
|
|
|
fn get_step_range<T>(from: T, to: T, step: T) -> FuncReturn<StepRange<T>>
|
|
|
|
where
|
|
|
|
for<'a> &'a T: Add<&'a T, Output = T>,
|
|
|
|
T: Variant + Clone + PartialOrd,
|
|
|
|
{
|
|
|
|
Ok(StepRange::<T>(from, to, step))
|
|
|
|
}
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-05-13 13:21:42 +02:00
|
|
|
def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, {
|
2020-04-21 17:01:10 +02:00
|
|
|
reg_range::<INT>(lib);
|
2020-05-13 13:21:42 +02:00
|
|
|
lib.set_fn_2("range", get_range::<INT>);
|
2020-04-21 17:01:10 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
#[cfg(not(feature = "only_i64"))]
|
|
|
|
{
|
|
|
|
macro_rules! reg_range {
|
2020-05-13 13:21:42 +02:00
|
|
|
($lib:expr, $x:expr, $( $y:ty ),*) => (
|
2020-04-21 17:01:10 +02:00
|
|
|
$(
|
2020-05-13 13:21:42 +02:00
|
|
|
reg_range::<$y>($lib);
|
|
|
|
$lib.set_fn_2($x, get_range::<$y>);
|
2020-04-21 17:01:10 +02:00
|
|
|
)*
|
|
|
|
)
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
|
|
|
|
2020-06-17 10:50:46 +02:00
|
|
|
reg_range!(lib, "range", i8, u8, i16, u16, i32, i64, u32, u64);
|
|
|
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
reg_range!(lib, "range", i128, u128);
|
2020-04-21 17:01:10 +02:00
|
|
|
}
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
reg_step::<INT>(lib);
|
2020-05-13 13:21:42 +02:00
|
|
|
lib.set_fn_3("range", get_step_range::<INT>);
|
2020-04-21 17:01:10 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
#[cfg(not(feature = "only_i64"))]
|
|
|
|
{
|
|
|
|
macro_rules! reg_step {
|
2020-05-13 13:21:42 +02:00
|
|
|
($lib:expr, $x:expr, $( $y:ty ),*) => (
|
2020-04-21 17:01:10 +02:00
|
|
|
$(
|
2020-05-13 13:21:42 +02:00
|
|
|
reg_step::<$y>($lib);
|
|
|
|
$lib.set_fn_3($x, get_step_range::<$y>);
|
2020-04-21 17:01:10 +02:00
|
|
|
)*
|
|
|
|
)
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
|
|
|
|
2020-06-17 10:50:46 +02:00
|
|
|
reg_step!(lib, "range", i8, u8, i16, u16, i32, i64, u32, u64);
|
|
|
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
reg_step!(lib, "range", i128, u128);
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
2020-04-21 17:01:10 +02:00
|
|
|
});
|