Skip to main content

Function

Trait Function 

Source
pub trait Function: PartialReflect + Debug {
    // Required methods
    fn name(&self) -> Option<&Cow<'static, str>>;
    fn info(&self) -> &FunctionInfo;
    fn reflect_call<'a>(
        &self,
        args: ArgList<'a>,
    ) -> Result<Return<'a>, FunctionError>;
    fn to_dynamic_function(&self) -> DynamicFunction<'static>;

    // Provided method
    fn arg_count(&self) -> ArgCount { ... }
}
Expand description

A trait used to power function-like operations via reflection.

This trait allows types to be called like regular functions with Reflect-based arguments and return values.

By default, this trait is currently only implemented for DynamicFunction, however, it is possible to implement this trait for custom function-like types.

§Example

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);

Required Methods§

Source

fn name(&self) -> Option<&Cow<'static, str>>

The name of the function, if any.

For DynamicFunctions created using IntoFunction, the default name will always be the full path to the function as returned by core::any::type_name, unless the function is a closure, anonymous function, or function pointer, in which case the name will be None.

Source

fn info(&self) -> &FunctionInfo

The FunctionInfo for this function.

Source

fn reflect_call<'a>( &self, args: ArgList<'a>, ) -> Result<Return<'a>, FunctionError>

Call this function with the given arguments.

Source

fn to_dynamic_function(&self) -> DynamicFunction<'static>

Creates a new DynamicFunction from this function.

Provided Methods§

Source

fn arg_count(&self) -> ArgCount

Returns the number of arguments the function expects.

For overloaded functions that can have a variable number of arguments, this will contain the full set of counts for all signatures.

Implementors§