#[function]
Expand description
Re-exports for procedural macros when the derive
feature is enabled.
§Macros
§#[function]
A procedural macro that transforms a Rust function into a plugin-compatible function. This macro enables the function to be called from plugins and handles serialization of arguments and return values.
§Usage
use plux_rs::prelude::*;
// Basic usage with primitive types
#[plux_rs::function]
fn add(_: (), a: &i32, b: &i32) -> i32 {
a + b
}
// With references for better performance
#[plux_rs::function]
fn concat(_: (), a: &String, b: Vec<&String>) -> String {
let v = b.into_iter().map(|s| s.clone()).collect::<Vec<String>>();
format!("{} {}", a, v.join(" "))
}
// With context parameter (first parameter is always the context)
#[plux_rs::function]
fn greet(message: &String, name: &Variable) -> String {
format!("{} {}", message, name)
}
let mut loader = Loader::<'_, FunctionOutput, StdInfo>::new();
loader
.context(move |mut ctx| {
ctx.register_function(add());
ctx.register_function(concat());
ctx.register_function(greet("Hello world,".to_string()));
Ok::<(), Box<dyn std::error::Error>>(())
})
.unwrap();
let registry = loader.get_registry();
let add_function = registry.get(0).unwrap();
let concat_function = registry.get(1).unwrap();
let greet_function = registry.get(2).unwrap();
let result = add_function.call(&[1.into(), 2.into()]).unwrap().unwrap();
assert_eq!(result, 3.into());
let result = concat_function
.call(&["Hi".into(), vec!["guest", "!"].into()])
.unwrap()
.unwrap();
assert_eq!(result, "Hi guest !".into());
let result = greet_function.call(&["guest".into()]).unwrap().unwrap();
assert_eq!(result, "Hello world, guest".into());
§Features
- Type Safety: Compile-time type checking of function signatures
- Zero-Copy: Always uses references to avoid unnecessary cloning
- Context Support: First parameter can be a context object
§Notes
- The first parameter can be used for context (use
_
if not needed) - Supported parameter types: primitive types,
&T
andVec<&T>
- The function will be available to plugins under its Rust name by default