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)]
|
|
|
|
|
2021-11-13 15:36:23 +01:00
|
|
|
use crate::types::dynamic::Variant;
|
2021-06-29 15:47:55 +02:00
|
|
|
use crate::Dynamic;
|
2021-04-21 04:05:28 +02:00
|
|
|
#[cfg(feature = "no_std")]
|
|
|
|
use std::prelude::v1::*;
|
2020-07-23 12:40:42 +02:00
|
|
|
|
2021-01-28 08:29:55 +01:00
|
|
|
/// Trait that parses arguments to a function call.
|
|
|
|
///
|
|
|
|
/// Any data type can implement this trait in order to pass arguments to a function call.
|
2020-07-23 12:40:42 +02:00
|
|
|
pub trait FuncArgs {
|
2021-01-28 08:29:55 +01:00
|
|
|
/// 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 {
|
2021-03-24 03:02:50 +01:00
|
|
|
/// fn parse<CONTAINER: Extend<Dynamic>>(self, container: &mut CONTAINER) {
|
2021-01-28 08:29:55 +01:00
|
|
|
/// 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>> {
|
2021-11-27 16:29:32 +01:00
|
|
|
/// # #[cfg(not(feature = "no_function"))]
|
|
|
|
/// # {
|
2021-01-28 08:29:55 +01:00
|
|
|
/// let options = Options { foo: false, bar: "world".to_string(), baz: 42 };
|
|
|
|
///
|
|
|
|
/// let engine = Engine::new();
|
|
|
|
/// let mut scope = Scope::new();
|
|
|
|
///
|
2021-06-05 09:26:43 +02:00
|
|
|
/// let ast = engine.compile(
|
|
|
|
/// "
|
|
|
|
/// fn hello(x, y, z) {
|
|
|
|
/// if x { `hello ${y}` } else { y + z }
|
|
|
|
/// }
|
|
|
|
/// ")?;
|
2021-01-28 08:29:55 +01:00
|
|
|
///
|
|
|
|
/// let result: String = engine.call_fn(&mut scope, &ast, "hello", options)?;
|
|
|
|
///
|
|
|
|
/// assert_eq!(result, "world42");
|
2021-11-27 16:29:32 +01:00
|
|
|
/// # }
|
2021-01-28 08:29:55 +01:00
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2021-03-24 03:02:50 +01:00
|
|
|
fn parse<CONTAINER: Extend<Dynamic>>(self, container: &mut CONTAINER);
|
2021-01-28 08:29:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Variant + Clone> FuncArgs for Vec<T> {
|
2021-10-21 11:26:43 +02:00
|
|
|
#[inline]
|
2021-03-24 03:02:50 +01:00
|
|
|
fn parse<CONTAINER: Extend<Dynamic>>(self, container: &mut CONTAINER) {
|
2021-01-28 08:29:55 +01:00
|
|
|
container.extend(self.into_iter().map(Variant::into_dynamic));
|
|
|
|
}
|
2020-07-23 12:40:42 +02:00
|
|
|
}
|
|
|
|
|
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,)*)
|
|
|
|
{
|
2021-10-21 11:26:43 +02:00
|
|
|
#[inline]
|
2021-06-29 15:47:55 +02:00
|
|
|
#[allow(unused_variables)]
|
2021-01-28 08:29:55 +01:00
|
|
|
fn parse<CONTAINER: Extend<Dynamic>>(self, container: &mut CONTAINER) {
|
2020-07-23 12:40:42 +02:00
|
|
|
let ($($p,)*) = self;
|
2021-06-29 15:47:55 +02:00
|
|
|
$(container.extend(Some($p.into_dynamic()));)*
|
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);
|