diff --git a/RELEASES.md b/RELEASES.md index 0b296b78..62fc4be6 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -10,6 +10,11 @@ Bug fixes * Constants are no longer propagated by the optimizer if shadowed by a non-constant variable. * Constants passed as the `this` parameter to Rhai functions now throws an error if assigned to. +Breaking changes +---------------- + +* `Engine::on_progress` now takes `u64` instead of `&u64`. + Enhancements ------------ diff --git a/doc/src/safety/progress.md b/doc/src/safety/progress.md index 44260b43..ff294976 100644 --- a/doc/src/safety/progress.md +++ b/doc/src/safety/progress.md @@ -13,7 +13,7 @@ the `Engine::on_progress` method: ```rust let mut engine = Engine::new(); -engine.on_progress(|&count| { // parameter is '&u64' - number of operations already performed +engine.on_progress(|count| { // parameter is number of operations already performed if count % 1000 == 0 { println!("{}", count); // print out a progress log every 1,000 operations } diff --git a/src/engine.rs b/src/engine.rs index b053af40..b3314482 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -3,7 +3,9 @@ use crate::ast::{Expr, FnCallExpr, Ident, IdentX, ReturnType, Stmt}; use crate::dynamic::{map_std_type_name, AccessMode, Union, Variant}; use crate::fn_call::run_builtin_op_assignment; -use crate::fn_native::{CallableFunction, Callback, IteratorFn, OnVarCallback}; +use crate::fn_native::{ + CallableFunction, IteratorFn, OnPrintCallback, OnProgressCallback, OnVarCallback, +}; use crate::module::NamespaceRef; use crate::optimize::OptimizationLevel; use crate::packages::{Package, PackagesCollection, StandardPackage}; @@ -622,11 +624,11 @@ pub struct Engine { pub(crate) resolve_var: Option, /// Callback closure for implementing the `print` command. - pub(crate) print: Callback, + pub(crate) print: OnPrintCallback, /// Callback closure for implementing the `debug` command. - pub(crate) debug: Callback, + pub(crate) debug: OnPrintCallback, /// Callback closure for progress reporting. - pub(crate) progress: Option>>, + pub(crate) progress: Option, /// Optimize the AST after compilation. pub(crate) optimization_level: OptimizationLevel, @@ -2542,7 +2544,7 @@ impl Engine { // Report progress - only in steps if let Some(progress) = &self.progress { - if let Some(token) = progress(&state.operations) { + if let Some(token) = progress(state.operations) { // Terminate script if progress returns a termination token return EvalAltResult::ErrorTerminated(token, Position::NONE).into(); } diff --git a/src/engine_api.rs b/src/engine_api.rs index 8169d791..6a2a5923 100644 --- a/src/engine_api.rs +++ b/src/engine_api.rs @@ -1726,7 +1726,7 @@ impl Engine { /// /// let mut engine = Engine::new(); /// - /// engine.on_progress(move |&ops| { + /// engine.on_progress(move |ops| { /// if ops > 10000 { /// Some("Over 10,000 operations!".into()) /// } else if ops % 800 == 0 { @@ -1748,7 +1748,7 @@ impl Engine { #[inline(always)] pub fn on_progress( &mut self, - callback: impl Fn(&u64) -> Option + SendSync + 'static, + callback: impl Fn(u64) -> Option + SendSync + 'static, ) -> &mut Self { self.progress = Some(Box::new(callback)); self diff --git a/src/fn_native.rs b/src/fn_native.rs index ee87de84..0706983d 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -343,18 +343,25 @@ pub type FnPlugin = dyn PluginFunction; #[cfg(feature = "sync")] pub type FnPlugin = dyn PluginFunction + Send + Sync; -/// A standard callback function. +/// A standard callback function for progress reporting. #[cfg(not(feature = "sync"))] -pub type Callback = Box R + 'static>; -/// A standard callback function. +pub type OnProgressCallback = Box Option + 'static>; +/// A standard callback function for progress reporting. #[cfg(feature = "sync")] -pub type Callback = Box R + Send + Sync + 'static>; +pub type OnProgressCallback = Box Option + Send + Sync + 'static>; -/// A standard callback function. +/// A standard callback function for printing. +#[cfg(not(feature = "sync"))] +pub type OnPrintCallback = Box; +/// A standard callback function for printing. +#[cfg(feature = "sync")] +pub type OnPrintCallback = Box; + +/// A standard callback function for variable access. #[cfg(not(feature = "sync"))] pub type OnVarCallback = Box Result, Box> + 'static>; -/// A standard callback function. +/// A standard callback function for variable access. #[cfg(feature = "sync")] pub type OnVarCallback = Box< dyn Fn(&str, usize, &EvalContext) -> Result, Box> diff --git a/tests/operations.rs b/tests/operations.rs index a0f52550..fc8c25cb 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); } @@ -117,7 +117,7 @@ fn test_max_operations_progress() -> Result<(), Box> { let mut engine = Engine::new(); engine.set_max_operations(500); - engine.on_progress(|&count| { + engine.on_progress(|count| { if count < 100 { None } else {