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-04-12 17:00:06 +02:00
|
|
|
use crate::any::{Dynamic, Variant};
|
2020-04-15 16:21:23 +02:00
|
|
|
use crate::stdlib::vec::Vec;
|
2020-03-18 15:03:50 +01:00
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
/// Trait that represents arguments to a function call.
|
|
|
|
/// Any data type that can be converted into a `Vec<Dynamic>` can be used
|
2020-03-25 04:24:06 +01:00
|
|
|
/// as arguments to a function call.
|
2020-03-10 07:09:05 +01:00
|
|
|
pub trait FuncArgs {
|
2020-04-21 17:01:10 +02:00
|
|
|
/// Convert to a `Vec<Dynamic>` of the function call arguments.
|
2020-03-10 07:09:05 +01:00
|
|
|
fn into_vec(self) -> Vec<Dynamic>;
|
2017-12-20 22:16:53 +01:00
|
|
|
}
|
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
/// Macro to implement `FuncArgs` for tuples of standard types (each can be
|
2020-03-25 04:24:06 +01:00
|
|
|
/// converted into `Dynamic`).
|
2017-12-20 22:16:53 +01:00
|
|
|
macro_rules! impl_args {
|
|
|
|
($($p:ident),*) => {
|
2020-04-12 17:00:06 +02:00
|
|
|
impl<$($p: Variant + 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) => {
|
2020-04-07 15:50:33 +02:00
|
|
|
impl_args!();
|
2020-03-12 07:54:14 +01:00
|
|
|
};
|
|
|
|
(@pop $head:ident $(, $tail:ident)+) => {
|
2017-12-20 22:16:53 +01:00
|
|
|
impl_args!($($tail),*);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-24 09:57:35 +01:00
|
|
|
#[rustfmt::skip]
|
2020-03-26 03:56:18 +01:00
|
|
|
impl_args!(A, B, C, D, E, F, G, H, J, K, L, M, N, P, Q, R, S, T, U, V);
|