diff --git a/README.md b/README.md index 8edae03a..7cfdb6d4 100644 --- a/README.md +++ b/README.md @@ -203,7 +203,7 @@ To get going with Rhai, create an instance of the scripting engine via `Engine:: ```rust use rhai::{Engine, EvalAltResult}; -fn main() -> Result<(), EvalAltResult> +fn main() -> Result<(), Box> { let engine = Engine::new(); @@ -324,7 +324,7 @@ let script = "fn calc(x, y) { x + y.len() < 42 }"; // 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 Result> and is callable! +// 'func' will have type Box Result>> and is callable! let func = Func::<(i64, String), bool>::create_from_script( // ^^^^^^^^^^^^^ function parameter types in tuple @@ -340,7 +340,7 @@ schedule_callback(func); // pass it as a callback to anot // Although there is nothing you can't do by manually writing out the closure yourself... let engine = Engine::new(); let ast = engine.compile(script)?; -schedule_callback(Box::new(move |x: i64, y: String| -> Result { +schedule_callback(Box::new(move |x: i64, y: String| -> Result> { engine.call_fn(&mut Scope::new(), &ast, "calc", (x, y)) })); ``` @@ -560,12 +560,12 @@ Traits A number of traits, under the `rhai::` module namespace, provide additional functionalities. -| Trait | Description | Methods | -| ------------------- | --------------------------------------------------------------------------------- | --------------------------------------- | -| `RegisterFn` | Trait for registering functions | `register_fn` | -| `RegisterDynamicFn` | Trait for registering functions returning [`Dynamic`] | `register_dynamic_fn` | -| `RegisterResultFn` | Trait for registering fallible functions returning `Result<`_T_`, EvalAltResult>` | `register_result_fn` | -| `Func` | Trait for creating anonymous functions from script | `create_from_ast`, `create_from_script` | +| Trait | Description | Methods | +| ------------------- | -------------------------------------------------------------------------------------- | --------------------------------------- | +| `RegisterFn` | Trait for registering functions | `register_fn` | +| `RegisterDynamicFn` | Trait for registering functions returning [`Dynamic`] | `register_dynamic_fn` | +| `RegisterResultFn` | Trait for registering fallible functions returning `Result<`_T_`, Box>` | `register_result_fn` | +| `Func` | Trait for creating anonymous functions from script | `create_from_ast`, `create_from_script` | Working with functions ---------------------- @@ -588,7 +588,7 @@ fn get_an_any() -> Dynamic { Dynamic::from(42_i64) } -fn main() -> Result<(), EvalAltResult> +fn main() -> Result<(), Box> { let engine = Engine::new(); @@ -657,18 +657,20 @@ Fallible functions If a function is _fallible_ (i.e. it returns a `Result<_, Error>`), it can be registered with `register_result_fn` (using the `RegisterResultFn` trait). -The function must return `Result<_, EvalAltResult>`. `EvalAltResult` implements `From<&str>` and `From` etc. -and the error text gets converted into `EvalAltResult::ErrorRuntime`. +The function must return `Result<_, Box>`. `Box` implements `From<&str>` and `From` etc. +and the error text gets converted into `Box`. + +The error values are `Box`-ed in order to reduce memory footprint of the error path, which should be hit rarely. ```rust use rhai::{Engine, EvalAltResult, Position}; use rhai::RegisterResultFn; // use 'RegisterResultFn' trait for 'register_result_fn' // Function that may fail -fn safe_divide(x: i64, y: i64) -> Result { +fn safe_divide(x: i64, y: i64) -> Result> { if y == 0 { // Return an error if y is zero - Err("Division by zero!".into()) // short-cut to create EvalAltResult + Err("Division by zero!".into()) // short-cut to create Box } else { Ok(x / y) } @@ -682,7 +684,7 @@ fn main() engine.register_result_fn("divide", safe_divide); if let Err(error) = engine.eval::("divide(40, 0)") { - println!("Error: {:?}", error); // prints ErrorRuntime("Division by zero detected!", (1, 1)") + println!("Error: {:?}", *error); // prints ErrorRuntime("Division by zero detected!", (1, 1)") } } ``` @@ -769,7 +771,7 @@ impl TestStruct { } } -fn main() -> Result<(), EvalAltResult> +fn main() -> Result<(), Box> { let engine = Engine::new(); @@ -933,7 +935,7 @@ threaded through multiple invocations: ```rust use rhai::{Engine, Scope, EvalAltResult}; -fn main() -> Result<(), EvalAltResult> +fn main() -> Result<(), Box> { let engine = Engine::new(); @@ -1794,7 +1796,7 @@ return 123 + 456; // returns 579 Errors and `throw`-ing exceptions -------------------------------- -All of [`Engine`]'s evaluation/consuming methods return `Result` with `EvalAltResult` +All of [`Engine`]'s evaluation/consuming methods return `Result>` with `EvalAltResult` holding error information. To deliberately return an error during an evaluation, use the `throw` keyword. ```rust diff --git a/examples/custom_types_and_methods.rs b/examples/custom_types_and_methods.rs index c912520d..d152dc9b 100644 --- a/examples/custom_types_and_methods.rs +++ b/examples/custom_types_and_methods.rs @@ -16,7 +16,7 @@ impl TestStruct { } #[cfg(not(feature = "no_object"))] -fn main() -> Result<(), EvalAltResult> { +fn main() -> Result<(), Box> { let mut engine = Engine::new(); engine.register_type::(); diff --git a/examples/hello.rs b/examples/hello.rs index e999861f..edec8cb8 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -1,7 +1,7 @@ use rhai::{packages::*, Engine, EvalAltResult, INT}; use std::rc::Rc; -fn main() -> Result<(), EvalAltResult> { +fn main() -> Result<(), Box> { let mut engine = Engine::new_raw(); engine.load_package(ArithmeticPackage::new().get()); diff --git a/examples/no_std.rs b/examples/no_std.rs index ad9d9a8a..ed1647ef 100644 --- a/examples/no_std.rs +++ b/examples/no_std.rs @@ -2,7 +2,7 @@ use rhai::{Engine, EvalAltResult, INT}; -fn main() -> Result<(), EvalAltResult> { +fn main() -> Result<(), Box> { let engine = Engine::new(); let result = engine.eval::("40 + 2")?; diff --git a/examples/repl.rs b/examples/repl.rs index 4c807814..4ae1c44f 100644 --- a/examples/repl.rs +++ b/examples/repl.rs @@ -157,7 +157,7 @@ fn main() { Ok(_) => (), Err(err) => { println!(); - print_error(&input, err); + print_error(&input, *err); println!(); } } diff --git a/examples/reuse_scope.rs b/examples/reuse_scope.rs index ab936656..a762ac8a 100644 --- a/examples/reuse_scope.rs +++ b/examples/reuse_scope.rs @@ -1,6 +1,6 @@ use rhai::{Engine, EvalAltResult, Scope, INT}; -fn main() -> Result<(), EvalAltResult> { +fn main() -> Result<(), Box> { let engine = Engine::new(); let mut scope = Scope::new(); diff --git a/examples/rhai_runner.rs b/examples/rhai_runner.rs index 166e1c3e..2bc273e2 100644 --- a/examples/rhai_runner.rs +++ b/examples/rhai_runner.rs @@ -72,7 +72,7 @@ fn main() { eprintln!("{:=<1$}", "", filename.len()); eprintln!(""); - eprint_error(&contents, err); + eprint_error(&contents, *err); } } } diff --git a/examples/simple_fn.rs b/examples/simple_fn.rs index 796b8114..21f532ae 100644 --- a/examples/simple_fn.rs +++ b/examples/simple_fn.rs @@ -1,6 +1,6 @@ use rhai::{Engine, EvalAltResult, RegisterFn, INT}; -fn main() -> Result<(), EvalAltResult> { +fn main() -> Result<(), Box> { let mut engine = Engine::new(); fn add(x: INT, y: INT) -> INT { diff --git a/src/any.rs b/src/any.rs index cb60384e..9c0e3288 100644 --- a/src/any.rs +++ b/src/any.rs @@ -450,7 +450,7 @@ impl Dynamic { /// Cast the `Dynamic` as the system integer type `INT` and return it. /// Returns the name of the actual type if the cast fails. - pub(crate) fn as_int(&self) -> Result { + pub fn as_int(&self) -> Result { match self.0 { Union::Int(n) => Ok(n), _ => Err(self.type_name()), @@ -459,7 +459,7 @@ impl Dynamic { /// Cast the `Dynamic` as a `bool` and return it. /// Returns the name of the actual type if the cast fails. - pub(crate) fn as_bool(&self) -> Result { + pub fn as_bool(&self) -> Result { match self.0 { Union::Bool(b) => Ok(b), _ => Err(self.type_name()), @@ -468,7 +468,7 @@ impl Dynamic { /// Cast the `Dynamic` as a `char` and return it. /// Returns the name of the actual type if the cast fails. - pub(crate) fn as_char(&self) -> Result { + pub fn as_char(&self) -> Result { match self.0 { Union::Char(n) => Ok(n), _ => Err(self.type_name()), @@ -477,7 +477,7 @@ impl Dynamic { /// Cast the `Dynamic` as a string and return the string slice. /// Returns the name of the actual type if the cast fails. - pub(crate) fn as_str(&self) -> Result<&str, &'static str> { + pub fn as_str(&self) -> Result<&str, &'static str> { match &self.0 { Union::Str(s) => Ok(s), _ => Err(self.type_name()), @@ -486,30 +486,36 @@ impl Dynamic { /// Convert the `Dynamic` into `String` and return it. /// Returns the name of the actual type if the cast fails. - pub(crate) fn take_string(self) -> Result { + pub fn take_string(self) -> Result { match self.0 { Union::Str(s) => Ok(*s), _ => Err(self.type_name()), } } - pub(crate) fn from_unit() -> Self { + /// Create a `Dynamic` from `()`. + pub fn from_unit() -> Self { Self(Union::Unit(())) } - pub(crate) fn from_bool(value: bool) -> Self { + /// Create a `Dynamic` from a `bool`. + pub fn from_bool(value: bool) -> Self { Self(Union::Bool(value)) } - pub(crate) fn from_int(value: INT) -> Self { + /// Create a `Dynamic` from an `INT`. + pub fn from_int(value: INT) -> Self { Self(Union::Int(value)) } + /// Create a `Dynamic` from a `FLOAT`. Not available under `no_float`. #[cfg(not(feature = "no_float"))] pub(crate) fn from_float(value: FLOAT) -> Self { Self(Union::Float(value)) } - pub(crate) fn from_char(value: char) -> Self { + /// Create a `Dynamic` from a `char`. + pub fn from_char(value: char) -> Self { Self(Union::Char(value)) } - pub(crate) fn from_string(value: String) -> Self { + /// Create a `Dynamic` from a `String`. + pub fn from_string(value: String) -> Self { Self(Union::Str(Box::new(value))) } } diff --git a/src/api.rs b/src/api.rs index 71a94b30..86cf39c0 100644 --- a/src/api.rs +++ b/src/api.rs @@ -76,7 +76,7 @@ impl Engine { /// fn update(&mut self, offset: i64) { self.field += offset; } /// } /// - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// use rhai::{Engine, RegisterFn}; /// /// let mut engine = Engine::new(); @@ -116,7 +116,7 @@ impl Engine { /// fn new() -> Self { TestStruct { field: 1 } } /// } /// - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// use rhai::{Engine, RegisterFn}; /// /// let mut engine = Engine::new(); @@ -182,7 +182,7 @@ impl Engine { /// fn get_field(&mut self) -> i64 { self.field } /// } /// - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// use rhai::{Engine, RegisterFn}; /// /// let mut engine = Engine::new(); @@ -224,7 +224,7 @@ impl Engine { /// fn set_field(&mut self, new_val: i64) { self.field = new_val; } /// } /// - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// use rhai::{Engine, RegisterFn}; /// /// let mut engine = Engine::new(); @@ -275,7 +275,7 @@ impl Engine { /// fn set_field(&mut self, new_val: i64) { self.field = new_val; } /// } /// - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// use rhai::{Engine, RegisterFn}; /// /// let mut engine = Engine::new(); @@ -310,7 +310,7 @@ impl Engine { /// # Example /// /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// use rhai::Engine; /// /// let engine = Engine::new(); @@ -324,7 +324,7 @@ impl Engine { /// # Ok(()) /// # } /// ``` - pub fn compile(&self, script: &str) -> Result { + pub fn compile(&self, script: &str) -> Result> { self.compile_with_scope(&Scope::new(), script) } @@ -335,7 +335,7 @@ impl Engine { /// # Example /// /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// # #[cfg(not(feature = "no_optimize"))] /// # { /// use rhai::{Engine, Scope, OptimizationLevel}; @@ -365,7 +365,7 @@ impl Engine { /// # Ok(()) /// # } /// ``` - pub fn compile_with_scope(&self, scope: &Scope, script: &str) -> Result { + pub fn compile_with_scope(&self, scope: &Scope, script: &str) -> Result> { self.compile_with_scope_and_optimization_level(scope, script, self.optimization_level) } @@ -375,22 +375,22 @@ impl Engine { scope: &Scope, script: &str, optimization_level: OptimizationLevel, - ) -> Result { + ) -> Result> { let scripts = [script]; let stream = lex(&scripts); - parse(&mut stream.peekable(), self, scope, optimization_level).map_err(|err| *err) + parse(&mut stream.peekable(), self, scope, optimization_level) } /// Read the contents of a file into a string. #[cfg(not(feature = "no_std"))] - fn read_file(path: PathBuf) -> Result { + fn read_file(path: PathBuf) -> Result> { let mut f = File::open(path.clone()) - .map_err(|err| EvalAltResult::ErrorReadingScriptFile(path.clone(), err))?; + .map_err(|err| Box::new(EvalAltResult::ErrorReadingScriptFile(path.clone(), err)))?; let mut contents = String::new(); f.read_to_string(&mut contents) - .map_err(|err| EvalAltResult::ErrorReadingScriptFile(path.clone(), err))?; + .map_err(|err| Box::new(EvalAltResult::ErrorReadingScriptFile(path.clone(), err)))?; Ok(contents) } @@ -400,7 +400,7 @@ impl Engine { /// # Example /// /// ```no_run - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// use rhai::Engine; /// /// let engine = Engine::new(); @@ -416,7 +416,7 @@ impl Engine { /// # } /// ``` #[cfg(not(feature = "no_std"))] - pub fn compile_file(&self, path: PathBuf) -> Result { + pub fn compile_file(&self, path: PathBuf) -> Result> { self.compile_file_with_scope(&Scope::new(), path) } @@ -427,7 +427,7 @@ impl Engine { /// # Example /// /// ```no_run - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// # #[cfg(not(feature = "no_optimize"))] /// # { /// use rhai::{Engine, Scope, OptimizationLevel}; @@ -455,7 +455,7 @@ impl Engine { &self, scope: &Scope, path: PathBuf, - ) -> Result { + ) -> Result> { Self::read_file(path).and_then(|contents| Ok(self.compile_with_scope(scope, &contents)?)) } @@ -467,7 +467,7 @@ impl Engine { /// # Example /// /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// use rhai::Engine; /// /// let engine = Engine::new(); @@ -483,7 +483,7 @@ impl Engine { /// # } /// ``` #[cfg(not(feature = "no_object"))] - pub fn parse_json(&self, json: &str, has_null: bool) -> Result { + pub fn parse_json(&self, json: &str, has_null: bool) -> Result> { let mut scope = Scope::new(); // Trims the JSON string and add a '#' in front @@ -510,7 +510,7 @@ impl Engine { /// # Example /// /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// use rhai::Engine; /// /// let engine = Engine::new(); @@ -524,7 +524,7 @@ impl Engine { /// # Ok(()) /// # } /// ``` - pub fn compile_expression(&self, script: &str) -> Result { + pub fn compile_expression(&self, script: &str) -> Result> { self.compile_expression_with_scope(&Scope::new(), script) } @@ -537,7 +537,7 @@ impl Engine { /// # Example /// /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// # #[cfg(not(feature = "no_optimize"))] /// # { /// use rhai::{Engine, Scope, OptimizationLevel}; @@ -571,12 +571,11 @@ impl Engine { &self, scope: &Scope, script: &str, - ) -> Result { + ) -> Result> { let scripts = [script]; let stream = lex(&scripts); parse_global_expr(&mut stream.peekable(), self, scope, self.optimization_level) - .map_err(|err| *err) } /// Evaluate a script file. @@ -584,7 +583,7 @@ impl Engine { /// # Example /// /// ```no_run - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// use rhai::Engine; /// /// let engine = Engine::new(); @@ -595,7 +594,7 @@ impl Engine { /// # } /// ``` #[cfg(not(feature = "no_std"))] - pub fn eval_file(&self, path: PathBuf) -> Result { + pub fn eval_file(&self, path: PathBuf) -> Result> { Self::read_file(path).and_then(|contents| self.eval::(&contents)) } @@ -604,7 +603,7 @@ impl Engine { /// # Example /// /// ```no_run - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// use rhai::{Engine, Scope}; /// /// let engine = Engine::new(); @@ -623,7 +622,7 @@ impl Engine { &self, scope: &mut Scope, path: PathBuf, - ) -> Result { + ) -> Result> { Self::read_file(path).and_then(|contents| self.eval_with_scope::(scope, &contents)) } @@ -632,7 +631,7 @@ impl Engine { /// # Example /// /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// use rhai::Engine; /// /// let engine = Engine::new(); @@ -641,7 +640,7 @@ impl Engine { /// # Ok(()) /// # } /// ``` - pub fn eval(&self, script: &str) -> Result { + pub fn eval(&self, script: &str) -> Result> { self.eval_with_scope(&mut Scope::new(), script) } @@ -650,7 +649,7 @@ impl Engine { /// # Example /// /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// use rhai::{Engine, Scope}; /// /// let engine = Engine::new(); @@ -671,7 +670,7 @@ impl Engine { &self, scope: &mut Scope, script: &str, - ) -> Result { + ) -> Result> { // Since the AST will be thrown away afterwards, don't bother to optimize it let ast = self.compile_with_scope_and_optimization_level(scope, script, OptimizationLevel::None)?; @@ -683,7 +682,7 @@ impl Engine { /// # Example /// /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// use rhai::Engine; /// /// let engine = Engine::new(); @@ -692,7 +691,10 @@ impl Engine { /// # Ok(()) /// # } /// ``` - pub fn eval_expression(&self, script: &str) -> Result { + pub fn eval_expression( + &self, + script: &str, + ) -> Result> { self.eval_expression_with_scope(&mut Scope::new(), script) } @@ -701,7 +703,7 @@ impl Engine { /// # Example /// /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// use rhai::{Engine, Scope}; /// /// let engine = Engine::new(); @@ -718,7 +720,7 @@ impl Engine { &self, scope: &mut Scope, script: &str, - ) -> Result { + ) -> Result> { let scripts = [script]; let stream = lex(&scripts); // Since the AST will be thrown away afterwards, don't bother to optimize it @@ -731,7 +733,7 @@ impl Engine { /// # Example /// /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// use rhai::Engine; /// /// let engine = Engine::new(); @@ -744,7 +746,7 @@ impl Engine { /// # Ok(()) /// # } /// ``` - pub fn eval_ast(&self, ast: &AST) -> Result { + pub fn eval_ast(&self, ast: &AST) -> Result> { self.eval_ast_with_scope(&mut Scope::new(), ast) } @@ -753,7 +755,7 @@ impl Engine { /// # Example /// /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// use rhai::{Engine, Scope}; /// /// let engine = Engine::new(); @@ -781,15 +783,16 @@ impl Engine { &self, scope: &mut Scope, ast: &AST, - ) -> Result { - let result = self - .eval_ast_with_scope_raw(scope, ast) - .map_err(|err| *err)?; + ) -> Result> { + let result = self.eval_ast_with_scope_raw(scope, ast)?; let return_type = self.map_type_name(result.type_name()); return result.try_cast::().ok_or_else(|| { - EvalAltResult::ErrorMismatchOutputType(return_type.to_string(), Position::none()) + Box::new(EvalAltResult::ErrorMismatchOutputType( + return_type.to_string(), + Position::none(), + )) }); } @@ -812,7 +815,7 @@ impl Engine { /// Evaluate a file, but throw away the result and only return error (if any). /// Useful for when you don't need the result, but still need to keep track of possible errors. #[cfg(not(feature = "no_std"))] - pub fn consume_file(&self, path: PathBuf) -> Result<(), EvalAltResult> { + pub fn consume_file(&self, path: PathBuf) -> Result<(), Box> { Self::read_file(path).and_then(|contents| self.consume(&contents)) } @@ -823,19 +826,23 @@ impl Engine { &self, scope: &mut Scope, path: PathBuf, - ) -> Result<(), EvalAltResult> { + ) -> Result<(), Box> { Self::read_file(path).and_then(|contents| self.consume_with_scope(scope, &contents)) } /// Evaluate a string, but throw away the result and only return error (if any). /// Useful for when you don't need the result, but still need to keep track of possible errors. - pub fn consume(&self, script: &str) -> Result<(), EvalAltResult> { + pub fn consume(&self, script: &str) -> Result<(), Box> { self.consume_with_scope(&mut Scope::new(), script) } /// Evaluate a string with own scope, but throw away the result and only return error (if any). /// Useful for when you don't need the result, but still need to keep track of possible errors. - pub fn consume_with_scope(&self, scope: &mut Scope, script: &str) -> Result<(), EvalAltResult> { + pub fn consume_with_scope( + &self, + scope: &mut Scope, + script: &str, + ) -> Result<(), Box> { let scripts = [script]; let stream = lex(&scripts); @@ -846,7 +853,7 @@ impl Engine { /// Evaluate an AST, but throw away the result and only return error (if any). /// Useful for when you don't need the result, but still need to keep track of possible errors. - pub fn consume_ast(&self, ast: &AST) -> Result<(), EvalAltResult> { + pub fn consume_ast(&self, ast: &AST) -> Result<(), Box> { self.consume_ast_with_scope(&mut Scope::new(), ast) } @@ -856,7 +863,7 @@ impl Engine { &self, scope: &mut Scope, ast: &AST, - ) -> Result<(), EvalAltResult> { + ) -> Result<(), Box> { ast.0 .iter() .try_fold(Dynamic::from_unit(), |_, stmt| { @@ -865,7 +872,7 @@ impl Engine { .map_or_else( |err| match *err { EvalAltResult::Return(_, _) => Ok(()), - err => Err(err), + err => Err(Box::new(err)), }, |_| Ok(()), ) @@ -876,7 +883,7 @@ impl Engine { /// # Example /// /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// # #[cfg(not(feature = "no_function"))] /// # { /// use rhai::{Engine, Scope}; @@ -913,21 +920,22 @@ impl Engine { ast: &AST, name: &str, args: A, - ) -> Result { + ) -> Result> { let mut arg_values = args.into_vec(); let mut args: Vec<_> = arg_values.iter_mut().collect(); let fn_lib = Some(ast.1.as_ref()); let pos = Position::none(); - let result = self - .call_fn_raw(Some(scope), fn_lib, name, &mut args, None, pos, 0) - .map_err(|err| *err)?; + let result = self.call_fn_raw(Some(scope), fn_lib, name, &mut args, None, pos, 0)?; let return_type = self.map_type_name(result.type_name()); - return result - .try_cast() - .ok_or_else(|| EvalAltResult::ErrorMismatchOutputType(return_type.into(), pos)); + return result.try_cast().ok_or_else(|| { + Box::new(EvalAltResult::ErrorMismatchOutputType( + return_type.into(), + pos, + )) + }); } /// Optimize the `AST` with constants defined in an external Scope. @@ -961,7 +969,7 @@ impl Engine { /// # Example /// /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// # use std::sync::RwLock; /// # use std::sync::Arc; /// use rhai::Engine; @@ -989,7 +997,7 @@ impl Engine { /// # Example /// /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// # use std::sync::RwLock; /// # use std::sync::Arc; /// use rhai::Engine; @@ -1046,7 +1054,7 @@ impl Engine { /// # Example /// /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// # use std::sync::RwLock; /// # use std::sync::Arc; /// use rhai::Engine; diff --git a/src/engine.rs b/src/engine.rs index 340dd7d9..d846c2d7 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -205,7 +205,7 @@ impl DerefMut for FunctionsLib { /// Rhai main scripting engine. /// /// ``` -/// # fn main() -> Result<(), rhai::EvalAltResult> { +/// # fn main() -> Result<(), Box> { /// use rhai::Engine; /// /// let engine = Engine::new(); @@ -1383,13 +1383,11 @@ impl Engine { // Compile the script text // No optimizations because we only run it once - let mut ast = self - .compile_with_scope_and_optimization_level( - &Scope::new(), - script, - OptimizationLevel::None, - ) - .map_err(|err| EvalAltResult::ErrorParsing(Box::new(err)))?; + let mut ast = self.compile_with_scope_and_optimization_level( + &Scope::new(), + script, + OptimizationLevel::None, + )?; // If new functions are defined within the eval string, it is an error if ast.1.len() > 0 { diff --git a/src/fn_func.rs b/src/fn_func.rs index 4c3544de..b9add7e4 100644 --- a/src/fn_func.rs +++ b/src/fn_func.rs @@ -21,18 +21,18 @@ pub trait Func { /// # Examples /// /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// 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 + y.len() < 42 }")?; + /// 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 Result> and is callable! + /// // 'func' will have type Box Result>> and is callable! /// let func = Func::<(i64, String), bool>::create_from_ast( /// // ^^^^^^^^^^^^^ function parameter types in tuple /// @@ -52,18 +52,18 @@ pub trait Func { /// # Examples /// /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// 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 + y.len() < 42 }"; + /// 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 Result> and is callable! + /// // 'func' will have type Box Result>> and is callable! /// let func = Func::<(i64, String), bool>::create_from_script( /// // ^^^^^^^^^^^^^ function parameter types in tuple /// @@ -80,7 +80,7 @@ pub trait Func { self, script: &str, entry_point: &str, - ) -> Result; + ) -> Result>; } macro_rules! def_anonymous_fn { @@ -91,10 +91,10 @@ macro_rules! def_anonymous_fn { impl<$($par: Variant + Clone,)* RET: Variant + Clone> Func<($($par,)*), RET> for Engine { #[cfg(feature = "sync")] - type Output = Box Result + Send + Sync>; + type Output = Box Result> + Send + Sync>; #[cfg(not(feature = "sync"))] - type Output = Box Result>; + type Output = Box Result>>; fn create_from_ast(self, ast: AST, entry_point: &str) -> Self::Output { let name = entry_point.to_string(); @@ -104,7 +104,7 @@ macro_rules! def_anonymous_fn { }) } - fn create_from_script(self, script: &str, entry_point: &str) -> Result { + fn create_from_script(self, script: &str, entry_point: &str) -> Result> { let ast = self.compile(script)?; Ok(Func::<($($par,)*), RET>::create_from_ast(self, ast, entry_point)) } diff --git a/src/fn_register.rs b/src/fn_register.rs index 393bda5a..4e490b67 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -16,7 +16,7 @@ pub trait RegisterFn { /// # Example /// /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// use rhai::{Engine, RegisterFn}; /// /// // Normal function @@ -48,7 +48,7 @@ pub trait RegisterDynamicFn { /// # Example /// /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// use rhai::{Engine, Dynamic, RegisterDynamicFn}; /// /// // Function that returns a Dynamic value @@ -68,7 +68,7 @@ pub trait RegisterDynamicFn { fn register_dynamic_fn(&mut self, name: &str, f: FN); } -/// A trait to register fallible custom functions returning Result<_, EvalAltResult> with the `Engine`. +/// A trait to register fallible custom functions returning `Result<_, Box>` with the `Engine`. pub trait RegisterResultFn { /// Register a custom fallible function with the `Engine`. /// @@ -78,9 +78,9 @@ pub trait RegisterResultFn { /// use rhai::{Engine, RegisterResultFn, EvalAltResult}; /// /// // Normal function - /// fn div(x: i64, y: i64) -> Result { + /// fn div(x: i64, y: i64) -> Result> { /// if y == 0 { - /// // '.into()' automatically converts to 'EvalAltResult::ErrorRuntime' + /// // '.into()' automatically converts to 'Box' /// Err("division by zero!".into()) /// } else { /// Ok(x / y) @@ -180,10 +180,10 @@ pub fn map_identity(data: Dynamic, _pos: Position) -> Result mapping function. +/// To `Result>` mapping function. #[inline] pub fn map_result( - data: Result, + data: Result>, pos: Position, ) -> Result> { data.map(|v| v.into_dynamic()) @@ -241,9 +241,9 @@ macro_rules! def_register { $($par: Variant + Clone,)* #[cfg(feature = "sync")] - FN: Fn($($param),*) -> Result + Send + Sync + 'static, + FN: Fn($($param),*) -> Result> + Send + Sync + 'static, #[cfg(not(feature = "sync"))] - FN: Fn($($param),*) -> Result + 'static, + FN: Fn($($param),*) -> Result> + 'static, RET: Variant + Clone > RegisterResultFn for Engine diff --git a/src/lib.rs b/src/lib.rs index 3729e1f4..f7fd783c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ //! ```,no_run //! use rhai::{Engine, EvalAltResult, RegisterFn}; //! -//! fn main() -> Result<(), EvalAltResult> +//! fn main() -> Result<(), Box> //! { //! // Define external function //! fn compute_something(x: i64) -> bool { @@ -71,7 +71,7 @@ extern crate alloc; mod any; mod api; -mod builtin; +//mod builtin; mod engine; mod error; mod fn_call; diff --git a/src/packages/arithmetic.rs b/src/packages/arithmetic.rs index 24bf9687..3ccbf5e7 100644 --- a/src/packages/arithmetic.rs +++ b/src/packages/arithmetic.rs @@ -22,67 +22,73 @@ use crate::stdlib::{ }; // Checked add -fn add(x: T, y: T) -> Result { +fn add(x: T, y: T) -> Result> { x.checked_add(&y).ok_or_else(|| { - EvalAltResult::ErrorArithmetic( + Box::new(EvalAltResult::ErrorArithmetic( format!("Addition overflow: {} + {}", x, y), Position::none(), - ) + )) }) } // Checked subtract -fn sub(x: T, y: T) -> Result { +fn sub(x: T, y: T) -> Result> { x.checked_sub(&y).ok_or_else(|| { - EvalAltResult::ErrorArithmetic( + Box::new(EvalAltResult::ErrorArithmetic( format!("Subtraction underflow: {} - {}", x, y), Position::none(), - ) + )) }) } // Checked multiply -fn mul(x: T, y: T) -> Result { +fn mul(x: T, y: T) -> Result> { x.checked_mul(&y).ok_or_else(|| { - EvalAltResult::ErrorArithmetic( + Box::new(EvalAltResult::ErrorArithmetic( format!("Multiplication overflow: {} * {}", x, y), Position::none(), - ) + )) }) } // Checked divide -fn div(x: T, y: T) -> Result +fn div(x: T, y: T) -> Result> where T: Display + CheckedDiv + PartialEq + Zero, { // Detect division by zero if y == T::zero() { - return Err(EvalAltResult::ErrorArithmetic( + return Err(Box::new(EvalAltResult::ErrorArithmetic( format!("Division by zero: {} / {}", x, y), Position::none(), - )); + ))); } x.checked_div(&y).ok_or_else(|| { - EvalAltResult::ErrorArithmetic( + Box::new(EvalAltResult::ErrorArithmetic( format!("Division overflow: {} / {}", x, y), Position::none(), - ) + )) }) } // Checked negative - e.g. -(i32::MIN) will overflow i32::MAX -fn neg(x: T) -> Result { +fn neg(x: T) -> Result> { x.checked_neg().ok_or_else(|| { - EvalAltResult::ErrorArithmetic(format!("Negation overflow: -{}", x), Position::none()) + Box::new(EvalAltResult::ErrorArithmetic( + format!("Negation overflow: -{}", x), + Position::none(), + )) }) } // Checked absolute -fn abs(x: T) -> Result { +fn abs(x: T) -> Result> { // FIX - We don't use Signed::abs() here because, contrary to documentation, it panics // when the number is ::MIN instead of returning ::MIN itself. if x >= ::zero() { Ok(x) } else { x.checked_neg().ok_or_else(|| { - EvalAltResult::ErrorArithmetic(format!("Negation overflow: -{}", x), Position::none()) + Box::new(EvalAltResult::ErrorArithmetic( + format!("Negation overflow: -{}", x), + Position::none(), + )) }) } } @@ -129,37 +135,37 @@ fn binary_xor(x: T, y: T) -> ::Output { x ^ y } // Checked left-shift -fn shl(x: T, y: INT) -> Result { +fn shl(x: T, y: INT) -> Result> { // Cannot shift by a negative number of bits if y < 0 { - return Err(EvalAltResult::ErrorArithmetic( + return Err(Box::new(EvalAltResult::ErrorArithmetic( format!("Left-shift by a negative number: {} << {}", x, y), Position::none(), - )); + ))); } CheckedShl::checked_shl(&x, y as u32).ok_or_else(|| { - EvalAltResult::ErrorArithmetic( + Box::new(EvalAltResult::ErrorArithmetic( format!("Left-shift by too many bits: {} << {}", x, y), Position::none(), - ) + )) }) } // Checked right-shift -fn shr(x: T, y: INT) -> Result { +fn shr(x: T, y: INT) -> Result> { // Cannot shift by a negative number of bits if y < 0 { - return Err(EvalAltResult::ErrorArithmetic( + return Err(Box::new(EvalAltResult::ErrorArithmetic( format!("Right-shift by a negative number: {} >> {}", x, y), Position::none(), - )); + ))); } CheckedShr::checked_shr(&x, y as u32).ok_or_else(|| { - EvalAltResult::ErrorArithmetic( + Box::new(EvalAltResult::ErrorArithmetic( format!("Right-shift by too many bits: {} % {}", x, y), Position::none(), - ) + )) }) } // Unchecked left-shift - may panic if shifting by a negative number of bits @@ -171,12 +177,12 @@ fn shr_u>(x: T, y: T) -> >::Output { x.shr(y) } // Checked modulo -fn modulo(x: T, y: T) -> Result { +fn modulo(x: T, y: T) -> Result> { x.checked_rem(&y).ok_or_else(|| { - EvalAltResult::ErrorArithmetic( + Box::new(EvalAltResult::ErrorArithmetic( format!("Modulo division by zero or overflow: {} % {}", x, y), Position::none(), - ) + )) }) } // Unchecked modulo - may panic if dividing by zero @@ -184,25 +190,25 @@ fn modulo_u(x: T, y: T) -> ::Output { x % y } // Checked power -fn pow_i_i(x: INT, y: INT) -> Result { +fn pow_i_i(x: INT, y: INT) -> Result> { #[cfg(not(feature = "only_i32"))] { if y > (u32::MAX as INT) { - Err(EvalAltResult::ErrorArithmetic( + Err(Box::new(EvalAltResult::ErrorArithmetic( format!("Integer raised to too large an index: {} ~ {}", x, y), Position::none(), - )) + ))) } else if y < 0 { - Err(EvalAltResult::ErrorArithmetic( + Err(Box::new(EvalAltResult::ErrorArithmetic( format!("Integer raised to a negative index: {} ~ {}", x, y), Position::none(), - )) + ))) } else { x.checked_pow(y as u32).ok_or_else(|| { - EvalAltResult::ErrorArithmetic( + Box::new(EvalAltResult::ErrorArithmetic( format!("Power overflow: {} ~ {}", x, y), Position::none(), - ) + )) }) } } @@ -210,16 +216,16 @@ fn pow_i_i(x: INT, y: INT) -> Result { #[cfg(feature = "only_i32")] { if y < 0 { - Err(EvalAltResult::ErrorArithmetic( + Err(Box::new(EvalAltResult::ErrorArithmetic( format!("Integer raised to a negative index: {} ~ {}", x, y), Position::none(), - )) + ))) } else { x.checked_pow(y as u32).ok_or_else(|| { - EvalAltResult::ErrorArithmetic( + Box::new(EvalAltResult::ErrorArithmetic( format!("Power overflow: {} ~ {}", x, y), Position::none(), - ) + )) }) } } @@ -235,13 +241,13 @@ fn pow_f_f(x: FLOAT, y: FLOAT) -> FLOAT { } // Checked power #[cfg(not(feature = "no_float"))] -fn pow_f_i(x: FLOAT, y: INT) -> Result { +fn pow_f_i(x: FLOAT, y: INT) -> Result> { // Raise to power that is larger than an i32 if y > (i32::MAX as INT) { - return Err(EvalAltResult::ErrorArithmetic( + return Err(Box::new(EvalAltResult::ErrorArithmetic( format!("Number raised to too large an index: {} ~ {}", x, y), Position::none(), - )); + ))); } Ok(x.powi(y as i32)) diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index e6b3fd1a..2c4c342c 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -1,7 +1,7 @@ use super::{reg_binary, reg_binary_mut, reg_trinary_mut, reg_unary_mut}; -use crate::def_package; use crate::any::{Dynamic, Variant}; +use crate::def_package; use crate::engine::Array; use crate::fn_register::{map_dynamic as map, map_identity as pass}; use crate::parser::INT; diff --git a/src/packages/iter_basic.rs b/src/packages/iter_basic.rs index eefc9e0f..830193eb 100644 --- a/src/packages/iter_basic.rs +++ b/src/packages/iter_basic.rs @@ -1,7 +1,7 @@ use super::{reg_binary, reg_trinary, reg_unary_mut, PackageStore}; -use crate::def_package; use crate::any::{Dynamic, Union, Variant}; +use crate::def_package; use crate::engine::{Array, Map}; use crate::fn_register::map_dynamic as map; use crate::parser::INT; diff --git a/src/packages/map_basic.rs b/src/packages/map_basic.rs index b1fb3706..72f6a698 100644 --- a/src/packages/map_basic.rs +++ b/src/packages/map_basic.rs @@ -1,7 +1,7 @@ use super::{reg_binary, reg_binary_mut, reg_unary_mut}; -use crate::def_package; use crate::any::Dynamic; +use crate::def_package; use crate::engine::Map; use crate::fn_register::map_dynamic as map; use crate::parser::INT; diff --git a/src/packages/math_basic.rs b/src/packages/math_basic.rs index eee708c9..72b953c0 100644 --- a/src/packages/math_basic.rs +++ b/src/packages/math_basic.rs @@ -95,10 +95,10 @@ def_package!(BasicMathPackage:"Basic mathematic functions.", lib, { "to_int", |x: f32| { if x > (MAX_INT as f32) { - return Err(EvalAltResult::ErrorArithmetic( + return Err(Box::new(EvalAltResult::ErrorArithmetic( format!("Integer overflow: to_int({})", x), Position::none(), - )); + ))); } Ok(x.trunc() as INT) @@ -110,10 +110,10 @@ def_package!(BasicMathPackage:"Basic mathematic functions.", lib, { "to_int", |x: FLOAT| { if x > (MAX_INT as FLOAT) { - return Err(EvalAltResult::ErrorArithmetic( + return Err(Box::new(EvalAltResult::ErrorArithmetic( format!("Integer overflow: to_int({})", x), Position::none(), - )); + ))); } Ok(x.trunc() as INT) diff --git a/src/packages/mod.rs b/src/packages/mod.rs index 25318d23..611dfef8 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -18,9 +18,11 @@ mod time_basic; mod utils; pub use arithmetic::ArithmeticPackage; +#[cfg(not(feature = "no_index"))] pub use array_basic::BasicArrayPackage; pub use iter_basic::BasicIteratorPackage; pub use logic::LogicPackage; +#[cfg(not(feature = "no_object"))] pub use map_basic::BasicMapPackage; pub use math_basic::BasicMathPackage; pub use pkg_core::CorePackage; diff --git a/src/packages/pkg_std.rs b/src/packages/pkg_std.rs index f17c275e..51aa9dc0 100644 --- a/src/packages/pkg_std.rs +++ b/src/packages/pkg_std.rs @@ -1,4 +1,6 @@ +#[cfg(not(feature = "no_index"))] use super::array_basic::BasicArrayPackage; +#[cfg(not(feature = "no_object"))] use super::map_basic::BasicMapPackage; use super::math_basic::BasicMathPackage; use super::pkg_core::CorePackage; diff --git a/src/packages/time_basic.rs b/src/packages/time_basic.rs index 91d5c9bc..13faad95 100644 --- a/src/packages/time_basic.rs +++ b/src/packages/time_basic.rs @@ -31,13 +31,13 @@ def_package!(BasicTimePackage:"Basic timing utilities.", lib, { #[cfg(not(feature = "unchecked"))] { if seconds > (MAX_INT as u64) { - return Err(EvalAltResult::ErrorArithmetic( + return Err(Box::new(EvalAltResult::ErrorArithmetic( format!( "Integer overflow for timestamp duration: {}", -(seconds as i64) ), Position::none(), - )); + ))); } } return Ok(-(seconds as INT)); @@ -53,10 +53,10 @@ def_package!(BasicTimePackage:"Basic timing utilities.", lib, { #[cfg(not(feature = "unchecked"))] { if seconds > (MAX_INT as u64) { - return Err(EvalAltResult::ErrorArithmetic( + return Err(Box::new(EvalAltResult::ErrorArithmetic( format!("Integer overflow for timestamp duration: {}", seconds), Position::none(), - )); + ))); } } return Ok(seconds as INT); diff --git a/src/parser.rs b/src/parser.rs index c77262a9..ea7b9a3c 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -72,7 +72,7 @@ impl AST { /// # Example /// /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { + /// # fn main() -> Result<(), Box> { /// # #[cfg(not(feature = "no_function"))] /// # { /// use rhai::Engine; diff --git a/src/result.rs b/src/result.rs index 21fe7ef8..4a4cb6bc 100644 --- a/src/result.rs +++ b/src/result.rs @@ -228,20 +228,23 @@ impl fmt::Display for EvalAltResult { } } -impl From for EvalAltResult { +impl From for Box { fn from(err: ParseError) -> Self { - Self::ErrorParsing(Box::new(err)) + Box::new(EvalAltResult::ErrorParsing(Box::new(err))) } } -impl From> for EvalAltResult { +impl From> for Box { fn from(err: Box) -> Self { - Self::ErrorParsing(err) + Box::new(EvalAltResult::ErrorParsing(err)) } } -impl> From for EvalAltResult { +impl> From for Box { fn from(err: T) -> Self { - Self::ErrorRuntime(err.as_ref().to_string(), Position::none()) + Box::new(EvalAltResult::ErrorRuntime( + err.as_ref().to_string(), + Position::none(), + )) } } diff --git a/src/scope.rs b/src/scope.rs index a9fd579c..6abfb40f 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -42,7 +42,7 @@ pub(crate) struct EntryRef<'a> { /// # Example /// /// ``` -/// # fn main() -> Result<(), rhai::EvalAltResult> { +/// # fn main() -> Result<(), Box> { /// use rhai::{Engine, Scope}; /// /// let engine = Engine::new(); diff --git a/tests/arrays.rs b/tests/arrays.rs index a41f0a63..60a41be4 100644 --- a/tests/arrays.rs +++ b/tests/arrays.rs @@ -2,7 +2,7 @@ use rhai::{Array, Engine, EvalAltResult, RegisterFn, INT}; #[test] -fn test_arrays() -> Result<(), EvalAltResult> { +fn test_arrays() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("let x = [1, 2, 3]; x[1]")?, 2); @@ -58,7 +58,7 @@ fn test_arrays() -> Result<(), EvalAltResult> { #[test] #[cfg(not(feature = "no_object"))] -fn test_array_with_structs() -> Result<(), EvalAltResult> { +fn test_array_with_structs() -> Result<(), Box> { #[derive(Clone)] struct TestStruct { x: INT, diff --git a/tests/binary_ops.rs b/tests/binary_ops.rs index 922c6a43..7ede89fb 100644 --- a/tests/binary_ops.rs +++ b/tests/binary_ops.rs @@ -1,7 +1,7 @@ use rhai::{Engine, EvalAltResult, INT}; #[test] -fn test_binary_ops() -> Result<(), EvalAltResult> { +fn test_binary_ops() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("10 % 4")?, 2); diff --git a/tests/bit_shift.rs b/tests/bit_shift.rs index e8e360f3..0b888e32 100644 --- a/tests/bit_shift.rs +++ b/tests/bit_shift.rs @@ -1,14 +1,14 @@ use rhai::{Engine, EvalAltResult, INT}; #[test] -fn test_left_shift() -> Result<(), EvalAltResult> { +fn test_left_shift() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("4 << 2")?, 16); Ok(()) } #[test] -fn test_right_shift() -> Result<(), EvalAltResult> { +fn test_right_shift() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("9 >> 1")?, 4); Ok(()) diff --git a/tests/bool_op.rs b/tests/bool_op.rs index 3bd5859a..913106e6 100644 --- a/tests/bool_op.rs +++ b/tests/bool_op.rs @@ -1,7 +1,7 @@ use rhai::{Engine, EvalAltResult}; #[test] -fn test_bool_op1() -> Result<(), EvalAltResult> { +fn test_bool_op1() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("true && (false || true)")?, true); @@ -11,7 +11,7 @@ fn test_bool_op1() -> Result<(), EvalAltResult> { } #[test] -fn test_bool_op2() -> Result<(), EvalAltResult> { +fn test_bool_op2() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("false && (false || true)")?, false); @@ -21,7 +21,7 @@ fn test_bool_op2() -> Result<(), EvalAltResult> { } #[test] -fn test_bool_op3() -> Result<(), EvalAltResult> { +fn test_bool_op3() -> Result<(), Box> { let engine = Engine::new(); assert!(engine.eval::("true && (false || 123)").is_err()); @@ -33,7 +33,7 @@ fn test_bool_op3() -> Result<(), EvalAltResult> { } #[test] -fn test_bool_op_short_circuit() -> Result<(), EvalAltResult> { +fn test_bool_op_short_circuit() -> Result<(), Box> { let engine = Engine::new(); assert_eq!( diff --git a/tests/call_fn.rs b/tests/call_fn.rs index fc658a15..f3d2a689 100644 --- a/tests/call_fn.rs +++ b/tests/call_fn.rs @@ -2,7 +2,7 @@ use rhai::{Engine, EvalAltResult, Func, ParseErrorType, Scope, INT}; #[test] -fn test_fn() -> Result<(), EvalAltResult> { +fn test_fn() -> Result<(), Box> { let engine = Engine::new(); // Expect duplicated parameters error @@ -19,7 +19,7 @@ fn test_fn() -> Result<(), EvalAltResult> { } #[test] -fn test_call_fn() -> Result<(), EvalAltResult> { +fn test_call_fn() -> Result<(), Box> { let engine = Engine::new(); let mut scope = Scope::new(); @@ -61,7 +61,7 @@ fn test_call_fn() -> Result<(), EvalAltResult> { } #[test] -fn test_anonymous_fn() -> Result<(), EvalAltResult> { +fn test_anonymous_fn() -> Result<(), Box> { let calc_func = Func::<(INT, INT, INT), INT>::create_from_script( Engine::new(), "fn calc(x, y, z) { (x + y) * z }", diff --git a/tests/chars.rs b/tests/chars.rs index db13f840..6882633a 100644 --- a/tests/chars.rs +++ b/tests/chars.rs @@ -1,7 +1,7 @@ use rhai::{Engine, EvalAltResult}; #[test] -fn test_chars() -> Result<(), EvalAltResult> { +fn test_chars() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("'y'")?, 'y'); diff --git a/tests/compound_equality.rs b/tests/compound_equality.rs index b516bb97..b09115a0 100644 --- a/tests/compound_equality.rs +++ b/tests/compound_equality.rs @@ -1,7 +1,7 @@ use rhai::{Engine, EvalAltResult, INT}; #[test] -fn test_or_equals() -> Result<(), EvalAltResult> { +fn test_or_equals() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("let x = 16; x |= 74; x")?, 90); @@ -12,7 +12,7 @@ fn test_or_equals() -> Result<(), EvalAltResult> { } #[test] -fn test_and_equals() -> Result<(), EvalAltResult> { +fn test_and_equals() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("let x = 16; x &= 31; x")?, 16); @@ -24,42 +24,42 @@ fn test_and_equals() -> Result<(), EvalAltResult> { } #[test] -fn test_xor_equals() -> Result<(), EvalAltResult> { +fn test_xor_equals() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("let x = 90; x ^= 12; x")?, 86); Ok(()) } #[test] -fn test_multiply_equals() -> Result<(), EvalAltResult> { +fn test_multiply_equals() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("let x = 2; x *= 3; x")?, 6); Ok(()) } #[test] -fn test_divide_equals() -> Result<(), EvalAltResult> { +fn test_divide_equals() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("let x = 6; x /= 2; x")?, 3); Ok(()) } #[test] -fn test_left_shift_equals() -> Result<(), EvalAltResult> { +fn test_left_shift_equals() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("let x = 9; x >>=1; x")?, 4); Ok(()) } #[test] -fn test_right_shift_equals() -> Result<(), EvalAltResult> { +fn test_right_shift_equals() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("let x = 4; x<<= 2; x")?, 16); Ok(()) } #[test] -fn test_modulo_equals() -> Result<(), EvalAltResult> { +fn test_modulo_equals() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("let x = 10; x %= 4; x")?, 2); Ok(()) diff --git a/tests/constants.rs b/tests/constants.rs index 2dd62000..55752d28 100644 --- a/tests/constants.rs +++ b/tests/constants.rs @@ -1,21 +1,21 @@ use rhai::{Engine, EvalAltResult, INT}; #[test] -fn test_constant() -> Result<(), EvalAltResult> { +fn test_constant() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("const x = 123; x")?, 123); - assert!( - matches!(engine.eval::("const x = 123; x = 42;").expect_err("expects error"), - EvalAltResult::ErrorAssignmentToConstant(var, _) if var == "x") - ); + assert!(matches!( + *engine.eval::("const x = 123; x = 42;").expect_err("expects error"), + EvalAltResult::ErrorAssignmentToConstant(var, _) if var == "x" + )); #[cfg(not(feature = "no_index"))] - assert!( - matches!(engine.eval::("const x = [1, 2, 3, 4, 5]; x[2] = 42;").expect_err("expects error"), - EvalAltResult::ErrorAssignmentToConstant(var, _) if var == "x") - ); + assert!(matches!( + *engine.eval::("const x = [1, 2, 3, 4, 5]; x[2] = 42;").expect_err("expects error"), + EvalAltResult::ErrorAssignmentToConstant(var, _) if var == "x" + )); Ok(()) } diff --git a/tests/decrement.rs b/tests/decrement.rs index a8cb2634..ab1f9376 100644 --- a/tests/decrement.rs +++ b/tests/decrement.rs @@ -1,14 +1,15 @@ use rhai::{Engine, EvalAltResult, INT}; #[test] -fn test_decrement() -> Result<(), EvalAltResult> { +fn test_decrement() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("let x = 10; x -= 7; x")?, 3); - assert!(matches!(engine - .eval::(r#"let s = "test"; s -= "ing"; s"#) - .expect_err("expects error"), EvalAltResult::ErrorFunctionNotFound(err, _) if err == "- (string, string)")); + assert!(matches!( + *engine.eval::(r#"let s = "test"; s -= "ing"; s"#).expect_err("expects error"), + EvalAltResult::ErrorFunctionNotFound(err, _) if err == "- (string, string)" + )); Ok(()) } diff --git a/tests/eval.rs b/tests/eval.rs index bf6f750f..c9027a25 100644 --- a/tests/eval.rs +++ b/tests/eval.rs @@ -1,7 +1,7 @@ use rhai::{Engine, EvalAltResult, Scope, INT}; #[test] -fn test_eval() -> Result<(), EvalAltResult> { +fn test_eval() -> Result<(), Box> { let engine = Engine::new(); assert_eq!( @@ -18,7 +18,7 @@ fn test_eval() -> Result<(), EvalAltResult> { #[test] #[cfg(not(feature = "no_function"))] -fn test_eval_function() -> Result<(), EvalAltResult> { +fn test_eval_function() -> Result<(), Box> { let engine = Engine::new(); let mut scope = Scope::new(); @@ -61,7 +61,7 @@ fn test_eval_function() -> Result<(), EvalAltResult> { #[test] #[cfg(not(feature = "no_function"))] -fn test_eval_override() -> Result<(), EvalAltResult> { +fn test_eval_override() -> Result<(), Box> { let engine = Engine::new(); assert_eq!( diff --git a/tests/expressions.rs b/tests/expressions.rs index 2091fe4e..35a6b90c 100644 --- a/tests/expressions.rs +++ b/tests/expressions.rs @@ -1,7 +1,7 @@ use rhai::{Engine, EvalAltResult, Scope, INT}; #[test] -fn test_expressions() -> Result<(), EvalAltResult> { +fn test_expressions() -> Result<(), Box> { let engine = Engine::new(); let mut scope = Scope::new(); @@ -28,7 +28,7 @@ fn test_expressions() -> Result<(), EvalAltResult> { /// This example taken from https://github.com/jonathandturner/rhai/issues/115 #[test] #[cfg(not(feature = "no_object"))] -fn test_expressions_eval() -> Result<(), EvalAltResult> { +fn test_expressions_eval() -> Result<(), Box> { #[derive(Debug, Clone)] struct AGENT { pub gender: String, diff --git a/tests/float.rs b/tests/float.rs index 94ec7a03..8d70c7b3 100644 --- a/tests/float.rs +++ b/tests/float.rs @@ -4,7 +4,7 @@ use rhai::{Engine, EvalAltResult, RegisterFn, FLOAT}; const EPSILON: FLOAT = 0.000_000_000_1; #[test] -fn test_float() -> Result<(), EvalAltResult> { +fn test_float() -> Result<(), Box> { let engine = Engine::new(); assert_eq!( @@ -22,7 +22,7 @@ fn test_float() -> Result<(), EvalAltResult> { #[test] #[cfg(not(feature = "no_object"))] -fn struct_with_float() -> Result<(), EvalAltResult> { +fn struct_with_float() -> Result<(), Box> { #[derive(Clone)] struct TestStruct { x: f64, diff --git a/tests/for.rs b/tests/for.rs index f36f3ae3..081fddb9 100644 --- a/tests/for.rs +++ b/tests/for.rs @@ -2,7 +2,7 @@ use rhai::{Engine, EvalAltResult, INT}; #[cfg(not(feature = "no_index"))] #[test] -fn test_for_array() -> Result<(), EvalAltResult> { +fn test_for_array() -> Result<(), Box> { let engine = Engine::new(); let script = r" @@ -32,7 +32,7 @@ fn test_for_array() -> Result<(), EvalAltResult> { #[cfg(not(feature = "no_object"))] #[test] -fn test_for_object() -> Result<(), EvalAltResult> { +fn test_for_object() -> Result<(), Box> { let engine = Engine::new(); let script = r#" diff --git a/tests/get_set.rs b/tests/get_set.rs index 9f63a365..5859ea2e 100644 --- a/tests/get_set.rs +++ b/tests/get_set.rs @@ -3,7 +3,7 @@ use rhai::{Engine, EvalAltResult, RegisterFn, INT}; #[test] -fn test_get_set() -> Result<(), EvalAltResult> { +fn test_get_set() -> Result<(), Box> { #[derive(Clone)] struct TestStruct { x: INT, @@ -36,7 +36,7 @@ fn test_get_set() -> Result<(), EvalAltResult> { } #[test] -fn test_big_get_set() -> Result<(), EvalAltResult> { +fn test_big_get_set() -> Result<(), Box> { #[derive(Clone)] struct TestChild { x: INT, diff --git a/tests/if_block.rs b/tests/if_block.rs index dcde8143..a5d5125a 100644 --- a/tests/if_block.rs +++ b/tests/if_block.rs @@ -1,7 +1,7 @@ use rhai::{Engine, EvalAltResult, INT}; #[test] -fn test_if() -> Result<(), EvalAltResult> { +fn test_if() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("if true { 55 }")?, 55); @@ -29,7 +29,7 @@ fn test_if() -> Result<(), EvalAltResult> { } #[test] -fn test_if_expr() -> Result<(), EvalAltResult> { +fn test_if_expr() -> Result<(), Box> { let engine = Engine::new(); assert_eq!( diff --git a/tests/increment.rs b/tests/increment.rs index 9642af0a..f622ab21 100644 --- a/tests/increment.rs +++ b/tests/increment.rs @@ -1,7 +1,7 @@ use rhai::{Engine, EvalAltResult, INT}; #[test] -fn test_increment() -> Result<(), EvalAltResult> { +fn test_increment() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("let x = 1; x += 2; x")?, 3); diff --git a/tests/internal_fn.rs b/tests/internal_fn.rs index cc99187a..764cf601 100644 --- a/tests/internal_fn.rs +++ b/tests/internal_fn.rs @@ -3,7 +3,7 @@ use rhai::{Engine, EvalAltResult, INT}; #[test] -fn test_internal_fn() -> Result<(), EvalAltResult> { +fn test_internal_fn() -> Result<(), Box> { let engine = Engine::new(); assert_eq!( @@ -16,7 +16,7 @@ fn test_internal_fn() -> Result<(), EvalAltResult> { } #[test] -fn test_big_internal_fn() -> Result<(), EvalAltResult> { +fn test_big_internal_fn() -> Result<(), Box> { let engine = Engine::new(); assert_eq!( @@ -35,7 +35,7 @@ fn test_big_internal_fn() -> Result<(), EvalAltResult> { } #[test] -fn test_internal_fn_overloading() -> Result<(), EvalAltResult> { +fn test_internal_fn_overloading() -> Result<(), Box> { let engine = Engine::new(); assert_eq!( diff --git a/tests/looping.rs b/tests/looping.rs index c7aace27..a973c98f 100644 --- a/tests/looping.rs +++ b/tests/looping.rs @@ -1,7 +1,7 @@ use rhai::{Engine, EvalAltResult, INT}; #[test] -fn test_loop() -> Result<(), EvalAltResult> { +fn test_loop() -> Result<(), Box> { let engine = Engine::new(); assert_eq!( diff --git a/tests/maps.rs b/tests/maps.rs index c1b91187..897f8430 100644 --- a/tests/maps.rs +++ b/tests/maps.rs @@ -3,7 +3,7 @@ use rhai::{Engine, EvalAltResult, Map, Scope, INT}; #[test] -fn test_map_indexing() -> Result<(), EvalAltResult> { +fn test_map_indexing() -> Result<(), Box> { let engine = Engine::new(); #[cfg(not(feature = "no_index"))] @@ -81,7 +81,7 @@ fn test_map_indexing() -> Result<(), EvalAltResult> { } #[test] -fn test_map_assign() -> Result<(), EvalAltResult> { +fn test_map_assign() -> Result<(), Box> { let engine = Engine::new(); let x = engine.eval::(r#"let x = #{a: 1, b: true, "c$": "hello"}; x"#)?; @@ -112,7 +112,7 @@ fn test_map_assign() -> Result<(), EvalAltResult> { } #[test] -fn test_map_return() -> Result<(), EvalAltResult> { +fn test_map_return() -> Result<(), Box> { let engine = Engine::new(); let x = engine.eval::(r#"#{a: 1, b: true, "c$": "hello"}"#)?; @@ -143,7 +143,7 @@ fn test_map_return() -> Result<(), EvalAltResult> { } #[test] -fn test_map_for() -> Result<(), EvalAltResult> { +fn test_map_for() -> Result<(), Box> { let engine = Engine::new(); assert_eq!( @@ -170,7 +170,7 @@ fn test_map_for() -> Result<(), EvalAltResult> { #[test] /// Because a Rhai object map literal is almost the same as JSON, /// it is possible to convert from JSON into a Rhai object map. -fn test_map_json() -> Result<(), EvalAltResult> { +fn test_map_json() -> Result<(), Box> { let engine = Engine::new(); let json = r#"{"a":1, "b":true, "c":42, "$d e f!":"hello", "z":null}"#; diff --git a/tests/math.rs b/tests/math.rs index 500b35b8..4e4a5d4b 100644 --- a/tests/math.rs +++ b/tests/math.rs @@ -1,7 +1,7 @@ use rhai::{Engine, EvalAltResult, INT}; #[test] -fn test_math() -> Result<(), EvalAltResult> { +fn test_math() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("1 + 2")?, 3); @@ -25,37 +25,37 @@ fn test_math() -> Result<(), EvalAltResult> { #[cfg(not(feature = "only_i32"))] { assert!(matches!( - engine + *engine .eval::("abs(-9223372036854775808)") .expect_err("expects negation overflow"), EvalAltResult::ErrorArithmetic(_, _) )); assert!(matches!( - engine + *engine .eval::("9223372036854775807 + 1") .expect_err("expects overflow"), EvalAltResult::ErrorArithmetic(_, _) )); assert!(matches!( - engine + *engine .eval::("-9223372036854775808 - 1") .expect_err("expects underflow"), EvalAltResult::ErrorArithmetic(_, _) )); assert!(matches!( - engine + *engine .eval::("9223372036854775807 * 9223372036854775807") .expect_err("expects overflow"), EvalAltResult::ErrorArithmetic(_, _) )); assert!(matches!( - engine + *engine .eval::("9223372036854775807 / 0") .expect_err("expects division by zero"), EvalAltResult::ErrorArithmetic(_, _) )); assert!(matches!( - engine + *engine .eval::("9223372036854775807 % 0") .expect_err("expects division by zero"), EvalAltResult::ErrorArithmetic(_, _) @@ -65,31 +65,31 @@ fn test_math() -> Result<(), EvalAltResult> { #[cfg(feature = "only_i32")] { assert!(matches!( - engine + *engine .eval::("2147483647 + 1") .expect_err("expects overflow"), EvalAltResult::ErrorArithmetic(_, _) )); assert!(matches!( - engine + *engine .eval::("-2147483648 - 1") .expect_err("expects underflow"), EvalAltResult::ErrorArithmetic(_, _) )); assert!(matches!( - engine + *engine .eval::("2147483647 * 2147483647") .expect_err("expects overflow"), EvalAltResult::ErrorArithmetic(_, _) )); assert!(matches!( - engine + *engine .eval::("2147483647 / 0") .expect_err("expects division by zero"), EvalAltResult::ErrorArithmetic(_, _) )); assert!(matches!( - engine + *engine .eval::("2147483647 % 0") .expect_err("expects division by zero"), EvalAltResult::ErrorArithmetic(_, _) diff --git a/tests/method_call.rs b/tests/method_call.rs index a4e19251..99d1d0bd 100644 --- a/tests/method_call.rs +++ b/tests/method_call.rs @@ -3,7 +3,7 @@ use rhai::{Engine, EvalAltResult, RegisterFn, INT}; #[test] -fn test_method_call() -> Result<(), EvalAltResult> { +fn test_method_call() -> Result<(), Box> { #[derive(Debug, Clone, Eq, PartialEq)] struct TestStruct { x: INT, diff --git a/tests/mismatched_op.rs b/tests/mismatched_op.rs index e6e6e15d..b129f29b 100644 --- a/tests/mismatched_op.rs +++ b/tests/mismatched_op.rs @@ -4,10 +4,10 @@ use rhai::{Engine, EvalAltResult, RegisterFn, INT}; fn test_mismatched_op() { let engine = Engine::new(); - assert!( - matches!(engine.eval::(r#""hello, " + "world!""#).expect_err("expects error"), - EvalAltResult::ErrorMismatchOutputType(err, _) if err == "string") - ); + assert!(matches!( + *engine.eval::(r#""hello, " + "world!""#).expect_err("expects error"), + EvalAltResult::ErrorMismatchOutputType(err, _) if err == "string" + )); } #[test] @@ -33,12 +33,14 @@ fn test_mismatched_op_custom_type() { .expect_err("expects error"); #[cfg(feature = "only_i32")] - assert!( - matches!(r, EvalAltResult::ErrorFunctionNotFound(err, _) if err == "+ (i32, TestStruct)") - ); + assert!(matches!( + *r, + EvalAltResult::ErrorFunctionNotFound(err, _) if err == "+ (i32, TestStruct)" + )); #[cfg(not(feature = "only_i32"))] - assert!( - matches!(r, EvalAltResult::ErrorFunctionNotFound(err, _) if err == "+ (i64, TestStruct)") - ); + assert!(matches!( + *r, + EvalAltResult::ErrorFunctionNotFound(err, _) if err == "+ (i64, TestStruct)" + )); } diff --git a/tests/not.rs b/tests/not.rs index 4ce9cd3e..ffc1d21d 100644 --- a/tests/not.rs +++ b/tests/not.rs @@ -1,7 +1,7 @@ use rhai::{Engine, EvalAltResult}; #[test] -fn test_not() -> Result<(), EvalAltResult> { +fn test_not() -> Result<(), Box> { let engine = Engine::new(); assert_eq!( diff --git a/tests/number_literals.rs b/tests/number_literals.rs index 3fe8c78f..699de19a 100644 --- a/tests/number_literals.rs +++ b/tests/number_literals.rs @@ -1,7 +1,7 @@ use rhai::{Engine, EvalAltResult, INT}; #[test] -fn test_number_literal() -> Result<(), EvalAltResult> { +fn test_number_literal() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("65")?, 65); @@ -10,7 +10,7 @@ fn test_number_literal() -> Result<(), EvalAltResult> { } #[test] -fn test_hex_literal() -> Result<(), EvalAltResult> { +fn test_hex_literal() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("let x = 0xf; x")?, 15); @@ -20,7 +20,7 @@ fn test_hex_literal() -> Result<(), EvalAltResult> { } #[test] -fn test_octal_literal() -> Result<(), EvalAltResult> { +fn test_octal_literal() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("let x = 0o77; x")?, 63); @@ -30,7 +30,7 @@ fn test_octal_literal() -> Result<(), EvalAltResult> { } #[test] -fn test_binary_literal() -> Result<(), EvalAltResult> { +fn test_binary_literal() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("let x = 0b1111; x")?, 15); diff --git a/tests/ops.rs b/tests/ops.rs index c817d65d..e087a8d0 100644 --- a/tests/ops.rs +++ b/tests/ops.rs @@ -1,7 +1,7 @@ use rhai::{Engine, EvalAltResult, INT}; #[test] -fn test_ops() -> Result<(), EvalAltResult> { +fn test_ops() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("60 + 5")?, 65); @@ -11,7 +11,7 @@ fn test_ops() -> Result<(), EvalAltResult> { } #[test] -fn test_op_precedence() -> Result<(), EvalAltResult> { +fn test_op_precedence() -> Result<(), Box> { let engine = Engine::new(); assert_eq!( diff --git a/tests/optimizer.rs b/tests/optimizer.rs index eb9f1d5c..49b15c25 100644 --- a/tests/optimizer.rs +++ b/tests/optimizer.rs @@ -3,8 +3,8 @@ use rhai::{Engine, EvalAltResult, OptimizationLevel, INT}; #[test] -fn test_optimizer() -> Result<(), EvalAltResult> { - fn run_test(engine: &mut Engine) -> Result<(), EvalAltResult> { +fn test_optimizer() -> Result<(), Box> { + fn run_test(engine: &mut Engine) -> Result<(), Box> { assert_eq!(engine.eval::(r"if true { 42 } else { 123 }")?, 42); assert_eq!( engine.eval::(r"if 1 == 1 || 2 > 3 { 42 } else { 123 }")?, diff --git a/tests/power_of.rs b/tests/power_of.rs index 3762ff0f..a3b6fd43 100644 --- a/tests/power_of.rs +++ b/tests/power_of.rs @@ -7,7 +7,7 @@ use rhai::FLOAT; const EPSILON: FLOAT = 0.000_000_000_1; #[test] -fn test_power_of() -> Result<(), EvalAltResult> { +fn test_power_of() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("2 ~ 3")?, 8); @@ -28,7 +28,7 @@ fn test_power_of() -> Result<(), EvalAltResult> { } #[test] -fn test_power_of_equals() -> Result<(), EvalAltResult> { +fn test_power_of_equals() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("let x = 2; x ~= 3; x")?, 8); diff --git a/tests/side_effects.rs b/tests/side_effects.rs index 259d1f63..fb364747 100644 --- a/tests/side_effects.rs +++ b/tests/side_effects.rs @@ -40,7 +40,7 @@ impl CommandWrapper { #[cfg(not(feature = "no_object"))] #[test] -fn test_side_effects_command() -> Result<(), EvalAltResult> { +fn test_side_effects_command() -> Result<(), Box> { let mut engine = Engine::new(); let mut scope = Scope::new(); @@ -80,7 +80,7 @@ fn test_side_effects_command() -> Result<(), EvalAltResult> { } #[test] -fn test_side_effects_print() -> Result<(), EvalAltResult> { +fn test_side_effects_print() -> Result<(), Box> { use std::sync::Arc; use std::sync::RwLock; diff --git a/tests/stack.rs b/tests/stack.rs index 8efc759e..503bcbde 100644 --- a/tests/stack.rs +++ b/tests/stack.rs @@ -2,7 +2,7 @@ use rhai::{Engine, EvalAltResult}; #[test] -fn test_stack_overflow() -> Result<(), EvalAltResult> { +fn test_stack_overflow() -> Result<(), Box> { let engine = Engine::new(); assert_eq!( @@ -22,8 +22,10 @@ fn test_stack_overflow() -> Result<(), EvalAltResult> { ", ) { Ok(_) => panic!("should be stack overflow"), - Err(EvalAltResult::ErrorStackOverflow(_)) => (), - Err(_) => panic!("should be stack overflow"), + Err(err) => match *err { + EvalAltResult::ErrorStackOverflow(_) => (), + _ => panic!("should be stack overflow"), + }, } Ok(()) diff --git a/tests/string.rs b/tests/string.rs index 1ec77e2e..981d19eb 100644 --- a/tests/string.rs +++ b/tests/string.rs @@ -1,7 +1,7 @@ use rhai::{Engine, EvalAltResult, INT}; #[test] -fn test_string() -> Result<(), EvalAltResult> { +fn test_string() -> Result<(), Box> { let engine = Engine::new(); assert_eq!( @@ -37,7 +37,7 @@ fn test_string() -> Result<(), EvalAltResult> { #[cfg(not(feature = "no_stdlib"))] #[cfg(not(feature = "no_object"))] #[test] -fn test_string_substring() -> Result<(), EvalAltResult> { +fn test_string_substring() -> Result<(), Box> { let engine = Engine::new(); assert_eq!( diff --git a/tests/throw.rs b/tests/throw.rs index 6e55303c..5f5c58a0 100644 --- a/tests/throw.rs +++ b/tests/throw.rs @@ -5,10 +5,12 @@ fn test_throw() { let engine = Engine::new(); assert!(matches!( - engine.eval::<()>(r#"if true { throw "hello" }"#).expect_err("expects error"), - EvalAltResult::ErrorRuntime(s, _) if s == "hello")); + *engine.eval::<()>(r#"if true { throw "hello" }"#).expect_err("expects error"), + EvalAltResult::ErrorRuntime(s, _) if s == "hello" + )); assert!(matches!( - engine.eval::<()>(r#"throw"#).expect_err("expects error"), - EvalAltResult::ErrorRuntime(s, _) if s == "")); + *engine.eval::<()>(r#"throw"#).expect_err("expects error"), + EvalAltResult::ErrorRuntime(s, _) if s == "" + )); } diff --git a/tests/time.rs b/tests/time.rs index eddb652d..aa5239b1 100644 --- a/tests/time.rs +++ b/tests/time.rs @@ -7,7 +7,7 @@ use rhai::{Engine, EvalAltResult, INT}; use rhai::FLOAT; #[test] -fn test_timestamp() -> Result<(), EvalAltResult> { +fn test_timestamp() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("type_of(timestamp())")?, "timestamp"); diff --git a/tests/types.rs b/tests/types.rs index 658d8b3f..d5a5928e 100644 --- a/tests/types.rs +++ b/tests/types.rs @@ -1,7 +1,7 @@ use rhai::{Engine, EvalAltResult, RegisterFn, INT}; #[test] -fn test_type_of() -> Result<(), EvalAltResult> { +fn test_type_of() -> Result<(), Box> { #[derive(Clone)] struct TestStruct { x: INT, diff --git a/tests/unary_after_binary.rs b/tests/unary_after_binary.rs index ba74c9f9..d2366be7 100644 --- a/tests/unary_after_binary.rs +++ b/tests/unary_after_binary.rs @@ -3,7 +3,7 @@ use rhai::{Engine, EvalAltResult, INT}; #[test] // TODO also add test case for unary after compound // Hah, turns out unary + has a good use after all! -fn test_unary_after_binary() -> Result<(), EvalAltResult> { +fn test_unary_after_binary() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("10 % +4")?, 2); diff --git a/tests/unary_minus.rs b/tests/unary_minus.rs index e1a359d7..87e2aa2b 100644 --- a/tests/unary_minus.rs +++ b/tests/unary_minus.rs @@ -1,7 +1,7 @@ use rhai::{Engine, EvalAltResult, INT}; #[test] -fn test_unary_minus() -> Result<(), EvalAltResult> { +fn test_unary_minus() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("let x = -5; x")?, -5); diff --git a/tests/unit.rs b/tests/unit.rs index 465c7b48..2777ae54 100644 --- a/tests/unit.rs +++ b/tests/unit.rs @@ -1,21 +1,21 @@ use rhai::{Engine, EvalAltResult}; #[test] -fn test_unit() -> Result<(), EvalAltResult> { +fn test_unit() -> Result<(), Box> { let engine = Engine::new(); engine.eval::<()>("let x = (); x")?; Ok(()) } #[test] -fn test_unit_eq() -> Result<(), EvalAltResult> { +fn test_unit_eq() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(engine.eval::("let x = (); let y = (); x == y")?, true); Ok(()) } #[test] -fn test_unit_with_spaces() -> Result<(), EvalAltResult> { +fn test_unit_with_spaces() -> Result<(), Box> { let engine = Engine::new(); engine.eval::<()>("let x = ( ); x")?; Ok(()) diff --git a/tests/var_scope.rs b/tests/var_scope.rs index 8d5244c8..fde83a05 100644 --- a/tests/var_scope.rs +++ b/tests/var_scope.rs @@ -1,7 +1,7 @@ use rhai::{Engine, EvalAltResult, Scope, INT}; #[test] -fn test_var_scope() -> Result<(), EvalAltResult> { +fn test_var_scope() -> Result<(), Box> { let engine = Engine::new(); let mut scope = Scope::new(); @@ -20,7 +20,7 @@ fn test_var_scope() -> Result<(), EvalAltResult> { } #[test] -fn test_scope_eval() -> Result<(), EvalAltResult> { +fn test_scope_eval() -> Result<(), Box> { let engine = Engine::new(); // First create the state diff --git a/tests/while_loop.rs b/tests/while_loop.rs index 3a035039..8916cd7c 100644 --- a/tests/while_loop.rs +++ b/tests/while_loop.rs @@ -1,7 +1,7 @@ use rhai::{Engine, EvalAltResult, INT}; #[test] -fn test_while() -> Result<(), EvalAltResult> { +fn test_while() -> Result<(), Box> { let engine = Engine::new(); assert_eq!(