From 3116a393312db60ab301b93ab7c74313be904a8f Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 18 May 2021 12:24:11 +0800 Subject: [PATCH] Minor code refactor. --- CHANGELOG.md | 2 +- src/engine.rs | 12 ++---------- src/engine_api.rs | 11 +++-------- src/module/mod.rs | 30 +++++++++++++++--------------- src/module/resolvers/dummy.rs | 4 ++-- src/optimize.rs | 28 +++++++++------------------- src/scope.rs | 10 +++++----- 7 files changed, 37 insertions(+), 60 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d73c19f..1082da15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ Breaking changes 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. * `DynamicReadLock` and `DynamicWriteLoc` are exposed under `internals`. * `From>>` is added for `Dynamic` mapping directly to a shared value, together with support for `Dynamic::from`. diff --git a/src/engine.rs b/src/engine.rs index fe2bcc7a..948278c7 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -899,11 +899,7 @@ impl Engine { progress: None, // optimization level - optimization_level: if cfg!(feature = "no_optimize") { - OptimizationLevel::None - } else { - OptimizationLevel::Simple - }, + optimization_level: Default::default(), #[cfg(not(feature = "unchecked"))] limits: Limits { @@ -956,11 +952,7 @@ impl Engine { #[cfg(not(feature = "unchecked"))] progress: None, - optimization_level: if cfg!(feature = "no_optimize") { - OptimizationLevel::None - } else { - OptimizationLevel::Simple - }, + optimization_level: Default::default(), #[cfg(not(feature = "unchecked"))] limits: Limits { diff --git a/src/engine_api.rs b/src/engine_api.rs index 152a5338..56f58522 100644 --- a/src/engine_api.rs +++ b/src/engine_api.rs @@ -477,9 +477,7 @@ impl Engine { set_fn: impl Fn(&mut T, V) -> Result<(), Box> + SendSync + 'static, ) -> &mut Self { use crate::engine::make_setter; - self.register_result_fn(&make_setter(name), move |obj: &mut T, value: V| { - set_fn(obj, value) - }) + self.register_result_fn(&make_setter(name), set_fn) } /// Short-hand for registering both getter and setter functions /// of a registered type with the [`Engine`]. @@ -807,12 +805,9 @@ impl Engine { panic!("Cannot register indexer for strings."); } - self.register_result_fn( - crate::engine::FN_IDX_SET, - move |obj: &mut T, index: X, value: V| set_fn(obj, index, value), - ) + self.register_result_fn(crate::engine::FN_IDX_SET, set_fn) } - /// 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`. /// diff --git a/src/module/mod.rs b/src/module/mod.rs index 16c93374..25830f17 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -156,20 +156,7 @@ pub struct Module { impl Default for Module { #[inline(always)] fn default() -> Self { - 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(), - } + Self::new() } } @@ -257,7 +244,20 @@ impl Module { /// ``` #[inline(always)] 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. diff --git a/src/module/resolvers/dummy.rs b/src/module/resolvers/dummy.rs index 3feb6852..15c706f2 100644 --- a/src/module/resolvers/dummy.rs +++ b/src/module/resolvers/dummy.rs @@ -31,8 +31,8 @@ impl DummyModuleResolver { /// engine.set_module_resolver(resolver); /// ``` #[inline(always)] - pub fn new() -> Self { - Default::default() + pub const fn new() -> Self { + Self } } diff --git a/src/optimize.rs b/src/optimize.rs index aab1b20a..74a4c7ef 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -31,24 +31,14 @@ pub enum OptimizationLevel { Full, } -impl OptimizationLevel { - /// Is the `OptimizationLevel` [`None`][OptimizationLevel::None]? - #[allow(dead_code)] +impl Default for OptimizationLevel { #[inline(always)] - pub fn is_none(self) -> bool { - self == Self::None - } - /// Is the `OptimizationLevel` [`Simple`][OptimizationLevel::Simple]? - #[allow(dead_code)] - #[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 + fn default() -> Self { + if cfg!(feature = "no_optimize") { + Self::None + } else { + Self::Simple + } } } @@ -1078,7 +1068,7 @@ pub fn optimize_into_ast( optimization_level: OptimizationLevel, ) -> AST { let level = if cfg!(feature = "no_optimize") { - OptimizationLevel::None + Default::default() } else { optimization_level }; @@ -1087,7 +1077,7 @@ pub fn optimize_into_ast( let lib = { let mut module = Module::new(); - if !level.is_none() { + if level != OptimizationLevel::None { // We only need the script library's signatures for optimization purposes let mut lib2 = Module::new(); diff --git a/src/scope.rs b/src/scope.rs index 373ad0cb..ed0bacd1 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -62,10 +62,7 @@ pub struct Scope<'a> { impl Default for Scope<'_> { #[inline(always)] fn default() -> Self { - Self { - values: Default::default(), - names: Vec::with_capacity(SCOPE_SIZE), - } + Self::new() } } @@ -99,7 +96,10 @@ impl<'a> Scope<'a> { /// ``` #[inline(always)] pub fn new() -> Self { - Default::default() + Self { + values: Default::default(), + names: Vec::with_capacity(SCOPE_SIZE), + } } /// Empty the [`Scope`]. ///