1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use crate::any::Variant;
use crate::def_package;
use crate::module::FuncReturn;
use crate::parser::INT;
use crate::stdlib::ops::{Add, Range};
fn get_range<T: Variant + Clone>(from: T, to: T) -> FuncReturn<Range<T>> {
Ok(from..to)
}
#[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
}
}
}
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))
}
def_package!(crate:BasicIteratorPackage:"Basic range iterators.", 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 ),*) => (
$(
$lib.set_iterator::<Range<$y>>();
$lib.set_fn_2($x, get_range::<$y>);
)*
)
}
reg_range!(lib, "range", i8, u8, i16, u16, i32, i64, u32, u64);
if cfg!(not(target_arch = "wasm32")) {
reg_range!(lib, "range", i128, u128);
}
}
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 ),*) => (
$(
$lib.set_iterator::<StepRange<$y>>();
$lib.set_fn_3($x, get_step_range::<$y>);
)*
)
}
reg_step!(lib, "range", i8, u8, i16, u16, i32, i64, u32, u64);
if cfg!(not(target_arch = "wasm32")) {
reg_step!(lib, "range", i128, u128);
}
}
});