rhai/src/call.rs

49 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
use crate::any::{Any, Variant};
2017-12-20 22:16:53 +01:00
2020-03-04 15:00:01 +01:00
/// Trait that represent arguments to a function call.
pub trait FuncArgs<'a> {
/// Convert to a `Vec` of `Variant` arguments.
fn into_vec(self) -> Vec<&'a mut Variant>;
2017-12-20 22:16:53 +01:00
}
2020-03-04 15:00:01 +01:00
impl<'a> FuncArgs<'a> for Vec<&'a mut Variant> {
fn into_vec(self) -> Self {
self
}
}
impl<'a, T: Any> FuncArgs<'a> for &'a mut Vec<T> {
fn into_vec(self) -> Vec<&'a mut Variant> {
self.iter_mut().map(|x| x as &mut Variant).collect()
}
}
2017-12-20 22:16:53 +01:00
macro_rules! impl_args {
($($p:ident),*) => {
2020-03-04 15:00:01 +01:00
impl<'a, $($p: Any + Clone),*> FuncArgs<'a> for ($(&'a mut $p,)*)
2017-12-20 22:16:53 +01:00
{
fn into_vec(self) -> Vec<&'a mut Variant> {
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 as &mut Variant);)*
2017-12-20 22:16:53 +01:00
v
}
}
impl_args!(@pop $($p),*);
};
(@pop) => {
};
(@pop $head:ident $(, $tail:ident)*) => {
impl_args!($($tail),*);
};
}
#[cfg_attr(rustfmt, rustfmt_skip)]
impl_args!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S);