pub struct DynamicFunction { /* private fields */ }
Expand description
A dynamic function that can be called at runtime.
DynamicFunction provides a concrete implementation of the Function trait that wraps a closure. This allows for runtime creation of functions with dynamic behavior.
§Fields
name
- The function nameinputs
- List of input argumentsoutput
- Optional output argumentptr
- The function implementation as a boxed closure
§Example
use plux_rs::function::{DynamicFunction, Arg, FunctionOutput};
use plux_rs::variable::VariableType;
let multiply = DynamicFunction::new(
"multiply",
vec![
Arg::new("x", VariableType::F64),
Arg::new("y", VariableType::F64),
],
Some(Arg::new("result", VariableType::F64)),
|args| -> FunctionOutput {
let x = args[0].parse_ref::<f64>();
let y = args[1].parse_ref::<f64>();
Ok(Some((x * y).into()))
}
);
Implementations§
Source§impl DynamicFunction
impl DynamicFunction
Sourcepub fn new<S, F>(name: S, inputs: Vec<Arg>, output: Option<Arg>, ptr: F) -> Self
pub fn new<S, F>(name: S, inputs: Vec<Arg>, output: Option<Arg>, ptr: F) -> Self
Creates a new dynamic function.
§Parameters
name
- The function name (will be converted to String)inputs
- Vector of input argumentsoutput
- Optional output argument (None for void functions)ptr
- The function implementation as a closure
§Returns
Returns a new DynamicFunction instance.
§Type Parameters
S
- Type that can be converted into String (for the name)F
- Function type that takes &Variable and returns FunctionOutput
§Example
use plux_rs::function::{DynamicFunction, Arg, FunctionOutput};
use plux_rs::variable::VariableType;
let greet = DynamicFunction::new(
"greet",
vec![Arg::new("name", VariableType::String)],
Some(Arg::new("message", VariableType::String)),
|args| -> FunctionOutput {
let name = args[0].parse_ref::<String>();
Ok(Some(format!("Hello, {}!", name).into()))
}
);
Trait Implementations§
Auto Trait Implementations§
impl Freeze for DynamicFunction
impl !RefUnwindSafe for DynamicFunction
impl Send for DynamicFunction
impl Sync for DynamicFunction
impl Unpin for DynamicFunction
impl !UnwindSafe for DynamicFunction
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more