rhai/src/fn_call.rs

47 lines
1.2 KiB
Rust
Raw Normal View History

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
#![allow(non_snake_case)]
2020-04-12 17:00:06 +02:00
use crate::any::{Dynamic, Variant};
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.
pub trait FuncArgs {
2020-04-21 17:01:10 +02:00
/// Convert to a `Vec<Dynamic>` of the function call arguments.
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
{
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();
$(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-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);