on_progress takes u64.

This commit is contained in:
Stephen Chung 2020-12-12 10:10:27 +08:00
parent bed29da71a
commit 4438c358d5
6 changed files with 32 additions and 18 deletions

View File

@ -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
------------

View File

@ -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
}

View File

@ -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<OnVarCallback>,
/// Callback closure for implementing the `print` command.
pub(crate) print: Callback<str, ()>,
pub(crate) print: OnPrintCallback,
/// Callback closure for implementing the `debug` command.
pub(crate) debug: Callback<str, ()>,
pub(crate) debug: OnPrintCallback,
/// Callback closure for progress reporting.
pub(crate) progress: Option<Callback<u64, Option<Dynamic>>>,
pub(crate) progress: Option<OnProgressCallback>,
/// 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();
}

View File

@ -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<Dynamic> + SendSync + 'static,
callback: impl Fn(u64) -> Option<Dynamic> + SendSync + 'static,
) -> &mut Self {
self.progress = Some(Box::new(callback));
self

View File

@ -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<T, R> = Box<dyn Fn(&T) -> R + 'static>;
/// A standard callback function.
pub type OnProgressCallback = Box<dyn Fn(u64) -> Option<Dynamic> + 'static>;
/// A standard callback function for progress reporting.
#[cfg(feature = "sync")]
pub type Callback<T, R> = Box<dyn Fn(&T) -> R + Send + Sync + 'static>;
pub type OnProgressCallback = Box<dyn Fn(u64) -> Option<Dynamic> + Send + Sync + 'static>;
/// A standard callback function.
/// A standard callback function for printing.
#[cfg(not(feature = "sync"))]
pub type OnPrintCallback = Box<dyn Fn(&str) + 'static>;
/// A standard callback function for printing.
#[cfg(feature = "sync")]
pub type OnPrintCallback<T, R> = Box<dyn Fn(&str) + Send + Sync + 'static>;
/// A standard callback function for variable access.
#[cfg(not(feature = "sync"))]
pub type OnVarCallback =
Box<dyn Fn(&str, usize, &EvalContext) -> Result<Option<Dynamic>, Box<EvalAltResult>> + '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<Option<Dynamic>, Box<EvalAltResult>>

View File

@ -6,7 +6,7 @@ fn test_max_operations() -> Result<(), Box<EvalAltResult>> {
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<EvalAltResult>> {
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<EvalAltResult>> {
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<EvalAltResult>> {
let mut engine = Engine::new();
engine.set_max_operations(500);
engine.on_progress(|&count| {
engine.on_progress(|count| {
if count < 100 {
None
} else {