Attribute Macro rusteval::Function

source ·
#[Function]
Expand description

Gives interactive access to a function.

Can be used in different modules.

This makes use of the inventory crate to submit a wrapper struct to a global registry.

You can gain access to the wrapped function by using #[derive(InteractiveRoot)]. (link)

Since the inventory crate requires std this macro is only available with default features on.

What it does:

#[Function]
fn add_one(a: u32) -> u32 {
    a + 1
}

Expands to something like:


struct FunctionXYZ;
impl Function for FunctionXYZ {
    fn eval(&self, args: &str, f: &mut dyn FnMut(Result<'_, &dyn Debug>)) {
        match parse_1_arg("add_one", args) {
            Ok((arg0,)) => f(Ok(&add_one(arg0))),
            Err(e) => f(Err(e)),
        }
    }
    fn function_name(&self) -> &'static str {
        "add_one"
    }
}
inventory::submit! {
    &FunctionXYZ as &dyn::rusteval::Function
}