use crate::{
func::{ArgList, DynamicFunction, FunctionInfo, FunctionResult},
PartialReflect,
};
use alloc::borrow::Cow;
use core::fmt::Debug;
pub trait Function: PartialReflect + Debug {
fn name(&self) -> Option<&Cow<'static, str>> {
self.info().name()
}
fn arg_count(&self) -> usize {
self.info().arg_count()
}
fn info(&self) -> &FunctionInfo;
fn reflect_call<'a>(&self, args: ArgList<'a>) -> FunctionResult<'a>;
fn clone_dynamic(&self) -> DynamicFunction<'static>;
}
#[cfg(test)]
mod tests {
use super::*;
use crate::func::IntoFunction;
#[test]
fn should_call_dyn_function() {
fn add(a: i32, b: i32) -> i32 {
a + b
}
let func: Box<dyn Function> = Box::new(add.into_function());
let args = ArgList::new().push_owned(25_i32).push_owned(75_i32);
let value = func.reflect_call(args).unwrap().unwrap_owned();
assert_eq!(value.try_take::<i32>().unwrap(), 100);
}
}