2020-11-20 09:52:28 +01:00
|
|
|
//! Helper module which defines [`FuncArgs`] to make function calling easier.
|
2020-07-23 12:40:42 +02:00
|
|
|
|
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
2020-11-16 16:10:14 +01:00
|
|
|
use crate::dynamic::Variant;
|
|
|
|
use crate::{Dynamic, StaticVec};
|
2020-07-23 12:40:42 +02:00
|
|
|
|
|
|
|
/// Trait that represents arguments to a function call.
|
2020-11-25 02:36:06 +01:00
|
|
|
/// Any data type that can be converted into a [`Vec`]`<`[`Dynamic`]`>` can be used
|
2020-07-23 12:40:42 +02:00
|
|
|
/// as arguments to a function call.
|
|
|
|
pub trait FuncArgs {
|
2020-11-20 09:52:28 +01:00
|
|
|
/// Convert to a [`StaticVec`]`<`[`Dynamic`]`>` of the function call arguments.
|
2020-07-23 12:40:42 +02:00
|
|
|
fn into_vec(self) -> StaticVec<Dynamic>;
|
|
|
|
}
|
|
|
|
|
2020-11-20 09:52:28 +01:00
|
|
|
/// Macro to implement [`FuncArgs`] for tuples of standard types (each can be
|
|
|
|
/// converted into a [`Dynamic`]).
|
2020-07-23 12:40:42 +02:00
|
|
|
macro_rules! impl_args {
|
|
|
|
($($p:ident),*) => {
|
|
|
|
impl<$($p: Variant + Clone),*> FuncArgs for ($($p,)*)
|
|
|
|
{
|
2020-10-08 16:25:50 +02:00
|
|
|
#[inline]
|
2020-07-23 12:40:42 +02:00
|
|
|
fn into_vec(self) -> StaticVec<Dynamic> {
|
|
|
|
let ($($p,)*) = self;
|
|
|
|
|
2020-07-26 09:53:22 +02:00
|
|
|
let mut _v = StaticVec::new();
|
|
|
|
$(_v.push($p.into_dynamic());)*
|
2020-07-23 12:40:42 +02:00
|
|
|
|
2020-07-26 09:53:22 +02:00
|
|
|
_v
|
2020-07-23 12:40:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_args!(@pop $($p),*);
|
|
|
|
};
|
|
|
|
(@pop) => {
|
|
|
|
};
|
|
|
|
(@pop $head:ident) => {
|
|
|
|
impl_args!();
|
|
|
|
};
|
|
|
|
(@pop $head:ident $(, $tail:ident)+) => {
|
|
|
|
impl_args!($($tail),*);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_args!(A, B, C, D, E, F, G, H, J, K, L, M, N, P, Q, R, S, T, U, V);
|