diff --git a/src/engine.rs b/src/engine.rs index 6b79dc70..b9bb7825 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -850,9 +850,9 @@ pub struct Engine { pub(crate) resolve_var: Option, /// Callback closure for implementing the `print` command. - pub(crate) print: OnPrintCallback, + pub(crate) print: Option, /// Callback closure for implementing the `debug` command. - pub(crate) debug: OnDebugCallback, + pub(crate) debug: Option, /// Callback closure for progress reporting. #[cfg(not(feature = "unchecked"))] pub(crate) progress: Option, @@ -954,8 +954,8 @@ impl Engine { resolve_var: None, // default print/debug implementations - print: Box::new(default_print), - debug: Box::new(default_debug), + print: Some(Box::new(default_print)), + debug: Some(Box::new(default_debug)), // progress callback #[cfg(not(feature = "unchecked"))] @@ -1010,8 +1010,8 @@ impl Engine { resolve_var: None, - print: Box::new(|_| {}), - debug: Box::new(|_, _, _| {}), + print: None, + debug: None, #[cfg(not(feature = "unchecked"))] progress: None, @@ -2755,10 +2755,9 @@ impl Engine { err_map.insert("message".into(), err.to_string().into()); - state - .source - .as_ref() - .map(|source| err_map.insert("source".into(), source.into())); + if let Some(ref source) = state.source { + err_map.insert("source".into(), source.as_str().into()); + } if err_pos.is_none() { // No position info diff --git a/src/engine_api.rs b/src/engine_api.rs index c012e3b7..6a1b350b 100644 --- a/src/engine_api.rs +++ b/src/engine_api.rs @@ -2219,7 +2219,7 @@ impl Engine { /// ``` #[inline(always)] pub fn on_print(&mut self, callback: impl Fn(&str) + SendSync + 'static) -> &mut Self { - self.print = Box::new(callback); + self.print = Some(Box::new(callback)); self } /// Override default action of `debug` (print to stdout using [`println!`]) @@ -2258,7 +2258,7 @@ impl Engine { &mut self, callback: impl Fn(&str, Option<&str>, Position) + SendSync + 'static, ) -> &mut Self { - self.debug = Box::new(callback); + self.debug = Some(Box::new(callback)); self } } diff --git a/src/fn_call.rs b/src/fn_call.rs index b0693fad..d4251efb 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -319,25 +319,33 @@ impl Engine { // See if the function match print/debug (which requires special processing) return Ok(match name { KEYWORD_PRINT => { - let text = result.as_immutable_string().map_err(|typ| { - EvalAltResult::ErrorMismatchOutputType( - self.map_type_name(type_name::()).into(), - typ.into(), - pos, - ) - })?; - ((self.print)(&text).into(), false) + if let Some(ref print) = self.print { + let text = result.as_immutable_string().map_err(|typ| { + EvalAltResult::ErrorMismatchOutputType( + self.map_type_name(type_name::()).into(), + typ.into(), + pos, + ) + })?; + (print(&text).into(), false) + } else { + (Dynamic::UNIT, false) + } } KEYWORD_DEBUG => { - let text = result.as_immutable_string().map_err(|typ| { - EvalAltResult::ErrorMismatchOutputType( - self.map_type_name(type_name::()).into(), - typ.into(), - pos, - ) - })?; - let source = state.source.as_ref().map(|s| s.as_str()); - ((self.debug)(&text, source, pos).into(), false) + if let Some(ref debug) = self.debug { + let text = result.as_immutable_string().map_err(|typ| { + EvalAltResult::ErrorMismatchOutputType( + self.map_type_name(type_name::()).into(), + typ.into(), + pos, + ) + })?; + let source = state.source.as_ref().map(|s| s.as_str()); + (debug(&text, source, pos).into(), false) + } else { + (Dynamic::UNIT, false) + } } _ => (result, func.is_method()), });