2020-06-18 18:39:28 +08:00
|
|
|
//! Helper module which defines `FuncArgs` to make function calling easier.
|
2017-12-20 22:16:53 +01:00
|
|
|
|
2020-03-18 12:04:26 +08:00
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
2020-04-12 23:00:06 +08:00
|
|
|
use crate::any::{Dynamic, Variant};
|
2020-05-10 16:56:17 +08:00
|
|
|
use crate::utils::StaticVec;
|
2020-03-18 22:03:50 +08:00
|
|
|
|
2020-04-21 23:01:10 +08: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 11:24:06 +08:00
|
|
|
/// as arguments to a function call.
|
2020-03-10 14:09:05 +08:00
|
|
|
pub trait FuncArgs {
|
2020-05-17 00:24:07 +08:00
|
|
|
/// Convert to a `StaticVec<Dynamic>` of the function call arguments.
|
2020-05-10 16:56:17 +08:00
|
|
|
fn into_vec(self) -> StaticVec<Dynamic>;
|
2017-12-20 22:16:53 +01:00
|
|
|
}
|
|
|
|
|
2020-04-21 23:01:10 +08:00
|
|
|
/// Macro to implement `FuncArgs` for tuples of standard types (each can be
|
2020-03-25 11:24:06 +08:00
|
|
|
/// converted into `Dynamic`).
|
2017-12-20 22:16:53 +01:00
|
|
|
macro_rules! impl_args {
|
|
|
|
($($p:ident),*) => {
|
2020-04-12 23:00:06 +08:00
|
|
|
impl<$($p: Variant + Clone),*> FuncArgs for ($($p,)*)
|
2017-12-20 22:16:53 +01:00
|
|
|
{
|
2020-05-10 16:56:17 +08:00
|
|
|
fn into_vec(self) -> StaticVec<Dynamic> {
|
2017-12-20 22:16:53 +01:00
|
|
|
let ($($p,)*) = self;
|
|
|
|
|
2020-05-03 16:54:24 +08:00
|
|
|
#[allow(unused_mut)]
|
2020-05-10 16:56:17 +08:00
|
|
|
let mut v = StaticVec::new();
|
2020-03-10 14:09:05 +08:00
|
|
|
$(v.push($p.into_dynamic());)*
|
2017-12-20 22:16:53 +01:00
|
|
|
|
|
|
|
v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_args!(@pop $($p),*);
|
|
|
|
};
|
|
|
|
(@pop) => {
|
|
|
|
};
|
2020-03-12 14:54:14 +08:00
|
|
|
(@pop $head:ident) => {
|
2020-04-07 21:50:33 +08:00
|
|
|
impl_args!();
|
2020-03-12 14:54:14 +08:00
|
|
|
};
|
|
|
|
(@pop $head:ident $(, $tail:ident)+) => {
|
2017-12-20 22:16:53 +01:00
|
|
|
impl_args!($($tail),*);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-26 10:56:18 +08:00
|
|
|
impl_args!(A, B, C, D, E, F, G, H, J, K, L, M, N, P, Q, R, S, T, U, V);
|