diff --git a/README.md b/README.md index aaf394fc..1333579b 100644 --- a/README.md +++ b/README.md @@ -2439,7 +2439,7 @@ provide a closure to the `Engine::on_progress` method: ```rust let mut engine = Engine::new(); -engine.on_progress(|count| { // 'count' is the number of operations performed +engine.on_progress(|&count| { // 'count' is the number of operations performed if count % 1000 == 0 { println!("{}", count); // print out a progress log every 1,000 operations } diff --git a/RELEASES.md b/RELEASES.md index 05da1a88..7ba44391 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -4,6 +4,11 @@ Rhai Release Notes Version 0.16.0 ============== +Breaking changes +---------------- + +* Callback closure passed to `Engine::on_progress` now takes `&u64` instead of `u64` to be consistent with other callback signatures. + Version 0.15.0 ============== diff --git a/src/api.rs b/src/api.rs index 6afc7a35..dc0a26d0 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1163,7 +1163,7 @@ impl Engine { /// /// let mut engine = Engine::new(); /// - /// engine.on_progress(move |ops| { + /// engine.on_progress(move |&ops| { /// if ops > 10000 { /// false /// } else if ops % 800 == 0 { @@ -1182,7 +1182,7 @@ impl Engine { /// # Ok(()) /// # } /// ``` - pub fn on_progress(&mut self, callback: impl Fn(u64) -> bool + SendSync + 'static) { + pub fn on_progress(&mut self, callback: impl Fn(&u64) -> bool + SendSync + 'static) { self.progress = Some(Box::new(callback)); } diff --git a/src/engine.rs b/src/engine.rs index 21561e70..a5001df3 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -3,7 +3,7 @@ use crate::any::{Dynamic, Union}; use crate::calc_fn_hash; use crate::error::ParseErrorType; -use crate::fn_native::{CallableFunction, FnCallArgs, Shared}; +use crate::fn_native::{CallableFunction, Callback, FnCallArgs, Shared}; use crate::module::{resolvers, Module, ModuleResolver}; use crate::optimize::OptimizationLevel; use crate::packages::{CorePackage, Package, PackageLibrary, PackagesCollection, StandardPackage}; @@ -302,26 +302,12 @@ pub struct Engine { /// A hashmap mapping type names to pretty-print names. pub(crate) type_names: HashMap, - /// Closure for implementing the `print` command. - #[cfg(not(feature = "sync"))] - pub(crate) print: Box, - /// Closure for implementing the `print` command. - #[cfg(feature = "sync")] - pub(crate) print: Box, - - /// Closure for implementing the `debug` command. - #[cfg(not(feature = "sync"))] - pub(crate) debug: Box, - /// Closure for implementing the `debug` command. - #[cfg(feature = "sync")] - pub(crate) debug: Box, - - /// Closure for progress reporting. - #[cfg(not(feature = "sync"))] - pub(crate) progress: Option bool + 'static>>, - /// Closure for progress reporting. - #[cfg(feature = "sync")] - pub(crate) progress: Option bool + Send + Sync + 'static>>, + /// Callback closure for implementing the `print` command. + pub(crate) print: Callback, + /// Callback closure for implementing the `debug` command. + pub(crate) debug: Callback, + /// Callback closure for progress reporting. + pub(crate) progress: Option>, /// Optimize the AST after compilation. pub(crate) optimization_level: OptimizationLevel, @@ -2012,7 +1998,7 @@ impl Engine { // Report progress - only in steps if let Some(progress) = &self.progress { - if !progress(state.operations) { + if !progress(&state.operations) { // Terminate script if progress returns false return Err(Box::new(EvalAltResult::ErrorTerminated(Position::none()))); } diff --git a/src/fn_native.rs b/src/fn_native.rs index 6c43adc6..334068b5 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -57,6 +57,11 @@ pub type FnAny = dyn Fn(&mut FnCallArgs) -> Result> pub type IteratorFn = fn(Dynamic) -> Box>; +#[cfg(not(feature = "sync"))] +pub type Callback = Box R + 'static>; +#[cfg(feature = "sync")] +pub type Callback = Box R + Send + Sync + 'static>; + /// A type encapsulating a function callable by Rhai. #[derive(Clone)] pub enum CallableFunction { diff --git a/src/scope.rs b/src/scope.rs index 50a5694f..bb15b777 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -175,7 +175,7 @@ impl<'a> Scope<'a> { /// /// Modules are used for accessing member variables, functions and plugins under a namespace. #[cfg(not(feature = "no_module"))] - pub fn push_module>>(&mut self, name: K, mut value: Module) { + pub fn push_module>>(&mut self, name: K, value: Module) { self.push_module_internal(name, value); } diff --git a/src/token.rs b/src/token.rs index 9e6613b0..2967fc02 100644 --- a/src/token.rs +++ b/src/token.rs @@ -198,6 +198,7 @@ pub enum Token { XOrAssign, ModuloAssign, PowerOfAssign, + #[cfg(not(feature = "no_function"))] Private, Import, #[cfg(not(feature = "no_module"))] @@ -284,6 +285,7 @@ impl Token { ModuloAssign => "%=", PowerOf => "~", PowerOfAssign => "~=", + #[cfg(not(feature = "no_function"))] Private => "private", Import => "import", #[cfg(not(feature = "no_module"))] @@ -757,6 +759,7 @@ impl<'a> TokenIterator<'a> { "throw" => Token::Throw, "for" => Token::For, "in" => Token::In, + #[cfg(not(feature = "no_function"))] "private" => Token::Private, "import" => Token::Import, #[cfg(not(feature = "no_module"))] diff --git a/tests/operations.rs b/tests/operations.rs index c02e2381..91df26c9 100644 --- a/tests/operations.rs +++ b/tests/operations.rs @@ -6,7 +6,7 @@ fn test_max_operations() -> Result<(), Box> { let mut engine = Engine::new(); engine.set_max_operations(500); - engine.on_progress(|count| { + engine.on_progress(|&count| { if count % 100 == 0 { println!("{}", count); } @@ -34,7 +34,7 @@ fn test_max_operations_functions() -> Result<(), Box> { let mut engine = Engine::new(); engine.set_max_operations(500); - engine.on_progress(|count| { + engine.on_progress(|&count| { if count % 100 == 0 { println!("{}", count); } @@ -90,7 +90,7 @@ fn test_max_operations_eval() -> Result<(), Box> { let mut engine = Engine::new(); engine.set_max_operations(500); - engine.on_progress(|count| { + engine.on_progress(|&count| { if count % 100 == 0 { println!("{}", count); }