Minor code refactor.

This commit is contained in:
Stephen Chung 2021-05-18 12:24:11 +08:00
parent d1fc362eec
commit 3116a39331
7 changed files with 37 additions and 60 deletions

View File

@ -19,7 +19,7 @@ Breaking changes
New features New features
------------ ------------
* Each `Dynamic` value can now contain arbitrary data (type `i16`) in the form of a _tag_. This is to use up otherwise wasted space in the `Dynamic` type. * Each `Dynamic` value can now contain arbitrary data (type `i32`) in the form of a _tag_. This is to use up otherwise wasted space in the `Dynamic` type.
* A new internal feature `no_smartstring` to turn off `SmartString` for those rare cases that it is needed. * A new internal feature `no_smartstring` to turn off `SmartString` for those rare cases that it is needed.
* `DynamicReadLock` and `DynamicWriteLoc` are exposed under `internals`. * `DynamicReadLock` and `DynamicWriteLoc` are exposed under `internals`.
* `From<Shared<Locked<Dynamic>>>` is added for `Dynamic` mapping directly to a shared value, together with support for `Dynamic::from`. * `From<Shared<Locked<Dynamic>>>` is added for `Dynamic` mapping directly to a shared value, together with support for `Dynamic::from`.

View File

@ -899,11 +899,7 @@ impl Engine {
progress: None, progress: None,
// optimization level // optimization level
optimization_level: if cfg!(feature = "no_optimize") { optimization_level: Default::default(),
OptimizationLevel::None
} else {
OptimizationLevel::Simple
},
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
limits: Limits { limits: Limits {
@ -956,11 +952,7 @@ impl Engine {
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
progress: None, progress: None,
optimization_level: if cfg!(feature = "no_optimize") { optimization_level: Default::default(),
OptimizationLevel::None
} else {
OptimizationLevel::Simple
},
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
limits: Limits { limits: Limits {

View File

@ -477,9 +477,7 @@ impl Engine {
set_fn: impl Fn(&mut T, V) -> Result<(), Box<EvalAltResult>> + SendSync + 'static, set_fn: impl Fn(&mut T, V) -> Result<(), Box<EvalAltResult>> + SendSync + 'static,
) -> &mut Self { ) -> &mut Self {
use crate::engine::make_setter; use crate::engine::make_setter;
self.register_result_fn(&make_setter(name), move |obj: &mut T, value: V| { self.register_result_fn(&make_setter(name), set_fn)
set_fn(obj, value)
})
} }
/// Short-hand for registering both getter and setter functions /// Short-hand for registering both getter and setter functions
/// of a registered type with the [`Engine`]. /// of a registered type with the [`Engine`].
@ -807,12 +805,9 @@ impl Engine {
panic!("Cannot register indexer for strings."); panic!("Cannot register indexer for strings.");
} }
self.register_result_fn( self.register_result_fn(crate::engine::FN_IDX_SET, set_fn)
crate::engine::FN_IDX_SET,
move |obj: &mut T, index: X, value: V| set_fn(obj, index, value),
)
} }
/// Short-hand for register both index getter and setter functions for a custom type with the [`Engine`]. /// Short-hand for registering both index getter and setter functions for a custom type with the [`Engine`].
/// ///
/// Not available under `no_index`. /// Not available under `no_index`.
/// ///

View File

@ -156,20 +156,7 @@ pub struct Module {
impl Default for Module { impl Default for Module {
#[inline(always)] #[inline(always)]
fn default() -> Self { fn default() -> Self {
Self { Self::new()
id: None,
internal: false,
modules: Default::default(),
variables: Default::default(),
all_variables: Default::default(),
functions: Default::default(),
all_functions: Default::default(),
type_iterators: Default::default(),
all_type_iterators: Default::default(),
indexed: true,
contains_indexed_global_functions: false,
identifiers: Default::default(),
}
} }
} }
@ -257,7 +244,20 @@ impl Module {
/// ``` /// ```
#[inline(always)] #[inline(always)]
pub fn new() -> Self { pub fn new() -> Self {
Default::default() Self {
id: None,
internal: false,
modules: Default::default(),
variables: Default::default(),
all_variables: Default::default(),
functions: Default::default(),
all_functions: Default::default(),
type_iterators: Default::default(),
all_type_iterators: Default::default(),
indexed: true,
contains_indexed_global_functions: false,
identifiers: Default::default(),
}
} }
/// Get the ID of the [`Module`], if any. /// Get the ID of the [`Module`], if any.

View File

@ -31,8 +31,8 @@ impl DummyModuleResolver {
/// engine.set_module_resolver(resolver); /// engine.set_module_resolver(resolver);
/// ``` /// ```
#[inline(always)] #[inline(always)]
pub fn new() -> Self { pub const fn new() -> Self {
Default::default() Self
} }
} }

View File

@ -31,24 +31,14 @@ pub enum OptimizationLevel {
Full, Full,
} }
impl OptimizationLevel { impl Default for OptimizationLevel {
/// Is the `OptimizationLevel` [`None`][OptimizationLevel::None]?
#[allow(dead_code)]
#[inline(always)] #[inline(always)]
pub fn is_none(self) -> bool { fn default() -> Self {
self == Self::None if cfg!(feature = "no_optimize") {
} Self::None
/// Is the `OptimizationLevel` [`Simple`][OptimizationLevel::Simple]? } else {
#[allow(dead_code)] Self::Simple
#[inline(always)] }
pub fn is_simple(self) -> bool {
self == Self::Simple
}
/// Is the `OptimizationLevel` [`Full`][OptimizationLevel::Full]?
#[allow(dead_code)]
#[inline(always)]
pub fn is_full(self) -> bool {
self == Self::Full
} }
} }
@ -1078,7 +1068,7 @@ pub fn optimize_into_ast(
optimization_level: OptimizationLevel, optimization_level: OptimizationLevel,
) -> AST { ) -> AST {
let level = if cfg!(feature = "no_optimize") { let level = if cfg!(feature = "no_optimize") {
OptimizationLevel::None Default::default()
} else { } else {
optimization_level optimization_level
}; };
@ -1087,7 +1077,7 @@ pub fn optimize_into_ast(
let lib = { let lib = {
let mut module = Module::new(); let mut module = Module::new();
if !level.is_none() { if level != OptimizationLevel::None {
// We only need the script library's signatures for optimization purposes // We only need the script library's signatures for optimization purposes
let mut lib2 = Module::new(); let mut lib2 = Module::new();

View File

@ -62,10 +62,7 @@ pub struct Scope<'a> {
impl Default for Scope<'_> { impl Default for Scope<'_> {
#[inline(always)] #[inline(always)]
fn default() -> Self { fn default() -> Self {
Self { Self::new()
values: Default::default(),
names: Vec::with_capacity(SCOPE_SIZE),
}
} }
} }
@ -99,7 +96,10 @@ impl<'a> Scope<'a> {
/// ``` /// ```
#[inline(always)] #[inline(always)]
pub fn new() -> Self { pub fn new() -> Self {
Default::default() Self {
values: Default::default(),
names: Vec::with_capacity(SCOPE_SIZE),
}
} }
/// Empty the [`Scope`]. /// Empty the [`Scope`].
/// ///