pub trait Function: Send + Sync {
type Output: Send + Sync;
// Required methods
fn name(&self) -> String;
fn inputs(&self) -> Vec<Arg>;
fn output(&self) -> Option<Arg>;
fn call(&self, args: &[Variable]) -> Self::Output;
}
Expand description
Trait for defining callable functions in the plugin system.
The Function trait represents a callable entity that can be invoked with arguments and returns a result. Functions can be registered with the loader and called by plugins.
§Type Parameters
Output
- The return type of the function (must implement Send + Sync)
§Required Methods
name
- Returns the function name as a stringinputs
- Returns the list of input argumentsoutput
- Returns the output argument (if any)call
- Executes the function with the given arguments
§Example
use plux_rs::function::{Function, Arg, DynamicFunction, FunctionOutput};
use plux_rs::variable::{Variable, VariableType};
// Create a simple add function
let add_func = DynamicFunction::new(
"add",
vec![
Arg::new("a", VariableType::I32),
Arg::new("b", VariableType::I32),
],
Some(Arg::new("result", VariableType::I32)),
|args| -> FunctionOutput {
let a = args[0].parse_ref::<i32>();
let b = args[1].parse_ref::<i32>();
Ok(Some((a + b).into()))
}
);
// Call the function
let result = add_func.call(&[1.into(), 2.into()]);
assert_eq!(result.unwrap(), Some(3.into()));
Required Associated Types§
Required Methods§
Sourcefn inputs(&self) -> Vec<Arg>
fn inputs(&self) -> Vec<Arg>
Returns the input arguments of the function.
§Returns
Returns a vector of Arg describing the function’s input parameters.