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 ```rust
let mut engine = Engine::new(); 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 { if count % 1000 == 0 {
println!("{}", count); // print out a progress log every 1,000 operations println!("{}", count); // print out a progress log every 1,000 operations
} }

View File

@ -4,6 +4,11 @@ Rhai Release Notes
Version 0.16.0 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 Version 0.15.0
============== ==============

View File

@ -1163,7 +1163,7 @@ impl Engine {
/// ///
/// let mut engine = Engine::new(); /// let mut engine = Engine::new();
/// ///
/// engine.on_progress(move |ops| { /// engine.on_progress(move |&ops| {
/// if ops > 10000 { /// if ops > 10000 {
/// false /// false
/// } else if ops % 800 == 0 { /// } else if ops % 800 == 0 {
@ -1182,7 +1182,7 @@ impl Engine {
/// # Ok(()) /// # 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)); self.progress = Some(Box::new(callback));
} }

View File

@ -3,7 +3,7 @@
use crate::any::{Dynamic, Union}; use crate::any::{Dynamic, Union};
use crate::calc_fn_hash; use crate::calc_fn_hash;
use crate::error::ParseErrorType; 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::module::{resolvers, Module, ModuleResolver};
use crate::optimize::OptimizationLevel; use crate::optimize::OptimizationLevel;
use crate::packages::{CorePackage, Package, PackageLibrary, PackagesCollection, StandardPackage}; use crate::packages::{CorePackage, Package, PackageLibrary, PackagesCollection, StandardPackage};
@ -302,26 +302,12 @@ pub struct Engine {
/// A hashmap mapping type names to pretty-print names. /// A hashmap mapping type names to pretty-print names.
pub(crate) type_names: HashMap<String, String>, pub(crate) type_names: HashMap<String, String>,
/// Closure for implementing the `print` command. /// Callback closure for implementing the `print` command.
#[cfg(not(feature = "sync"))] pub(crate) print: Callback<str, ()>,
pub(crate) print: Box<dyn Fn(&str) + 'static>, /// Callback closure for implementing the `debug` command.
/// Closure for implementing the `print` command. pub(crate) debug: Callback<str, ()>,
#[cfg(feature = "sync")] /// Callback closure for progress reporting.
pub(crate) print: Box<dyn Fn(&str) + Send + Sync + 'static>, pub(crate) progress: Option<Callback<u64, bool>>,
/// 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>>,
/// Optimize the AST after compilation. /// Optimize the AST after compilation.
pub(crate) optimization_level: OptimizationLevel, pub(crate) optimization_level: OptimizationLevel,
@ -2012,7 +1998,7 @@ impl Engine {
// Report progress - only in steps // Report progress - only in steps
if let Some(progress) = &self.progress { if let Some(progress) = &self.progress {
if !progress(state.operations) { if !progress(&state.operations) {
// Terminate script if progress returns false // Terminate script if progress returns false
return Err(Box::new(EvalAltResult::ErrorTerminated(Position::none()))); 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>>; 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. /// A type encapsulating a function callable by Rhai.
#[derive(Clone)] #[derive(Clone)]
pub enum CallableFunction { 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. /// Modules are used for accessing member variables, functions and plugins under a namespace.
#[cfg(not(feature = "no_module"))] #[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); self.push_module_internal(name, value);
} }

View File

@ -198,6 +198,7 @@ pub enum Token {
XOrAssign, XOrAssign,
ModuloAssign, ModuloAssign,
PowerOfAssign, PowerOfAssign,
#[cfg(not(feature = "no_function"))]
Private, Private,
Import, Import,
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
@ -284,6 +285,7 @@ impl Token {
ModuloAssign => "%=", ModuloAssign => "%=",
PowerOf => "~", PowerOf => "~",
PowerOfAssign => "~=", PowerOfAssign => "~=",
#[cfg(not(feature = "no_function"))]
Private => "private", Private => "private",
Import => "import", Import => "import",
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
@ -757,6 +759,7 @@ impl<'a> TokenIterator<'a> {
"throw" => Token::Throw, "throw" => Token::Throw,
"for" => Token::For, "for" => Token::For,
"in" => Token::In, "in" => Token::In,
#[cfg(not(feature = "no_function"))]
"private" => Token::Private, "private" => Token::Private,
"import" => Token::Import, "import" => Token::Import,
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]

View File

@ -6,7 +6,7 @@ fn test_max_operations() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new(); let mut engine = Engine::new();
engine.set_max_operations(500); engine.set_max_operations(500);
engine.on_progress(|count| { engine.on_progress(|&count| {
if count % 100 == 0 { if count % 100 == 0 {
println!("{}", count); println!("{}", count);
} }
@ -34,7 +34,7 @@ fn test_max_operations_functions() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new(); let mut engine = Engine::new();
engine.set_max_operations(500); engine.set_max_operations(500);
engine.on_progress(|count| { engine.on_progress(|&count| {
if count % 100 == 0 { if count % 100 == 0 {
println!("{}", count); println!("{}", count);
} }
@ -90,7 +90,7 @@ fn test_max_operations_eval() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new(); let mut engine = Engine::new();
engine.set_max_operations(500); engine.set_max_operations(500);
engine.on_progress(|count| { engine.on_progress(|&count| {
if count % 100 == 0 { if count % 100 == 0 {
println!("{}", count); println!("{}", count);
} }