rhai/src/fn_call.rs

46 lines
1.2 KiB
Rust
Raw Normal View History

2020-06-18 12:39:28 +02:00
//! Helper module which defines `FuncArgs` to make function calling easier.
2017-12-20 22:16:53 +01:00
#![allow(non_snake_case)]
2020-04-12 17:00:06 +02:00
use crate::any::{Dynamic, Variant};
2020-05-10 10:56:17 +02:00
use crate::utils::StaticVec;
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.
pub trait FuncArgs {
/// Convert to a `StaticVec<Dynamic>` of the function call arguments.
2020-05-10 10:56:17 +02:00
fn into_vec(self) -> StaticVec<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-05-10 10:56:17 +02:00
fn into_vec(self) -> StaticVec<Dynamic> {
2017-12-20 22:16:53 +01:00
let ($($p,)*) = self;
#[allow(unused_mut)]
2020-05-10 10:56:17 +02:00
let mut v = StaticVec::new();
$(v.push($p.into_dynamic());)*
2017-12-20 22:16:53 +01:00
v
}
}
impl_args!(@pop $($p),*);
};
(@pop) => {
};
(@pop $head:ident) => {
impl_args!();
};
(@pop $head:ident $(, $tail:ident)+) => {
2017-12-20 22:16:53 +01:00
impl_args!($($tail),*);
};
}
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);