1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//! Helper module which defines [`FuncArgs`] to make function calling easier.
#![cfg(not(feature = "no_function"))]
#![allow(non_snake_case)]
use crate::dynamic::Variant;
use crate::stdlib::vec::Vec;
use crate::{Dynamic, StaticVec};
/// Trait that parses arguments to a function call.
///
/// Any data type can implement this trait in order to pass arguments to a function call.
pub trait FuncArgs {
/// Parse function call arguments into a container.
///
/// # Example
///
/// ```
/// use rhai::{Engine, Dynamic, FuncArgs, Scope};
///
/// // A struct containing function arguments
/// struct Options {
/// pub foo: bool,
/// pub bar: String,
/// pub baz: i64,
/// }
///
/// impl FuncArgs for Options {
/// fn parse<CONTAINER: Extend<Dynamic>>(self, container: &mut CONTAINER) {
/// container.extend(std::iter::once(self.foo.into()));
/// container.extend(std::iter::once(self.bar.into()));
/// container.extend(std::iter::once(self.baz.into()));
/// }
/// }
///
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// let options = Options { foo: false, bar: "world".to_string(), baz: 42 };
///
/// let engine = Engine::new();
/// let mut scope = Scope::new();
///
/// let ast = engine.compile(r#"
/// fn hello(x, y, z) {
/// if x { "hello " + y } else { y + z }
/// }
/// "#)?;
///
/// let result: String = engine.call_fn(&mut scope, &ast, "hello", options)?;
///
/// assert_eq!(result, "world42");
/// # Ok(())
/// # }
/// ```
fn parse<CONTAINER: Extend<Dynamic>>(self, container: &mut CONTAINER);
}
impl<T: Variant + Clone> FuncArgs for Vec<T> {
#[inline(always)]
fn parse<CONTAINER: Extend<Dynamic>>(self, container: &mut CONTAINER) {
container.extend(self.into_iter().map(Variant::into_dynamic));
}
}
/// Macro to implement [`FuncArgs`] for tuples of standard types (each can be
/// converted into a [`Dynamic`]).
macro_rules! impl_args {
($($p:ident),*) => {
impl<$($p: Variant + Clone),*> FuncArgs for ($($p,)*)
{
#[inline(always)]
fn parse<CONTAINER: Extend<Dynamic>>(self, container: &mut CONTAINER) {
let ($($p,)*) = self;
let mut _v = StaticVec::new();
$(_v.push($p.into_dynamic());)*
container.extend(_v.into_iter());
}
}
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);