Simplify iterator registration.
This commit is contained in:
parent
ea9ef1091a
commit
3b99b8f166
10
src/api.rs
10
src/api.rs
@ -177,10 +177,12 @@ impl Engine {
|
|||||||
/// Register an iterator adapter for an iterable type with the `Engine`.
|
/// Register an iterator adapter for an iterable type with the `Engine`.
|
||||||
/// This is an advanced feature.
|
/// This is an advanced feature.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn register_iterator<T: Variant + Clone + IntoIterator<Item = U>, U: Variant + Clone>(
|
pub fn register_iterator<T>(&mut self) -> &mut Self
|
||||||
&mut self,
|
where
|
||||||
) -> &mut Self {
|
T: Variant + Clone + Iterator,
|
||||||
self.global_module.set_iterable::<T, U>();
|
<T as Iterator>::Item: Variant + Clone,
|
||||||
|
{
|
||||||
|
self.global_module.set_iterable::<T>();
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1469,18 +1469,22 @@ impl Module {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Set a type iterator into the module.
|
/// Set a type iterator into the module.
|
||||||
pub fn set_iterable<T: Variant + Clone + IntoIterator<Item = U>, U: Variant + Clone>(
|
pub fn set_iterable<T>(&mut self) -> &mut Self
|
||||||
&mut self,
|
where
|
||||||
) -> &mut Self {
|
T: Variant + Clone + IntoIterator,
|
||||||
|
<T as IntoIterator>::Item: Variant + Clone,
|
||||||
|
{
|
||||||
self.set_iter(TypeId::of::<T>(), |obj: Dynamic| {
|
self.set_iter(TypeId::of::<T>(), |obj: Dynamic| {
|
||||||
Box::new(obj.cast::<T>().into_iter().map(Dynamic::from))
|
Box::new(obj.cast::<T>().into_iter().map(Dynamic::from))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set an iterator type into the module as a type iterator.
|
/// Set an iterator type into the module as a type iterator.
|
||||||
pub fn set_iterator<T: Variant + Clone + Iterator<Item = U>, U: Variant + Clone>(
|
pub fn set_iterator<T>(&mut self) -> &mut Self
|
||||||
&mut self,
|
where
|
||||||
) -> &mut Self {
|
T: Variant + Clone + Iterator,
|
||||||
|
<T as Iterator>::Item: Variant + Clone,
|
||||||
|
{
|
||||||
self.set_iter(TypeId::of::<T>(), |obj: Dynamic| {
|
self.set_iter(TypeId::of::<T>(), |obj: Dynamic| {
|
||||||
Box::new(obj.cast::<T>().map(Dynamic::from))
|
Box::new(obj.cast::<T>().map(Dynamic::from))
|
||||||
})
|
})
|
||||||
|
@ -86,7 +86,7 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
|
|||||||
combine_with_exported_module!(lib, "array", array_functions);
|
combine_with_exported_module!(lib, "array", array_functions);
|
||||||
|
|
||||||
// Register array iterator
|
// Register array iterator
|
||||||
lib.set_iterable::<Array, Dynamic>();
|
lib.set_iterable::<Array>();
|
||||||
});
|
});
|
||||||
|
|
||||||
#[export_module]
|
#[export_module]
|
||||||
|
@ -1,18 +1,10 @@
|
|||||||
use crate::any::Variant;
|
use crate::any::Variant;
|
||||||
use crate::def_package;
|
use crate::def_package;
|
||||||
use crate::module::{FuncReturn, Module};
|
use crate::module::FuncReturn;
|
||||||
use crate::parser::INT;
|
use crate::parser::INT;
|
||||||
|
|
||||||
use crate::stdlib::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_iterator::<Range<T>, T>();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_range<T: Variant + Clone>(from: T, to: T) -> FuncReturn<Range<T>> {
|
fn get_range<T: Variant + Clone>(from: T, to: T) -> FuncReturn<Range<T>> {
|
||||||
Ok(from..to)
|
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>>
|
fn get_step_range<T>(from: T, to: T, step: T) -> FuncReturn<StepRange<T>>
|
||||||
where
|
where
|
||||||
for<'a> &'a T: Add<&'a T, Output = T>,
|
for<'a> &'a T: Add<&'a T, Output = T>,
|
||||||
@ -60,14 +43,15 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, {
|
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>);
|
lib.set_fn_2("range", get_range::<INT>);
|
||||||
|
|
||||||
if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) {
|
if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) {
|
||||||
macro_rules! reg_range {
|
macro_rules! reg_range {
|
||||||
($lib:expr, $x:expr, $( $y:ty ),*) => (
|
($lib:expr, $x:expr, $( $y:ty ),*) => (
|
||||||
$(
|
$(
|
||||||
reg_range::<$y>($lib);
|
$lib.set_iterator::<Range<$y>>();
|
||||||
$lib.set_fn_2($x, get_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>);
|
lib.set_fn_3("range", get_step_range::<INT>);
|
||||||
|
|
||||||
if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) {
|
if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) {
|
||||||
macro_rules! reg_step {
|
macro_rules! reg_step {
|
||||||
($lib:expr, $x:expr, $( $y:ty ),*) => (
|
($lib:expr, $x:expr, $( $y:ty ),*) => (
|
||||||
$(
|
$(
|
||||||
reg_step::<$y>($lib);
|
$lib.set_iterator::<StepRange<$y>>();
|
||||||
$lib.set_fn_3($x, get_step_range::<$y>);
|
$lib.set_fn_3($x, get_step_range::<$y>);
|
||||||
)*
|
)*
|
||||||
)
|
)
|
||||||
|
@ -6,7 +6,8 @@ use crate::engine::Map;
|
|||||||
use crate::parser::{ImmutableString, INT};
|
use crate::parser::{ImmutableString, INT};
|
||||||
use crate::plugin::*;
|
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, {
|
def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
|
||||||
combine_with_exported_module!(lib, "map", map_functions);
|
combine_with_exported_module!(lib, "map", map_functions);
|
||||||
@ -47,10 +48,10 @@ mod map_functions {
|
|||||||
|
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
pub mod indexing {
|
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()
|
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()
|
map.iter().map(|(_, v)| v.clone()).collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user