Skip to content

Commit

Permalink
Use turbofish for impl Trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
schungx committed Aug 12, 2022
1 parent 195c81c commit 1c7b80e
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion examples/event_handler_main/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions examples/event_handler_map/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/api/call_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i64>(&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::<i64>(&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::<i64>(&mut scope, &ast, "bar", () )?;
/// assert_eq!(result, 21);
/// # }
/// # Ok(())
Expand Down
28 changes: 14 additions & 14 deletions src/api/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
let mut f = File::open(path.clone()).map_err(|err| {
fn read_file(path: impl AsRef<Path>) -> RhaiResultOf<String> {
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(),
Expand Down Expand Up @@ -214,21 +220,20 @@ impl Engine {
}
}

/// Evaluate a script file.
/// Evaluate a script file, returning the result value or an error.
///
/// Not available under `no_std` or `WASM`.
///
/// # Example
///
/// ```no_run
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// // 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::<i64>("script.rhai")?;
/// # Ok(())
/// # }
/// ```
#[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))
}

Expand All @@ -240,16 +245,11 @@ pub fn eval_file<T: Variant + Clone>(path: PathBuf) -> RhaiResultOf<T> {
///
/// ```no_run
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// 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<Path>) -> RhaiResultOf<()> {
Engine::read_file(path).and_then(|contents| Engine::new().run(&contents))
}
14 changes: 7 additions & 7 deletions tests/call_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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);

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!(
Expand All @@ -45,7 +45,7 @@ fn test_call_fn() -> Result<(), Box<EvalAltResult>> {
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!(!scope.contains("bar"));
Expand Down Expand Up @@ -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");

Expand All @@ -146,12 +146,12 @@ fn test_call_fn_private() -> Result<(), Box<EvalAltResult>> {

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);

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);

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion tests/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ fn test_closures_shared_obj() -> Result<(), Box<EvalAltResult>> {
let res = engine.eval_ast::<Map>(&ast)?;

// 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 name = action_ptr.fn_name();
engine.call_fn(&mut Scope::new(), &ast, name, (p1, p2))
Expand Down
4 changes: 2 additions & 2 deletions tests/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ fn test_module_resolver() -> Result<(), Box<EvalAltResult>> {

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);

Expand All @@ -296,7 +296,7 @@ fn test_module_resolver() -> Result<(), Box<EvalAltResult>> {
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);
}
Expand Down

0 comments on commit 1c7b80e

Please sign in to comment.