use crate::{
func::{
args::{ArgCount, 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>>;
fn arg_count(&self) -> ArgCount {
self.info().arg_count()
}
fn info(&self) -> &FunctionInfo;
fn reflect_call<'a>(&self, args: ArgList<'a>) -> FunctionResult<'a>;
fn to_dynamic_function(&self) -> DynamicFunction<'static>;
}
#[cfg(test)]
mod tests {
use super::*;
use crate::func::IntoFunction;
use alloc::boxed::Box;
#[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().with_owned(25_i32).with_owned(75_i32);
let value = func.reflect_call(args).unwrap().unwrap_owned();
assert_eq!(value.try_take::<i32>().unwrap(), 100);
}
}