From 46cd8af9517d3bd62593333e10743ef99172953a Mon Sep 17 00:00:00 2001 From: starovoid Date: Sat, 21 Jan 2023 17:34:58 +0300 Subject: [PATCH 1/2] feat: Replace PathBuf to AsRef in the Engine file API The use of PathBuf has been replaced with AsRef in the file API of the engine. An implementation of `From<&Path>` for Immutable `String` has been added to satisfy type constraints in `ast` module. --- .tool-versions | 1 + examples/event_handler_js/main.rs | 3 ++- examples/event_handler_main/main.rs | 3 ++- examples/event_handler_map/main.rs | 3 ++- src/api/files.rs | 31 +++++++++++++---------------- src/lib.rs | 3 ++- src/types/immutable_string.rs | 8 ++++++++ 7 files changed, 31 insertions(+), 21 deletions(-) create mode 100644 .tool-versions diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 000000000..aa5e78b51 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +rust 1.65.0 \ No newline at end of file diff --git a/examples/event_handler_js/main.rs b/examples/event_handler_js/main.rs index e6998d7a1..0953812d9 100644 --- a/examples/event_handler_js/main.rs +++ b/examples/event_handler_js/main.rs @@ -10,6 +10,7 @@ pub fn main() { pub fn main() { use rhai::{CallFnOptions, Dynamic, Engine, Map, Scope, AST}; use std::io::{stdin, stdout, Write}; + use std::path::Path; const SCRIPT_FILE: &str = "event_handler_js/script.rhai"; @@ -80,7 +81,7 @@ pub fn main() { // Compile the handler script. println!("> Loading script file: {path}"); - let ast = match engine.compile_file_with_scope(&mut scope, path.into()) { + let ast = match engine.compile_file_with_scope(&mut scope, path) { Ok(ast) => ast, Err(err) => { eprintln!("! Error: {err}"); diff --git a/examples/event_handler_main/main.rs b/examples/event_handler_main/main.rs index 8fbfdafd4..1cf313ab4 100644 --- a/examples/event_handler_main/main.rs +++ b/examples/event_handler_main/main.rs @@ -9,6 +9,7 @@ pub fn main() { pub fn main() { use rhai::{CallFnOptions, Dynamic, Engine, Scope, AST}; use std::io::{stdin, stdout, Write}; + use std::path::Path; const SCRIPT_FILE: &str = "event_handler_main/script.rhai"; @@ -69,7 +70,7 @@ pub fn main() { // Compile the handler script. println!("> Loading script file: {path}"); - let ast = match engine.compile_file_with_scope(&mut scope, path.into()) { + let ast = match engine.compile_file_with_scope(&mut scope, &Path::new(path)) { Ok(ast) => ast, Err(err) => { eprintln!("! Error: {}", err); diff --git a/examples/event_handler_map/main.rs b/examples/event_handler_map/main.rs index b92dba94d..7bf444a1b 100644 --- a/examples/event_handler_map/main.rs +++ b/examples/event_handler_map/main.rs @@ -10,6 +10,7 @@ pub fn main() { pub fn main() { use rhai::{Dynamic, Engine, Map, Scope, AST}; use std::io::{stdin, stdout, Write}; + use std::path::Path; const SCRIPT_FILE: &str = "event_handler_map/script.rhai"; @@ -83,7 +84,7 @@ pub fn main() { // Compile the handler script. println!("> Loading script file: {path}"); - let ast = match engine.compile_file_with_scope(&mut scope, path.into()) { + let ast = match engine.compile_file_with_scope(&mut scope, &Path::new(path)) { Ok(ast) => ast, Err(err) => { eprintln!("! Error: {err}"); diff --git a/src/api/files.rs b/src/api/files.rs index 2025b412c..276f4a703 100644 --- a/src/api/files.rs +++ b/src/api/files.rs @@ -9,7 +9,7 @@ use std::prelude::v1::*; use std::{ fs::File, io::Read, - path::{Path, PathBuf}, + path::Path, }; impl Engine { @@ -57,8 +57,7 @@ impl Engine { /// let engine = Engine::new(); /// /// // Compile a script file to an AST and store it for later evaluation. - /// // Notice that a PathBuf is required which can easily be constructed from a string. - /// let ast = engine.compile_file("script.rhai".into())?; + /// let ast = engine.compile_file("script.rhai")?; /// /// for _ in 0..42 { /// engine.eval_ast::(&ast)?; @@ -67,7 +66,7 @@ impl Engine { /// # } /// ``` #[inline(always)] - pub fn compile_file(&self, path: PathBuf) -> RhaiResultOf { + pub fn compile_file(&self, path: impl AsRef) -> RhaiResultOf { self.compile_file_with_scope(&Scope::new(), path) } /// Compile a script file into an [`AST`] using own scope, which can be used later for evaluation. @@ -96,8 +95,7 @@ impl Engine { /// scope.push_constant("x", 42_i64); // 'x' is a constant /// /// // Compile a script to an AST and store it for later evaluation. - /// // Notice that a PathBuf is required which can easily be constructed from a string. - /// let ast = engine.compile_file_with_scope(&mut scope, "script.rhai".into())?; + /// let ast = engine.compile_file_with_scope(&mut scope, "script.rhai")?; /// /// let result = engine.eval_ast::(&ast)?; /// # } @@ -105,10 +103,10 @@ impl Engine { /// # } /// ``` #[inline] - pub fn compile_file_with_scope(&self, scope: &Scope, path: PathBuf) -> RhaiResultOf { + pub fn compile_file_with_scope(&self, scope: &Scope, path: impl AsRef) -> RhaiResultOf { Self::read_file(&path).and_then(|contents| { let mut ast = self.compile_with_scope(scope, contents)?; - ast.set_source(path.to_string_lossy().as_ref()); + ast.set_source(path.as_ref().to_string_lossy().as_ref()); Ok(ast) }) } @@ -125,12 +123,12 @@ impl Engine { /// let engine = Engine::new(); /// /// // Notice that a PathBuf is required which can easily be constructed from a string. - /// let result = engine.eval_file::("script.rhai".into())?; + /// let result = engine.eval_file::("script.rhai")?; /// # Ok(()) /// # } /// ``` #[inline] - pub fn eval_file(&self, path: PathBuf) -> RhaiResultOf { + pub fn eval_file(&self, path: impl AsRef) -> RhaiResultOf { Self::read_file(path).and_then(|contents| self.eval::(&contents)) } /// Evaluate a script file with own scope, returning the result value or an error. @@ -157,7 +155,7 @@ impl Engine { /// scope.push("x", 42_i64); /// /// // Notice that a PathBuf is required which can easily be constructed from a string. - /// let result = engine.eval_file_with_scope::(&mut scope, "script.rhai".into())?; + /// let result = engine.eval_file_with_scope::(&mut scope, "script.rhai")?; /// # Ok(()) /// # } /// ``` @@ -165,7 +163,7 @@ impl Engine { pub fn eval_file_with_scope( &self, scope: &mut Scope, - path: PathBuf, + path: impl AsRef, ) -> RhaiResultOf { Self::read_file(path).and_then(|contents| self.eval_with_scope(scope, &contents)) } @@ -182,12 +180,12 @@ impl Engine { /// let engine = Engine::new(); /// /// // Notice that a PathBuf is required which can easily be constructed from a string. - /// engine.run_file("script.rhai".into())?; + /// engine.run_file("script.rhai")?; /// # Ok(()) /// # } /// ``` #[inline] - pub fn run_file(&self, path: PathBuf) -> RhaiResultOf<()> { + pub fn run_file(&self, path: impl AsRef) -> RhaiResultOf<()> { Self::read_file(path).and_then(|contents| self.run(&contents)) } /// Evaluate a file with own scope. @@ -213,13 +211,12 @@ impl Engine { /// let mut scope = Scope::new(); /// scope.push("x", 42_i64); /// - /// // Notice that a PathBuf is required which can easily be constructed from a string. - /// engine.run_file_with_scope(&mut scope, "script.rhai".into())?; + /// engine.run_file_with_scope(&mut scope, "script.rhai")?; /// # Ok(()) /// # } /// ``` #[inline] - pub fn run_file_with_scope(&self, scope: &mut Scope, path: PathBuf) -> RhaiResultOf<()> { + pub fn run_file_with_scope(&self, scope: &mut Scope, path: impl AsRef) -> RhaiResultOf<()> { Self::read_file(path).and_then(|contents| self.run_with_scope(scope, &contents)) } } diff --git a/src/lib.rs b/src/lib.rs index b6be66ebb..5c05ec5c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,6 +26,7 @@ //! //! ```no_run //! use rhai::{Engine, EvalAltResult}; +//! use std::path::Path; //! //! fn main() -> Result<(), Box> //! { @@ -44,7 +45,7 @@ //! # #[cfg(not(target_family = "wasm"))] //! # //! // Evaluate the script, expecting a 'bool' result -//! let result: bool = engine.eval_file("my_script.rhai".into())?; +//! let result: bool = engine.eval_file(Path::new("my_script.rhai"))?; //! //! assert_eq!(result, true); //! diff --git a/src/types/immutable_string.rs b/src/types/immutable_string.rs index 0d251081d..74bf7f313 100644 --- a/src/types/immutable_string.rs +++ b/src/types/immutable_string.rs @@ -12,6 +12,7 @@ use std::{ iter::FromIterator, ops::{Add, AddAssign, Deref, Sub, SubAssign}, str::FromStr, + path::Path, }; /// The system immutable string type. @@ -142,6 +143,13 @@ impl From for SmartString { std::mem::take(shared_make_mut(&mut value.0)) } } +impl From<&Path> for ImmutableString { + #[inline(always)] + fn from(value: &Path) -> Self { + let value: SmartString = value.to_str().expect("UTF-8 encoding is expected").into(); + Self(value.into()) + } +} impl FromStr for ImmutableString { type Err = (); From 7317cdd2cc437c8958ebc64c109a9ee259f3afaf Mon Sep 17 00:00:00 2001 From: starovoid Date: Sat, 21 Jan 2023 17:36:37 +0300 Subject: [PATCH 2/2] Rm temp files --- .tool-versions | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .tool-versions diff --git a/.tool-versions b/.tool-versions deleted file mode 100644 index aa5e78b51..000000000 --- a/.tool-versions +++ /dev/null @@ -1 +0,0 @@ -rust 1.65.0 \ No newline at end of file