Trait rhai::Func[][src]

pub trait Func<ARGS, RET> {
    type Output;
    fn create_from_ast(self, ast: AST, entry_point: &str) -> Self::Output;
fn create_from_script(
        self,
        script: &str,
        entry_point: &str
    ) -> Result<Self::Output, ParseError>; }
Expand description

Trait to create a Rust closure from a script.

Associated Types

Required methods

Create a Rust closure from an AST. The Engine and AST are consumed and basically embedded into the closure.

Examples
use rhai::{Engine, Func};                       // use 'Func' for 'create_from_ast'

let engine = Engine::new();                     // create a new 'Engine' just for this

let ast = engine.compile("fn calc(x, y) { x + len(y) < 42 }")?;

// Func takes two type parameters:
//   1) a tuple made up of the types of the script function's parameters
//   2) the return type of the script function
//
// 'func' will have type Box<dyn Fn(i64, String) -> Result<bool, Box<EvalAltResult>>> and is callable!
let func = Func::<(i64, String), bool>::create_from_ast(
//                ^^^^^^^^^^^^^ function parameter types in tuple

                engine,                         // the 'Engine' is consumed into the closure
                ast,                            // the 'AST'
                "calc"                          // the entry-point function name
);

func(123, "hello".to_string())? == false;       // call the anonymous function

Create a Rust closure from a script. The Engine is consumed and basically embedded into the closure.

Examples
use rhai::{Engine, Func};                       // use 'Func' for 'create_from_script'

let engine = Engine::new();                     // create a new 'Engine' just for this

let script = "fn calc(x, y) { x + len(y) < 42 }";

// Func takes two type parameters:
//   1) a tuple made up of the types of the script function's parameters
//   2) the return type of the script function
//
// 'func' will have type Box<dyn Fn(i64, String) -> Result<bool, Box<EvalAltResult>>> and is callable!
let func = Func::<(i64, String), bool>::create_from_script(
//                ^^^^^^^^^^^^^ function parameter types in tuple

                engine,                         // the 'Engine' is consumed into the closure
                script,                         // the script, notice number of parameters must match
                "calc"                          // the entry-point function name
)?;

func(123, "hello".to_string())? == false;       // call the anonymous function

Implementors