From 1c7b80ed1361fb89044ec1296e39a7665e84d3e3 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 12 Aug 2022 22:48:15 +0800 Subject: [PATCH] Use turbofish for impl Trait. --- examples/event_handler_main/main.rs | 2 +- examples/event_handler_map/main.rs | 4 ++-- src/api/call_fn.rs | 8 ++++---- src/api/files.rs | 28 ++++++++++++++-------------- tests/call_fn.rs | 14 +++++++------- tests/closures.rs | 2 +- tests/modules.rs | 4 ++-- 7 files changed, 31 insertions(+), 31 deletions(-) diff --git a/examples/event_handler_main/main.rs b/examples/event_handler_main/main.rs index bc64a8b6..c70c8740 100644 --- a/examples/event_handler_main/main.rs +++ b/examples/event_handler_main/main.rs @@ -124,7 +124,7 @@ pub fn main() { let scope = &mut handler.scope; let ast = &handler.ast; - let result: Result<(), _> = engine.call_fn(scope, ast, event, (arg.to_string(),)); + let result = engine.call_fn::<()>(scope, ast, event, (arg.to_string(),)); if let Err(err) = result { eprintln!("! {}", err) diff --git a/examples/event_handler_map/main.rs b/examples/event_handler_map/main.rs index ad27e8ea..e45688db 100644 --- a/examples/event_handler_map/main.rs +++ b/examples/event_handler_map/main.rs @@ -100,7 +100,7 @@ pub fn main() { println!(); // Run the 'init' function to initialize the state, retaining variables. - let result: Result<(), _> = engine.call_fn(&mut scope, &ast, "init", ()); + let result = engine.call_fn::<()>(&mut scope, &ast, "init", ()); if let Err(err) = result { eprintln!("! {}", err) @@ -138,7 +138,7 @@ pub fn main() { let scope = &mut handler.scope; let ast = &handler.ast; - let result: Result<(), _> = engine.call_fn(scope, ast, event, (arg.to_string(),)); + let result = engine.call_fn::<()>(scope, ast, event, (arg.to_string(),)); if let Err(err) = result { eprintln!("! {}", err) diff --git a/src/api/call_fn.rs b/src/api/call_fn.rs index 53445c2c..611ede44 100644 --- a/src/api/call_fn.rs +++ b/src/api/call_fn.rs @@ -41,14 +41,14 @@ impl Engine { /// scope.push("foo", 42_i64); /// /// // Call the script-defined function - /// let result: i64 = engine.call_fn(&mut scope, &ast, "add", ( "abc", 123_i64 ) )?; + /// let result = engine.call_fn::(&mut scope, &ast, "add", ( "abc", 123_i64 ) )?; /// assert_eq!(result, 168); /// - /// let result: i64 = engine.call_fn(&mut scope, &ast, "add1", ( "abc", ) )?; - /// // ^^^^^^^^^^ tuple of one + /// let result = engine.call_fn::(&mut scope, &ast, "add1", ( "abc", ) )?; + /// // ^^^^^^^^^^ tuple of one /// assert_eq!(result, 46); /// - /// let result: i64 = engine.call_fn(&mut scope, &ast, "bar", () )?; + /// let result = engine.call_fn::(&mut scope, &ast, "bar", () )?; /// assert_eq!(result, 21); /// # } /// # Ok(()) diff --git a/src/api/files.rs b/src/api/files.rs index 9c452e0b..17bd324c 100644 --- a/src/api/files.rs +++ b/src/api/files.rs @@ -6,12 +6,18 @@ use crate::types::dynamic::Variant; use crate::{Engine, RhaiResultOf, Scope, AST, ERR}; #[cfg(feature = "no_std")] use std::prelude::v1::*; -use std::{fs::File, io::Read, path::PathBuf}; +use std::{ + fs::File, + io::Read, + path::{Path, PathBuf}, +}; impl Engine { /// Read the contents of a file into a string. - fn read_file(path: PathBuf) -> RhaiResultOf { - let mut f = File::open(path.clone()).map_err(|err| { + fn read_file(path: impl AsRef) -> RhaiResultOf { + let path = path.as_ref(); + + let mut f = File::open(path).map_err(|err| { ERR::ErrorSystem( format!("Cannot open script file '{}'", path.to_string_lossy()), err.into(), @@ -214,7 +220,7 @@ impl Engine { } } -/// Evaluate a script file. +/// Evaluate a script file, returning the result value or an error. /// /// Not available under `no_std` or `WASM`. /// @@ -222,13 +228,12 @@ impl Engine { /// /// ```no_run /// # fn main() -> Result<(), Box> { -/// // Notice that a PathBuf is required which can easily be constructed from a string. -/// let result: i64 = rhai::eval_file("script.rhai".into())?; +/// let result = rhai::eval_file::("script.rhai")?; /// # Ok(()) /// # } /// ``` #[inline] -pub fn eval_file(path: PathBuf) -> RhaiResultOf { +pub fn eval_file(path: impl AsRef) -> RhaiResultOf { Engine::read_file(path).and_then(|contents| Engine::new().eval::(&contents)) } @@ -240,16 +245,11 @@ pub fn eval_file(path: PathBuf) -> RhaiResultOf { /// /// ```no_run /// # fn main() -> Result<(), Box> { -/// use rhai::Engine; -/// -/// let engine = Engine::new(); -/// -/// // Notice that a PathBuf is required which can easily be constructed from a string. -/// rhai::run_file("script.rhai".into())?; +/// rhai::run_file("script.rhai")?; /// # Ok(()) /// # } /// ``` #[inline] -pub fn run_file(path: PathBuf) -> RhaiResultOf<()> { +pub fn run_file(path: impl AsRef) -> RhaiResultOf<()> { Engine::read_file(path).and_then(|contents| Engine::new().run(&contents)) } diff --git a/tests/call_fn.rs b/tests/call_fn.rs index 30a02cc1..0ac876a6 100644 --- a/tests/call_fn.rs +++ b/tests/call_fn.rs @@ -29,13 +29,13 @@ fn test_call_fn() -> Result<(), Box> { ", )?; - let r: INT = engine.call_fn(&mut scope, &ast, "hello", (42 as INT, 123 as INT))?; + let r = engine.call_fn::(&mut scope, &ast, "hello", (42 as INT, 123 as INT))?; assert_eq!(r, 165); - let r: INT = engine.call_fn(&mut scope, &ast, "hello", (123 as INT,))?; + let r = engine.call_fn::(&mut scope, &ast, "hello", (123 as INT,))?; assert_eq!(r, 5166); - let r: INT = engine.call_fn(&mut scope, &ast, "hello", ())?; + let r = engine.call_fn::(&mut scope, &ast, "hello", ())?; assert_eq!(r, 42); assert_eq!( @@ -45,7 +45,7 @@ fn test_call_fn() -> Result<(), Box> { 1 ); - let r: INT = engine.call_fn(&mut scope, &ast, "define_var", (2 as INT,))?; + let r = engine.call_fn::(&mut scope, &ast, "define_var", (2 as INT,))?; assert_eq!(r, 42); assert!(!scope.contains("bar")); @@ -132,7 +132,7 @@ fn test_call_fn_args() -> Result<(), Box> { ", )?; - let result: String = engine.call_fn(&mut scope, &ast, "hello", options)?; + let result = engine.call_fn::(&mut scope, &ast, "hello", options)?; assert_eq!(result, "world42"); @@ -146,12 +146,12 @@ fn test_call_fn_private() -> Result<(), Box> { let ast = engine.compile("fn add(x, n) { x + n }")?; - let r: INT = engine.call_fn(&mut scope, &ast, "add", (40 as INT, 2 as INT))?; + let r = engine.call_fn::(&mut scope, &ast, "add", (40 as INT, 2 as INT))?; assert_eq!(r, 42); let ast = engine.compile("private fn add(x, n, ) { x + n }")?; - let r: INT = engine.call_fn(&mut scope, &ast, "add", (40 as INT, 2 as INT))?; + let r = engine.call_fn::(&mut scope, &ast, "add", (40 as INT, 2 as INT))?; assert_eq!(r, 42); Ok(()) diff --git a/tests/closures.rs b/tests/closures.rs index 392e8e94..9be2b82c 100644 --- a/tests/closures.rs +++ b/tests/closures.rs @@ -338,7 +338,7 @@ fn test_closures_shared_obj() -> Result<(), Box> { let res = engine.eval_ast::(&ast)?; // Make closure - let f = move |p1: TestStruct, p2: TestStruct| -> Result<(), Box> { + let f = move |p1: TestStruct, p2: TestStruct| { let action_ptr = res["action"].clone_cast::(); let name = action_ptr.fn_name(); engine.call_fn(&mut Scope::new(), &ast, name, (p1, p2)) diff --git a/tests/modules.rs b/tests/modules.rs index d80fcfba..a781be3f 100644 --- a/tests/modules.rs +++ b/tests/modules.rs @@ -279,7 +279,7 @@ fn test_module_resolver() -> Result<(), Box> { assert!(engine.eval::(script).is_err()); - let result: INT = engine.call_fn(&mut Scope::new(), &ast, "foo", (2 as INT,))?; + let result = engine.call_fn::(&mut Scope::new(), &ast, "foo", (2 as INT,))?; assert_eq!(result, 84); @@ -296,7 +296,7 @@ fn test_module_resolver() -> Result<(), Box> { assert_eq!(ast2.resolver().unwrap().len(), len); } - let result: INT = engine.call_fn(&mut Scope::new(), &ast2, "foo", (2 as INT,))?; + let result = engine.call_fn::(&mut Scope::new(), &ast2, "foo", (2 as INT,))?; assert_eq!(result, 84); }