Use turbofish for impl Trait.

This commit is contained in:
Stephen Chung 2022-08-12 22:48:15 +08:00
parent 195c81c6ff
commit 1c7b80ed13
7 changed files with 31 additions and 31 deletions

View File

@ -124,7 +124,7 @@ pub fn main() {
let scope = &mut handler.scope; let scope = &mut handler.scope;
let ast = &handler.ast; 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 { if let Err(err) = result {
eprintln!("! {}", err) eprintln!("! {}", err)

View File

@ -100,7 +100,7 @@ pub fn main() {
println!(); println!();
// Run the 'init' function to initialize the state, retaining variables. // 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 { if let Err(err) = result {
eprintln!("! {}", err) eprintln!("! {}", err)
@ -138,7 +138,7 @@ pub fn main() {
let scope = &mut handler.scope; let scope = &mut handler.scope;
let ast = &handler.ast; 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 { if let Err(err) = result {
eprintln!("! {}", err) eprintln!("! {}", err)

View File

@ -41,14 +41,14 @@ impl Engine {
/// scope.push("foo", 42_i64); /// scope.push("foo", 42_i64);
/// ///
/// // Call the script-defined function /// // Call the script-defined function
/// let result: i64 = engine.call_fn(&mut scope, &ast, "add", ( "abc", 123_i64 ) )?; /// let result = engine.call_fn::<i64>(&mut scope, &ast, "add", ( "abc", 123_i64 ) )?;
/// assert_eq!(result, 168); /// assert_eq!(result, 168);
/// ///
/// let result: i64 = engine.call_fn(&mut scope, &ast, "add1", ( "abc", ) )?; /// let result = engine.call_fn::<i64>(&mut scope, &ast, "add1", ( "abc", ) )?;
/// // ^^^^^^^^^^ tuple of one /// // ^^^^^^^^^^ tuple of one
/// assert_eq!(result, 46); /// assert_eq!(result, 46);
/// ///
/// let result: i64 = engine.call_fn(&mut scope, &ast, "bar", () )?; /// let result = engine.call_fn::<i64>(&mut scope, &ast, "bar", () )?;
/// assert_eq!(result, 21); /// assert_eq!(result, 21);
/// # } /// # }
/// # Ok(()) /// # Ok(())

View File

@ -6,12 +6,18 @@ use crate::types::dynamic::Variant;
use crate::{Engine, RhaiResultOf, Scope, AST, ERR}; use crate::{Engine, RhaiResultOf, Scope, AST, ERR};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{fs::File, io::Read, path::PathBuf}; use std::{
fs::File,
io::Read,
path::{Path, PathBuf},
};
impl Engine { impl Engine {
/// Read the contents of a file into a string. /// Read the contents of a file into a string.
fn read_file(path: PathBuf) -> RhaiResultOf<String> { fn read_file(path: impl AsRef<Path>) -> RhaiResultOf<String> {
let mut f = File::open(path.clone()).map_err(|err| { let path = path.as_ref();
let mut f = File::open(path).map_err(|err| {
ERR::ErrorSystem( ERR::ErrorSystem(
format!("Cannot open script file '{}'", path.to_string_lossy()), format!("Cannot open script file '{}'", path.to_string_lossy()),
err.into(), 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`. /// Not available under `no_std` or `WASM`.
/// ///
@ -222,13 +228,12 @@ impl Engine {
/// ///
/// ```no_run /// ```no_run
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> { /// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// // Notice that a PathBuf is required which can easily be constructed from a string. /// let result = rhai::eval_file::<i64>("script.rhai")?;
/// let result: i64 = rhai::eval_file("script.rhai".into())?;
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
#[inline] #[inline]
pub fn eval_file<T: Variant + Clone>(path: PathBuf) -> RhaiResultOf<T> { pub fn eval_file<T: Variant + Clone>(path: impl AsRef<Path>) -> RhaiResultOf<T> {
Engine::read_file(path).and_then(|contents| Engine::new().eval::<T>(&contents)) Engine::read_file(path).and_then(|contents| Engine::new().eval::<T>(&contents))
} }
@ -240,16 +245,11 @@ pub fn eval_file<T: Variant + Clone>(path: PathBuf) -> RhaiResultOf<T> {
/// ///
/// ```no_run /// ```no_run
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> { /// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::Engine; /// rhai::run_file("script.rhai")?;
///
/// let engine = Engine::new();
///
/// // Notice that a PathBuf is required which can easily be constructed from a string.
/// rhai::run_file("script.rhai".into())?;
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
#[inline] #[inline]
pub fn run_file(path: PathBuf) -> RhaiResultOf<()> { pub fn run_file(path: impl AsRef<Path>) -> RhaiResultOf<()> {
Engine::read_file(path).and_then(|contents| Engine::new().run(&contents)) Engine::read_file(path).and_then(|contents| Engine::new().run(&contents))
} }

View File

@ -29,13 +29,13 @@ fn test_call_fn() -> Result<(), Box<EvalAltResult>> {
", ",
)?; )?;
let r: INT = engine.call_fn(&mut scope, &ast, "hello", (42 as INT, 123 as INT))?; let r = engine.call_fn::<INT>(&mut scope, &ast, "hello", (42 as INT, 123 as INT))?;
assert_eq!(r, 165); assert_eq!(r, 165);
let r: INT = engine.call_fn(&mut scope, &ast, "hello", (123 as INT,))?; let r = engine.call_fn::<INT>(&mut scope, &ast, "hello", (123 as INT,))?;
assert_eq!(r, 5166); assert_eq!(r, 5166);
let r: INT = engine.call_fn(&mut scope, &ast, "hello", ())?; let r = engine.call_fn::<INT>(&mut scope, &ast, "hello", ())?;
assert_eq!(r, 42); assert_eq!(r, 42);
assert_eq!( assert_eq!(
@ -45,7 +45,7 @@ fn test_call_fn() -> Result<(), Box<EvalAltResult>> {
1 1
); );
let r: INT = engine.call_fn(&mut scope, &ast, "define_var", (2 as INT,))?; let r = engine.call_fn::<INT>(&mut scope, &ast, "define_var", (2 as INT,))?;
assert_eq!(r, 42); assert_eq!(r, 42);
assert!(!scope.contains("bar")); assert!(!scope.contains("bar"));
@ -132,7 +132,7 @@ fn test_call_fn_args() -> Result<(), Box<EvalAltResult>> {
", ",
)?; )?;
let result: String = engine.call_fn(&mut scope, &ast, "hello", options)?; let result = engine.call_fn::<String>(&mut scope, &ast, "hello", options)?;
assert_eq!(result, "world42"); assert_eq!(result, "world42");
@ -146,12 +146,12 @@ fn test_call_fn_private() -> Result<(), Box<EvalAltResult>> {
let ast = engine.compile("fn add(x, n) { x + n }")?; 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::<INT>(&mut scope, &ast, "add", (40 as INT, 2 as INT))?;
assert_eq!(r, 42); assert_eq!(r, 42);
let ast = engine.compile("private fn add(x, n, ) { x + n }")?; 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::<INT>(&mut scope, &ast, "add", (40 as INT, 2 as INT))?;
assert_eq!(r, 42); assert_eq!(r, 42);
Ok(()) Ok(())

View File

@ -338,7 +338,7 @@ fn test_closures_shared_obj() -> Result<(), Box<EvalAltResult>> {
let res = engine.eval_ast::<Map>(&ast)?; let res = engine.eval_ast::<Map>(&ast)?;
// Make closure // Make closure
let f = move |p1: TestStruct, p2: TestStruct| -> Result<(), Box<EvalAltResult>> { let f = move |p1: TestStruct, p2: TestStruct| {
let action_ptr = res["action"].clone_cast::<FnPtr>(); let action_ptr = res["action"].clone_cast::<FnPtr>();
let name = action_ptr.fn_name(); let name = action_ptr.fn_name();
engine.call_fn(&mut Scope::new(), &ast, name, (p1, p2)) engine.call_fn(&mut Scope::new(), &ast, name, (p1, p2))

View File

@ -279,7 +279,7 @@ fn test_module_resolver() -> Result<(), Box<EvalAltResult>> {
assert!(engine.eval::<INT>(script).is_err()); assert!(engine.eval::<INT>(script).is_err());
let result: INT = engine.call_fn(&mut Scope::new(), &ast, "foo", (2 as INT,))?; let result = engine.call_fn::<INT>(&mut Scope::new(), &ast, "foo", (2 as INT,))?;
assert_eq!(result, 84); assert_eq!(result, 84);
@ -296,7 +296,7 @@ fn test_module_resolver() -> Result<(), Box<EvalAltResult>> {
assert_eq!(ast2.resolver().unwrap().len(), len); 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::<INT>(&mut Scope::new(), &ast2, "foo", (2 as INT,))?;
assert_eq!(result, 84); assert_eq!(result, 84);
} }