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