From 2e724b804e0c5fca08c25118d19ac98e97346df7 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 23 Mar 2023 13:37:10 +0800 Subject: [PATCH] Remove indirection. --- src/api/custom_syntax.rs | 10 +++++----- src/api/definitions/mod.rs | 4 ++-- src/api/formatting.rs | 4 ++-- src/api/mod.rs | 6 +++--- src/api/register.rs | 2 +- src/engine.rs | 10 ++++------ src/eval/stmt.rs | 2 +- src/func/call.rs | 4 ++-- src/func/script.rs | 2 +- src/module/mod.rs | 36 ++++++++++++++++++------------------ src/optimizer.rs | 2 +- src/packages/lang_core.rs | 2 +- src/parser.rs | 16 ++++++++-------- src/serde/metadata.rs | 2 +- src/tokenizer.rs | 12 ++++++------ 15 files changed, 56 insertions(+), 58 deletions(-) diff --git a/src/api/custom_syntax.rs b/src/api/custom_syntax.rs index acd01195..e5dfb56c 100644 --- a/src/api/custom_syntax.rs +++ b/src/api/custom_syntax.rs @@ -261,12 +261,12 @@ impl Engine { // Make it a custom keyword/symbol if it is disabled or reserved if (self .disabled_symbols - .as_deref() + .as_ref() .map_or(false, |m| m.contains(s)) || token.as_ref().map_or(false, Token::is_reserved)) && !self .custom_keywords - .as_deref() + .as_ref() .map_or(false, |m| m.contains_key(s)) { self.custom_keywords @@ -281,7 +281,7 @@ impl Engine { && token.as_ref().map_or(false, Token::is_standard_keyword) && !self .disabled_symbols - .as_deref() + .as_ref() .map_or(false, |m| m.contains(s)) => { return Err(LexError::ImproperSymbol( @@ -301,12 +301,12 @@ impl Engine { // Make it a custom keyword/symbol if it is disabled or reserved if self .disabled_symbols - .as_deref() + .as_ref() .map_or(false, |m| m.contains(s)) || (token.as_ref().map_or(false, Token::is_reserved) && !self .custom_keywords - .as_deref() + .as_ref() .map_or(false, |m| m.contains_key(s))) { self.custom_keywords diff --git a/src/api/definitions/mod.rs b/src/api/definitions/mod.rs index a34a5ac5..69224b27 100644 --- a/src/api/definitions/mod.rs +++ b/src/api/definitions/mod.rs @@ -372,7 +372,7 @@ impl Definitions<'_> { let mut m = self .engine .global_sub_modules - .as_deref() + .as_ref() .into_iter() .flatten() .map(move |(name, module)| { @@ -461,7 +461,7 @@ impl Module { || def .engine .custom_keywords - .as_deref() + .as_ref() .map_or(false, |m| m.contains_key(f.metadata.name.as_str())); f.write_definition(writer, def, operator)?; diff --git a/src/api/formatting.rs b/src/api/formatting.rs index 07441f49..7fb7a376 100644 --- a/src/api/formatting.rs +++ b/src/api/formatting.rs @@ -208,7 +208,7 @@ impl Engine { #[cfg(not(feature = "no_module"))] return self .global_sub_modules - .as_deref() + .as_ref() .into_iter() .flatten() .find_map(|(_, m)| m.get_custom_type(name)); @@ -243,7 +243,7 @@ impl Engine { #[cfg(not(feature = "no_module"))] return self .global_sub_modules - .as_deref() + .as_ref() .into_iter() .flatten() .find_map(|(_, m)| m.get_custom_type(name)); diff --git a/src/api/mod.rs b/src/api/mod.rs index a2cf21ce..21238f5a 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -171,7 +171,7 @@ impl Engine { Some(token) if token.is_standard_keyword() => { if !self .disabled_symbols - .as_deref() + .as_ref() .map_or(false, |m| m.contains(token.literal_syntax())) { return Err(format!("'{keyword}' is a reserved keyword")); @@ -181,7 +181,7 @@ impl Engine { Some(token) if token.is_standard_symbol() => { if !self .disabled_symbols - .as_deref() + .as_ref() .map_or(false, |m| m.contains(token.literal_syntax())) { return Err(format!("'{keyword}' is a reserved operator")); @@ -191,7 +191,7 @@ impl Engine { Some(token) if !self .disabled_symbols - .as_deref() + .as_ref() .map_or(false, |m| m.contains(token.literal_syntax())) => { return Err(format!("'{keyword}' is a reserved symbol")) diff --git a/src/api/register.rs b/src/api/register.rs index 43c43a72..8f4d4833 100644 --- a/src/api/register.rs +++ b/src/api/register.rs @@ -786,7 +786,7 @@ impl Engine { } #[cfg(not(feature = "no_module"))] - for (name, m) in self.global_sub_modules.as_deref().into_iter().flatten() { + for (name, m) in self.global_sub_modules.as_ref().into_iter().flatten() { signatures.extend(m.gen_fn_signatures().map(|f| format!("{name}::{f}"))); } diff --git a/src/engine.rs b/src/engine.rs index 1e35d21f..59516a82 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -96,8 +96,7 @@ pub struct Engine { pub(crate) global_modules: StaticVec, /// A collection of all sub-modules directly loaded into the Engine. #[cfg(not(feature = "no_module"))] - pub(crate) global_sub_modules: - Option>>, + pub(crate) global_sub_modules: Option>, /// A module resolution service. #[cfg(not(feature = "no_module"))] @@ -107,15 +106,14 @@ pub struct Engine { pub(crate) interned_strings: Option>>, /// A set of symbols to disable. - pub(crate) disabled_symbols: Option>>, + pub(crate) disabled_symbols: Option>, /// A map containing custom keywords and precedence to recognize. #[cfg(not(feature = "no_custom_syntax"))] - pub(crate) custom_keywords: - Option>>>, + pub(crate) custom_keywords: Option>>, /// Custom syntax. #[cfg(not(feature = "no_custom_syntax"))] pub(crate) custom_syntax: Option< - Box>>, + std::collections::BTreeMap>, >, /// Callback closure for filtering variable definition. pub(crate) def_var_filter: Option>, diff --git a/src/eval/stmt.rs b/src/eval/stmt.rs index 18ffb037..021417d7 100644 --- a/src/eval/stmt.rs +++ b/src/eval/stmt.rs @@ -664,7 +664,7 @@ impl Engine { .or_else(|| global.get_iter(iter_type)) .or_else(|| { self.global_sub_modules - .as_deref() + .as_ref() .into_iter() .flatten() .find_map(|(_, m)| m.get_qualified_iter(iter_type)) diff --git a/src/func/call.rs b/src/func/call.rs index e834b750..61d54818 100644 --- a/src/func/call.rs +++ b/src/func/call.rs @@ -206,7 +206,7 @@ impl Engine { .or_else(|| _global.get_qualified_fn(hash, true)) .or_else(|| { self.global_sub_modules - .as_deref() + .as_ref() .into_iter() .flatten() .filter(|(_, m)| m.contains_indexed_global_functions()) @@ -248,7 +248,7 @@ impl Engine { #[cfg(not(feature = "no_module"))] let is_dynamic = is_dynamic || _global.may_contain_dynamic_fn(hash_base) - || self.global_sub_modules.as_deref().map_or(false, |m| { + || self.global_sub_modules.as_ref().map_or(false, |m| { m.values().any(|m| m.may_contain_dynamic_fn(hash_base)) }); diff --git a/src/func/script.rs b/src/func/script.rs index d385f389..98551fe5 100644 --- a/src/func/script.rs +++ b/src/func/script.rs @@ -219,7 +219,7 @@ impl Engine { // Then check imported modules global.contains_qualified_fn(hash_script) // Then check sub-modules - || self.global_sub_modules.as_deref().map_or(false, |m| { + || self.global_sub_modules.as_ref().map_or(false, |m| { m.values().any(|m| m.contains_qualified_fn(hash_script)) }); diff --git a/src/module/mod.rs b/src/module/mod.rs index 84b27c4b..26dbca16 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -195,22 +195,22 @@ pub struct Module { /// Custom types. custom_types: Option>, /// Sub-modules. - modules: Option>>, + modules: Option>, /// [`Module`] variables. - variables: Option>>, + variables: Option>, /// Flattened collection of all [`Module`] variables, including those in sub-modules. - all_variables: Option>>, + all_variables: Option>, /// Functions (both native Rust and scripted). functions: Option>, /// Flattened collection of all functions, native Rust and scripted. /// including those in sub-modules. - all_functions: Option>>, + all_functions: Option>, /// Bloom filter on native Rust functions (in scripted hash format) that contain [`Dynamic`] parameters. dynamic_functions_filter: Option>, /// Iterator functions, keyed by the type producing the iterator. - type_iterators: Option>>>, + type_iterators: Option>>, /// Flattened collection of iterator functions, including those in sub-modules. - all_type_iterators: Option>>>, + all_type_iterators: Option>>, /// Flags. pub(crate) flags: ModuleFlags, } @@ -234,7 +234,7 @@ impl fmt::Debug for Module { "modules", &self .modules - .as_deref() + .as_ref() .into_iter() .flat_map(BTreeMap::keys) .map(SmartString::as_str) @@ -561,23 +561,23 @@ impl Module { .functions .as_ref() .map_or(true, StraightHashMap::is_empty) - && self.variables.as_deref().map_or(true, BTreeMap::is_empty) - && self.modules.as_deref().map_or(true, BTreeMap::is_empty) + && self.variables.as_ref().map_or(true, BTreeMap::is_empty) + && self.modules.as_ref().map_or(true, BTreeMap::is_empty) && self .type_iterators - .as_deref() + .as_ref() .map_or(true, BTreeMap::is_empty) && self .all_functions - .as_deref() + .as_ref() .map_or(true, StraightHashMap::is_empty) && self .all_variables - .as_deref() + .as_ref() .map_or(true, StraightHashMap::is_empty) && self .all_type_iterators - .as_deref() + .as_ref() .map_or(true, BTreeMap::is_empty) } @@ -1979,9 +1979,9 @@ impl Module { #[must_use] pub fn count(&self) -> (usize, usize, usize) { ( - self.variables.as_deref().map_or(0, BTreeMap::len), + self.variables.as_ref().map_or(0, BTreeMap::len), self.functions.as_ref().map_or(0, StraightHashMap::len), - self.type_iterators.as_deref().map_or(0, BTreeMap::len), + self.type_iterators.as_ref().map_or(0, BTreeMap::len), ) } @@ -1989,7 +1989,7 @@ impl Module { #[inline] pub fn iter_sub_modules(&self) -> impl Iterator { self.modules - .as_deref() + .as_ref() .into_iter() .flatten() .map(|(k, m)| (k.as_str(), m)) @@ -1999,7 +1999,7 @@ impl Module { #[inline] pub fn iter_var(&self) -> impl Iterator { self.variables - .as_deref() + .as_ref() .into_iter() .flatten() .map(|(k, v)| (k.as_str(), v)) @@ -2392,7 +2392,7 @@ impl Module { if !self.is_indexed() { let mut path = Vec::with_capacity(4); - let mut variables = new_hash_map(self.variables.as_deref().map_or(0, BTreeMap::len)); + let mut variables = new_hash_map(self.variables.as_ref().map_or(0, BTreeMap::len)); let mut functions = new_hash_map(self.functions.as_ref().map_or(0, StraightHashMap::len)); let mut type_iterators = BTreeMap::new(); diff --git a/src/optimizer.rs b/src/optimizer.rs index dd8166c0..ea8f00d2 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -1262,7 +1262,7 @@ impl Engine { #[cfg(not(feature = "no_module"))] if self .global_sub_modules - .as_deref() + .as_ref() .into_iter() .flatten() .any(|(_, m)| m.contains_qualified_fn(hash)) diff --git a/src/packages/lang_core.rs b/src/packages/lang_core.rs index c7041387..26815f43 100644 --- a/src/packages/lang_core.rs +++ b/src/packages/lang_core.rs @@ -273,7 +273,7 @@ fn collect_fn_metadata( #[cfg(not(feature = "no_module"))] ctx.engine() .global_sub_modules - .as_deref() + .as_ref() .into_iter() .flatten() .flat_map(|(_, m)| m.iter_script_fn()) diff --git a/src/parser.rs b/src/parser.rs index 64f5731e..37c13265 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -601,7 +601,7 @@ impl Engine { .any(|m| m.as_str() == root) && !self .global_sub_modules - .as_deref() + .as_ref() .map_or(false, |m| m.contains_key(root)) { return Err( @@ -676,7 +676,7 @@ impl Engine { .any(|m| m.as_str() == root) && !self .global_sub_modules - .as_deref() + .as_ref() .map_or(false, |m| m.contains_key(root)) { return Err( @@ -1577,12 +1577,12 @@ impl Engine { Token::Custom(key) | Token::Reserved(key) | Token::Identifier(key) if self .custom_syntax - .as_deref() + .as_ref() .map_or(false, |m| m.contains_key(&**key)) => { let (key, syntax) = self .custom_syntax - .as_deref() + .as_ref() .and_then(|m| m.get_key_value(&**key)) .unwrap(); let (.., pos) = input.next().expect(NEVER_ENDS); @@ -1888,7 +1888,7 @@ impl Engine { .any(|m| m.as_str() == root) && !self .global_sub_modules - .as_deref() + .as_ref() .map_or(false, |m| m.contains_key(root)) { return Err( @@ -2303,7 +2303,7 @@ impl Engine { #[cfg(not(feature = "no_custom_syntax"))] Token::Custom(c) => self .custom_keywords - .as_deref() + .as_ref() .and_then(|m| m.get(&**c)) .copied() .ok_or_else(|| PERR::Reserved(c.to_string()).into_err(*current_pos))?, @@ -2329,7 +2329,7 @@ impl Engine { #[cfg(not(feature = "no_custom_syntax"))] Token::Custom(c) => self .custom_keywords - .as_deref() + .as_ref() .and_then(|m| m.get(&**c)) .copied() .ok_or_else(|| PERR::Reserved(c.to_string()).into_err(*next_pos))?, @@ -2434,7 +2434,7 @@ impl Engine { Token::Custom(s) if self .custom_keywords - .as_deref() + .as_ref() .and_then(|m| m.get(s.as_str())) .map_or(false, Option::is_some) => { diff --git a/src/serde/metadata.rs b/src/serde/metadata.rs index 719298eb..4eec9053 100644 --- a/src/serde/metadata.rs +++ b/src/serde/metadata.rs @@ -182,7 +182,7 @@ pub fn gen_metadata_to_json( let mut global = ModuleMetadata::new(); #[cfg(not(feature = "no_module"))] - for (name, m) in engine.global_sub_modules.as_deref().into_iter().flatten() { + for (name, m) in engine.global_sub_modules.as_ref().into_iter().flatten() { global.modules.insert(name, m.as_ref().into()); } diff --git a/src/tokenizer.rs b/src/tokenizer.rs index d82075fe..60d13808 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -2450,7 +2450,7 @@ impl<'a> Iterator for TokenIterator<'a> { Some((Token::Reserved(s), pos)) => (match (s.as_str(), #[cfg(not(feature = "no_custom_syntax"))] - self.engine.custom_keywords.as_deref().map_or(false, |m| m.contains_key(&*s)), + self.engine.custom_keywords.as_ref().map_or(false, |m| m.contains_key(&*s)), #[cfg(feature = "no_custom_syntax")] false ) @@ -2487,7 +2487,7 @@ impl<'a> Iterator for TokenIterator<'a> { #[cfg(feature = "no_custom_syntax")] (.., true) => unreachable!("no custom operators"), // Reserved keyword that is not custom and disabled. - (token, false) if self.engine.disabled_symbols.as_deref().map_or(false,|m| m.contains(token)) => { + (token, false) if self.engine.disabled_symbols.as_ref().map_or(false,|m| m.contains(token)) => { let msg = format!("reserved {} '{token}' is disabled", if is_valid_identifier(token) { "keyword"} else {"symbol"}); Token::LexError(LERR::ImproperSymbol(s.to_string(), msg).into()) }, @@ -2496,13 +2496,13 @@ impl<'a> Iterator for TokenIterator<'a> { }, pos), // Custom keyword #[cfg(not(feature = "no_custom_syntax"))] - Some((Token::Identifier(s), pos)) if self.engine.custom_keywords.as_deref().map_or(false,|m| m.contains_key(&*s)) => { + Some((Token::Identifier(s), pos)) if self.engine.custom_keywords.as_ref().map_or(false,|m| m.contains_key(&*s)) => { (Token::Custom(s), pos) } // Custom keyword/symbol - must be disabled #[cfg(not(feature = "no_custom_syntax"))] - Some((token, pos)) if token.is_literal() && self.engine.custom_keywords.as_deref().map_or(false,|m| m.contains_key(token.literal_syntax())) => { - if self.engine.disabled_symbols.as_deref().map_or(false,|m| m.contains(token.literal_syntax())) { + Some((token, pos)) if token.is_literal() && self.engine.custom_keywords.as_ref().map_or(false,|m| m.contains_key(token.literal_syntax())) => { + if self.engine.disabled_symbols.as_ref().map_or(false,|m| m.contains(token.literal_syntax())) { // Disabled standard keyword/symbol (Token::Custom(Box::new(token.literal_syntax().into())), pos) } else { @@ -2511,7 +2511,7 @@ impl<'a> Iterator for TokenIterator<'a> { } } // Disabled symbol - Some((token, pos)) if token.is_literal() && self.engine.disabled_symbols.as_deref().map_or(false,|m| m.contains(token.literal_syntax())) => { + Some((token, pos)) if token.is_literal() && self.engine.disabled_symbols.as_ref().map_or(false,|m| m.contains(token.literal_syntax())) => { (Token::Reserved(Box::new(token.literal_syntax().into())), pos) } // Normal symbol