From 1200ffcd2b74e11426d82185fbd1d1a62424bb17 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 17 Mar 2021 22:32:22 +0800 Subject: [PATCH] Add test with &str parameter. --- src/fn_func.rs | 20 ++++++++++---------- tests/call_fn.rs | 8 ++++++++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/fn_func.rs b/src/fn_func.rs index aea3923e..a9576989 100644 --- a/src/fn_func.rs +++ b/src/fn_func.rs @@ -21,9 +21,9 @@ pub trait Func { /// /// ``` /// # fn main() -> Result<(), Box> { - /// use rhai::{Engine, Func}; // use 'Func' for 'create_from_ast' + /// use rhai::{Engine, Func}; // use 'Func' for 'create_from_ast' /// - /// let engine = Engine::new(); // create a new 'Engine' just for this + /// let engine = Engine::new(); // create a new 'Engine' just for this /// /// let ast = engine.compile("fn calc(x, y) { x + len(y) < 42 }")?; /// @@ -32,15 +32,15 @@ pub trait Func { /// // 2) the return type of the script function /// /// // 'func' will have type Box Result>> and is callable! - /// let func = Func::<(i64, String), bool>::create_from_ast( - /// // ^^^^^^^^^^^^^ function parameter types in tuple + /// let func = Func::<(i64, &str), 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 + /// func(123, "hello")? == false; // call the anonymous function /// # Ok(()) /// # } fn create_from_ast(self, ast: AST, entry_point: &str) -> Self::Output; @@ -53,9 +53,9 @@ pub trait Func { /// /// ``` /// # fn main() -> Result<(), Box> { - /// use rhai::{Engine, Func}; // use 'Func' for 'create_from_script' + /// use rhai::{Engine, Func}; // use 'Func' for 'create_from_script' /// - /// let engine = Engine::new(); // create a new 'Engine' just for this + /// let engine = Engine::new(); // create a new 'Engine' just for this /// /// let script = "fn calc(x, y) { x + len(y) < 42 }"; /// @@ -64,15 +64,15 @@ pub trait Func { /// // 2) the return type of the script function /// /// // 'func' will have type Box Result>> and is callable! - /// let func = Func::<(i64, String), bool>::create_from_script( - /// // ^^^^^^^^^^^^^ function parameter types in tuple + /// let func = Func::<(i64, &str), 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 + /// func(123, "hello")? == false; // call the anonymous function /// # Ok(()) /// # } /// ``` diff --git a/tests/call_fn.rs b/tests/call_fn.rs index dbe3ece0..fdf82e88 100644 --- a/tests/call_fn.rs +++ b/tests/call_fn.rs @@ -215,5 +215,13 @@ fn test_anonymous_fn() -> Result<(), Box> { assert_eq!(calc_func(42, "hello".to_string(), 9)?, 423); + let calc_func = Func::<(INT, &str, INT), INT>::create_from_script( + Engine::new(), + "fn calc(x, y, z) { (x + len(y)) * z }", + "calc", + )?; + + assert_eq!(calc_func(42, "hello", 9)?, 423); + Ok(()) }