Consolidate callbacks.

This commit is contained in:
Stephen Chung 2020-06-02 13:33:16 +08:00
parent b70d38e820
commit b8da1691d3
8 changed files with 28 additions and 29 deletions

View File

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

View File

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

View File

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

View File

@ -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<String, String>,
/// Closure for implementing the `print` command.
#[cfg(not(feature = "sync"))]
pub(crate) print: Box<dyn Fn(&str) + 'static>,
/// Closure for implementing the `print` command.
#[cfg(feature = "sync")]
pub(crate) print: Box<dyn Fn(&str) + Send + Sync + 'static>,
/// Closure for implementing the `debug` command.
#[cfg(not(feature = "sync"))]
pub(crate) debug: Box<dyn Fn(&str) + 'static>,
/// Closure for implementing the `debug` command.
#[cfg(feature = "sync")]
pub(crate) debug: Box<dyn Fn(&str) + Send + Sync + 'static>,
/// Closure for progress reporting.
#[cfg(not(feature = "sync"))]
pub(crate) progress: Option<Box<dyn Fn(u64) -> bool + 'static>>,
/// Closure for progress reporting.
#[cfg(feature = "sync")]
pub(crate) progress: Option<Box<dyn Fn(u64) -> bool + Send + Sync + 'static>>,
/// Callback closure for implementing the `print` command.
pub(crate) print: Callback<str, ()>,
/// Callback closure for implementing the `debug` command.
pub(crate) debug: Callback<str, ()>,
/// Callback closure for progress reporting.
pub(crate) progress: Option<Callback<u64, bool>>,
/// 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())));
}

View File

@ -57,6 +57,11 @@ pub type FnAny = dyn Fn(&mut FnCallArgs) -> Result<Dynamic, Box<EvalAltResult>>
pub type IteratorFn = fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>>;
#[cfg(not(feature = "sync"))]
pub type Callback<T, R> = Box<dyn Fn(&T) -> R + 'static>;
#[cfg(feature = "sync")]
pub type Callback<T, R> = Box<dyn Fn(&T) -> R + Send + Sync + 'static>;
/// A type encapsulating a function callable by Rhai.
#[derive(Clone)]
pub enum CallableFunction {

View File

@ -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<K: Into<Cow<'a, str>>>(&mut self, name: K, mut value: Module) {
pub fn push_module<K: Into<Cow<'a, str>>>(&mut self, name: K, value: Module) {
self.push_module_internal(name, value);
}

View File

@ -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"))]

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