Skip to main content

Function

Trait Function 

Source
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 string
  • inputs - Returns the list of input arguments
  • output - 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§

Source

type Output: Send + Sync

The output type of the function

Required Methods§

Source

fn name(&self) -> String

Returns the name of the function.

§Returns

Returns the function name as a String.

Source

fn inputs(&self) -> Vec<Arg>

Returns the input arguments of the function.

§Returns

Returns a vector of Arg describing the function’s input parameters.

Source

fn output(&self) -> Option<Arg>

Returns the output argument of the function.

§Returns

Returns Some(Arg) if the function has an output, None for void functions.

Source

fn call(&self, args: &[Variable]) -> Self::Output

Calls the function with the given arguments.

§Parameters
  • args - Slice of Variable arguments to pass to the function
§Returns

Returns the function’s output of type Self::Output.

Trait Implementations§

Source§

impl<O: Send + Sync> Debug for dyn Function<Output = O>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<O: Send + Sync> Display for dyn Function<Output = O>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<O: Send + Sync> Eq for dyn Function<Output = O>

Source§

impl<O: Send + Sync> PartialEq for dyn Function<Output = O>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§