Reduce unnecessary Option's.
This commit is contained in:
parent
c2bb1f48c2
commit
d043300ae2
14
src/api.rs
14
src/api.rs
@ -146,14 +146,8 @@ impl Engine {
|
||||
/// ```
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
pub fn register_type_with_name<T: Variant + Clone>(&mut self, name: &str) {
|
||||
if self.type_names.is_none() {
|
||||
self.type_names = Some(HashMap::new());
|
||||
}
|
||||
|
||||
// Add the pretty-print type name into the map
|
||||
self.type_names
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.insert(type_name::<T>().to_string(), name.to_string());
|
||||
}
|
||||
|
||||
@ -994,7 +988,7 @@ impl Engine {
|
||||
/// ```
|
||||
#[cfg(feature = "sync")]
|
||||
pub fn on_print(&mut self, callback: impl Fn(&str) + Send + Sync + 'static) {
|
||||
self.on_print = Some(Box::new(callback));
|
||||
self.print = Box::new(callback);
|
||||
}
|
||||
/// Override default action of `print` (print to stdout using `println!`)
|
||||
///
|
||||
@ -1022,7 +1016,7 @@ impl Engine {
|
||||
/// ```
|
||||
#[cfg(not(feature = "sync"))]
|
||||
pub fn on_print(&mut self, callback: impl Fn(&str) + 'static) {
|
||||
self.on_print = Some(Box::new(callback));
|
||||
self.print = Box::new(callback);
|
||||
}
|
||||
|
||||
/// Override default action of `debug` (print to stdout using `println!`)
|
||||
@ -1051,7 +1045,7 @@ impl Engine {
|
||||
/// ```
|
||||
#[cfg(feature = "sync")]
|
||||
pub fn on_debug(&mut self, callback: impl Fn(&str) + Send + Sync + 'static) {
|
||||
self.on_debug = Some(Box::new(callback));
|
||||
self.debug = Box::new(callback);
|
||||
}
|
||||
/// Override default action of `debug` (print to stdout using `println!`)
|
||||
///
|
||||
@ -1079,6 +1073,6 @@ impl Engine {
|
||||
/// ```
|
||||
#[cfg(not(feature = "sync"))]
|
||||
pub fn on_debug(&mut self, callback: impl Fn(&str) + 'static) {
|
||||
self.on_debug = Some(Box::new(callback));
|
||||
self.debug = Box::new(callback);
|
||||
}
|
||||
}
|
||||
|
@ -295,21 +295,21 @@ pub struct Engine {
|
||||
/// A hashmap containing all iterators known to the engine.
|
||||
pub(crate) type_iterators: HashMap<TypeId, Box<IteratorFn>>,
|
||||
/// A hashmap mapping type names to pretty-print names.
|
||||
pub(crate) type_names: Option<HashMap<String, String>>,
|
||||
pub(crate) type_names: HashMap<String, String>,
|
||||
|
||||
/// Closure for implementing the `print` command.
|
||||
#[cfg(feature = "sync")]
|
||||
pub(crate) on_print: Option<Box<dyn Fn(&str) + Send + Sync + 'static>>,
|
||||
pub(crate) print: Box<dyn Fn(&str) + Send + Sync + 'static>,
|
||||
/// Closure for implementing the `print` command.
|
||||
#[cfg(not(feature = "sync"))]
|
||||
pub(crate) on_print: Option<Box<dyn Fn(&str) + 'static>>,
|
||||
pub(crate) print: Box<dyn Fn(&str) + 'static>,
|
||||
|
||||
/// Closure for implementing the `debug` command.
|
||||
#[cfg(feature = "sync")]
|
||||
pub(crate) on_debug: Option<Box<dyn Fn(&str) + Send + Sync + 'static>>,
|
||||
pub(crate) debug: Box<dyn Fn(&str) + Send + Sync + 'static>,
|
||||
/// Closure for implementing the `debug` command.
|
||||
#[cfg(not(feature = "sync"))]
|
||||
pub(crate) on_debug: Option<Box<dyn Fn(&str) + 'static>>,
|
||||
pub(crate) debug: Box<dyn Fn(&str) + 'static>,
|
||||
|
||||
/// Optimize the AST after compilation.
|
||||
pub(crate) optimization_level: OptimizationLevel,
|
||||
@ -327,11 +327,11 @@ impl Default for Engine {
|
||||
packages: Vec::new(),
|
||||
functions: HashMap::with_capacity(FUNCTIONS_COUNT),
|
||||
type_iterators: HashMap::new(),
|
||||
type_names: None,
|
||||
type_names: HashMap::new(),
|
||||
|
||||
// default print/debug implementations
|
||||
on_print: Some(Box::new(default_print)),
|
||||
on_debug: Some(Box::new(default_print)),
|
||||
print: Box::new(default_print),
|
||||
debug: Box::new(default_print),
|
||||
|
||||
// optimization level
|
||||
#[cfg(feature = "no_optimize")]
|
||||
@ -459,9 +459,9 @@ impl Engine {
|
||||
packages: Vec::new(),
|
||||
functions: HashMap::with_capacity(FUNCTIONS_COUNT / 2),
|
||||
type_iterators: HashMap::new(),
|
||||
type_names: None,
|
||||
on_print: None,
|
||||
on_debug: None,
|
||||
type_names: HashMap::new(),
|
||||
print: Box::new(|_| {}),
|
||||
debug: Box::new(|_| {}),
|
||||
|
||||
#[cfg(feature = "no_optimize")]
|
||||
optimization_level: OptimizationLevel::None,
|
||||
@ -539,25 +539,20 @@ impl Engine {
|
||||
|
||||
// See if the function match print/debug (which requires special processing)
|
||||
return Ok(match fn_name {
|
||||
KEYWORD_PRINT if self.on_print.is_some() => {
|
||||
self.on_print.as_ref().unwrap()(result.as_str().map_err(|type_name| {
|
||||
KEYWORD_PRINT => (self.print)(result.as_str().map_err(|type_name| {
|
||||
Box::new(EvalAltResult::ErrorMismatchOutputType(
|
||||
type_name.into(),
|
||||
pos,
|
||||
))
|
||||
})?)
|
||||
.into()
|
||||
}
|
||||
KEYWORD_DEBUG if self.on_debug.is_some() => {
|
||||
self.on_debug.as_ref().unwrap()(result.as_str().map_err(|type_name| {
|
||||
.into(),
|
||||
KEYWORD_DEBUG => (self.debug)(result.as_str().map_err(|type_name| {
|
||||
Box::new(EvalAltResult::ErrorMismatchOutputType(
|
||||
type_name.into(),
|
||||
pos,
|
||||
))
|
||||
})?)
|
||||
.into()
|
||||
}
|
||||
KEYWORD_PRINT | KEYWORD_DEBUG => ().into(),
|
||||
.into(),
|
||||
_ => result,
|
||||
});
|
||||
}
|
||||
@ -1493,8 +1488,8 @@ impl Engine {
|
||||
/// Map a type_name into a pretty-print name
|
||||
pub(crate) fn map_type_name<'a>(&'a self, name: &'a str) -> &'a str {
|
||||
self.type_names
|
||||
.as_ref()
|
||||
.and_then(|list| list.get(name).map(String::as_str))
|
||||
.get(name)
|
||||
.map(String::as_str)
|
||||
.unwrap_or(name)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user