2020-03-08 12:54:02 +01:00
|
|
|
//! Helper module which defines `FnArgs` to make function calling easier.
|
2017-12-20 22:16:53 +01:00
|
|
|
|
2020-03-18 05:04:26 +01:00
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
2020-03-10 07:09:05 +01:00
|
|
|
use crate::any::{Any, Dynamic};
|
2020-03-14 13:06:40 +01:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-03-12 07:54:14 +01:00
|
|
|
use crate::engine::Array;
|
2017-12-20 22:16:53 +01:00
|
|
|
|
2020-03-18 15:03:50 +01:00
|
|
|
use crate::stdlib::{string::String, vec, vec::Vec};
|
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
/// Trait that represent arguments to a function call.
|
2020-03-10 07:09:05 +01:00
|
|
|
pub trait FuncArgs {
|
|
|
|
/// Convert to a `Vec` of `Dynamic` arguments.
|
|
|
|
fn into_vec(self) -> Vec<Dynamic>;
|
2017-12-20 22:16:53 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 07:54:14 +01:00
|
|
|
macro_rules! impl_std_args {
|
|
|
|
($($p:ty),*) => {
|
|
|
|
$(
|
|
|
|
impl FuncArgs for $p {
|
|
|
|
fn into_vec(self) -> Vec<Dynamic> {
|
|
|
|
vec![self.into_dynamic()]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
};
|
2020-03-04 15:00:01 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 07:54:14 +01:00
|
|
|
impl_std_args!(String, char, bool);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
impl_std_args!(Array);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
#[cfg(not(feature = "only_i64"))]
|
|
|
|
impl_std_args!(u8, i8, u16, i16, u32, i32, u64, i64);
|
|
|
|
|
|
|
|
#[cfg(feature = "only_i32")]
|
|
|
|
impl_std_args!(i32);
|
|
|
|
|
|
|
|
#[cfg(feature = "only_i64")]
|
|
|
|
impl_std_args!(i64);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
impl_std_args!(f32, f64);
|
|
|
|
|
2017-12-20 22:16:53 +01:00
|
|
|
macro_rules! impl_args {
|
|
|
|
($($p:ident),*) => {
|
2020-03-10 07:09:05 +01:00
|
|
|
impl<$($p: Any + Clone),*> FuncArgs for ($($p,)*)
|
2017-12-20 22:16:53 +01:00
|
|
|
{
|
2020-03-10 07:09:05 +01:00
|
|
|
fn into_vec(self) -> Vec<Dynamic> {
|
2017-12-20 22:16:53 +01:00
|
|
|
let ($($p,)*) = self;
|
|
|
|
|
2019-09-18 12:21:07 +02:00
|
|
|
#[allow(unused_variables, unused_mut)]
|
2017-12-20 22:16:53 +01:00
|
|
|
let mut v = Vec::new();
|
2020-03-10 07:09:05 +01:00
|
|
|
$(v.push($p.into_dynamic());)*
|
2017-12-20 22:16:53 +01:00
|
|
|
|
|
|
|
v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_args!(@pop $($p),*);
|
|
|
|
};
|
|
|
|
(@pop) => {
|
|
|
|
};
|
2020-03-12 07:54:14 +01:00
|
|
|
(@pop $head:ident) => {
|
|
|
|
};
|
|
|
|
(@pop $head:ident $(, $tail:ident)+) => {
|
2017-12-20 22:16:53 +01:00
|
|
|
impl_args!($($tail),*);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(rustfmt, rustfmt_skip)]
|
2020-03-10 07:09:05 +01:00
|
|
|
impl_args!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T);
|