Use as_deref().

This commit is contained in:
Stephen Chung 2022-11-25 23:03:20 +08:00
parent d645d8271c
commit e8e1706d98
14 changed files with 36546 additions and 74 deletions

View File

@ -368,8 +368,9 @@ impl Definitions<'_> {
let mut m = self let mut m = self
.engine .engine
.global_sub_modules .global_sub_modules
.iter() .as_deref()
.flat_map(|m| m.iter()) .into_iter()
.flatten()
.map(move |(name, module)| { .map(move |(name, module)| {
( (
name.to_string(), name.to_string(),

View File

@ -743,7 +743,7 @@ impl Engine {
signatures.extend(self.global_namespace().gen_fn_signatures()); signatures.extend(self.global_namespace().gen_fn_signatures());
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
for (name, m) in self.global_sub_modules.iter().flat_map(|m| m.iter()) { for (name, m) in self.global_sub_modules.as_deref().into_iter().flatten() {
signatures.extend(m.gen_fn_signatures().map(|f| format!("{name}::{f}"))); signatures.extend(m.gen_fn_signatures().map(|f| format!("{name}::{f}")));
} }

View File

@ -204,8 +204,9 @@ impl Engine {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
return self return self
.global_sub_modules .global_sub_modules
.iter() .as_deref()
.flat_map(|m| m.iter()) .into_iter()
.flatten()
.find_map(|(_, m)| m.get_custom_type(name)); .find_map(|(_, m)| m.get_custom_type(name));
#[cfg(feature = "no_module")] #[cfg(feature = "no_module")]
return None; return None;
@ -238,8 +239,9 @@ impl Engine {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
return self return self
.global_sub_modules .global_sub_modules
.iter() .as_deref()
.flat_map(|m| m.iter()) .into_iter()
.flatten()
.find_map(|(_, m)| m.get_custom_type(name)); .find_map(|(_, m)| m.get_custom_type(name));
#[cfg(feature = "no_module")] #[cfg(feature = "no_module")]
return None; return None;

View File

@ -191,6 +191,9 @@ impl fmt::Debug for Engine {
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
f.field("limits", &self.limits); f.field("limits", &self.limits);
#[cfg(not(feature = "debugging"))]
f.field("debugger_interface", &self.debugger_interface.is_some());
f.finish() f.finish()
} }
} }

View File

@ -195,10 +195,11 @@ impl GlobalRuntimeState {
#[inline] #[inline]
pub fn iter_imports(&self) -> impl Iterator<Item = (&str, &crate::Module)> { pub fn iter_imports(&self) -> impl Iterator<Item = (&str, &crate::Module)> {
self.imports self.imports
.iter() .as_deref()
.flat_map(|x| x.iter()) .into_iter()
.flatten()
.rev() .rev()
.zip(self.modules.iter().flat_map(|x| x.iter()).rev()) .zip(self.modules.as_deref().into_iter().flatten().rev())
.map(|(name, module)| (name.as_str(), &**module)) .map(|(name, module)| (name.as_str(), &**module))
} }
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in reverse order. /// Get an iterator to the stack of globally-imported [modules][crate::Module] in reverse order.
@ -210,10 +211,11 @@ impl GlobalRuntimeState {
&self, &self,
) -> impl Iterator<Item = (&ImmutableString, &crate::SharedModule)> { ) -> impl Iterator<Item = (&ImmutableString, &crate::SharedModule)> {
self.imports self.imports
.iter() .as_deref()
.flat_map(|x| x.iter()) .into_iter()
.flatten()
.rev() .rev()
.zip(self.modules.iter().flat_map(|x| x.iter()).rev()) .zip(self.modules.as_deref().into_iter().flatten())
} }
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in forward order. /// Get an iterator to the stack of globally-imported [modules][crate::Module] in forward order.
/// ///
@ -224,9 +226,10 @@ impl GlobalRuntimeState {
&self, &self,
) -> impl Iterator<Item = (&ImmutableString, &crate::SharedModule)> { ) -> impl Iterator<Item = (&ImmutableString, &crate::SharedModule)> {
self.imports self.imports
.iter() .as_deref()
.flat_map(|x| x.iter()) .into_iter()
.zip(self.modules.iter().flat_map(|x| x.iter())) .flatten()
.zip(self.modules.as_deref().into_iter().flatten())
} }
/// Can the particular function with [`Dynamic`] parameter(s) exist in the stack of /// Can the particular function with [`Dynamic`] parameter(s) exist in the stack of
/// globally-imported [modules][crate::Module]? /// globally-imported [modules][crate::Module]?

View File

@ -503,9 +503,10 @@ impl Engine {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
let func = func.or_else(|| global.get_iter(iter_type)).or_else(|| { let func = func.or_else(|| global.get_iter(iter_type)).or_else(|| {
self.global_sub_modules self.global_sub_modules
.iter() .as_deref()
.flat_map(|m| m.values()) .into_iter()
.find_map(|m| m.get_qualified_iter(iter_type)) .flatten()
.find_map(|(_, m)| m.get_qualified_iter(iter_type))
}); });
let func = func.ok_or_else(|| ERR::ErrorFor(expr.start_position()))?; let func = func.ok_or_else(|| ERR::ErrorFor(expr.start_position()))?;

View File

@ -212,9 +212,12 @@ impl Engine {
} else { } else {
func.or_else(|| _global.get_qualified_fn(hash)).or_else(|| { func.or_else(|| _global.get_qualified_fn(hash)).or_else(|| {
self.global_sub_modules self.global_sub_modules
.iter() .as_deref()
.flat_map(|m| m.values()) .into_iter()
.find_map(|m| m.get_qualified_fn(hash).map(|f| (f, m.id_raw()))) .flatten()
.find_map(|(_, m)| {
m.get_qualified_fn(hash).map(|f| (f, m.id_raw()))
})
}) })
}; };
@ -251,11 +254,9 @@ impl Engine {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
let is_dynamic = is_dynamic let is_dynamic = is_dynamic
|| _global.may_contain_dynamic_fn(hash_base) || _global.may_contain_dynamic_fn(hash_base)
|| self || self.global_sub_modules.as_deref().map_or(false, |m| {
.global_sub_modules m.values().any(|m| m.may_contain_dynamic_fn(hash_base))
.iter() });
.flat_map(|m| m.values())
.any(|m| m.may_contain_dynamic_fn(hash_base));
// Set maximum bitmask when there are dynamic versions of the function // Set maximum bitmask when there are dynamic versions of the function
if is_dynamic { if is_dynamic {
@ -270,24 +271,25 @@ impl Engine {
} }
// Try to find a built-in version // Try to find a built-in version
let builtin = let builtin = args.and_then(|args| match op_token {
args.and_then(|args| match op_token { Token::NonToken => None,
Token::NonToken => None, token if token.is_op_assignment() => {
token if token.is_op_assignment() => { let (first_arg, rest_args) = args.split_first().unwrap();
let (first_arg, rest_args) = args.split_first().unwrap();
get_builtin_op_assignment_fn(token, first_arg, rest_args[0]) get_builtin_op_assignment_fn(token, first_arg, rest_args[0]).map(
.map(|f| FnResolutionCacheEntry { |f| FnResolutionCacheEntry {
func: CallableFunction::Method(Shared::new(f)),
source: None,
})
}
token => get_builtin_binary_op_fn(token.clone(), args[0], args[1])
.map(|f| FnResolutionCacheEntry {
func: CallableFunction::Method(Shared::new(f)), func: CallableFunction::Method(Shared::new(f)),
source: None, source: None,
}), },
}); )
}
token => get_builtin_binary_op_fn(token, args[0], args[1]).map(|f| {
FnResolutionCacheEntry {
func: CallableFunction::Method(Shared::new(f)),
source: None,
}
}),
});
return if cache.filter.is_absent_and_set(hash) { return if cache.filter.is_absent_and_set(hash) {
// Do not cache "one-hit wonders" // Do not cache "one-hit wonders"

View File

@ -236,7 +236,9 @@ impl Engine {
// Then check imported modules // Then check imported modules
global.contains_qualified_fn(hash_script) global.contains_qualified_fn(hash_script)
// Then check sub-modules // Then check sub-modules
|| self.global_sub_modules.iter().flat_map(|m| m.values()).any(|m| m.contains_qualified_fn(hash_script)); || self.global_sub_modules.as_deref().map_or(false, |m| {
m.values().any(|m| m.contains_qualified_fn(hash_script))
});
if !result && !cache.filter.is_absent_and_set(hash_script) { if !result && !cache.filter.is_absent_and_set(hash_script) {
// Do not cache "one-hit wonders" // Do not cache "one-hit wonders"

View File

@ -231,7 +231,8 @@ impl fmt::Debug for Module {
"modules", "modules",
&self &self
.modules .modules
.iter() .as_deref()
.into_iter()
.flat_map(|m| m.keys()) .flat_map(|m| m.keys())
.map(SmartString::as_str) .map(SmartString::as_str)
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
@ -1025,8 +1026,8 @@ impl Module {
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
let (param_names, return_type_name) = { let (param_names, return_type_name) = {
let mut names = _arg_names let mut names = _arg_names
.iter() .into_iter()
.flat_map(|&p| p.iter()) .flatten()
.map(|&s| s.into()) .map(|&s| s.into())
.collect::<StaticVec<_>>(); .collect::<StaticVec<_>>();
let return_type = if names.len() > arg_types.as_ref().len() { let return_type = if names.len() > arg_types.as_ref().len() {
@ -1908,16 +1909,20 @@ impl Module {
#[inline] #[inline]
pub fn iter_sub_modules(&self) -> impl Iterator<Item = (&str, &SharedModule)> { pub fn iter_sub_modules(&self) -> impl Iterator<Item = (&str, &SharedModule)> {
self.modules self.modules
.iter() .as_deref()
.flat_map(|m| m.iter().map(|(k, m)| (k.as_str(), m))) .into_iter()
.flatten()
.map(|(k, m)| (k.as_str(), m))
} }
/// Get an iterator to the variables in the [`Module`]. /// Get an iterator to the variables in the [`Module`].
#[inline] #[inline]
pub fn iter_var(&self) -> impl Iterator<Item = (&str, &Dynamic)> { pub fn iter_var(&self) -> impl Iterator<Item = (&str, &Dynamic)> {
self.variables self.variables
.iter() .as_deref()
.flat_map(|m| m.iter().map(|(k, v)| (k.as_str(), v))) .into_iter()
.flatten()
.map(|(k, v)| (k.as_str(), v))
} }
/// Get an iterator to the functions in the [`Module`]. /// Get an iterator to the functions in the [`Module`].
@ -2235,7 +2240,7 @@ impl Module {
} }
// Index all Rust functions // Index all Rust functions
for (&hash, f) in module.functions.iter().flat_map(|m| m.iter()) { for (&hash, f) in module.functions.iter().flatten() {
match f.metadata.namespace { match f.metadata.namespace {
FnNamespace::Global => { FnNamespace::Global => {
// Flatten all functions with global namespace // Flatten all functions with global namespace

View File

@ -1247,9 +1247,10 @@ impl Engine {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
if self if self
.global_sub_modules .global_sub_modules
.iter() .as_deref()
.flat_map(|m| m.values()) .into_iter()
.any(|m| m.contains_qualified_fn(hash)) .flatten()
.any(|(_, m)| m.contains_qualified_fn(hash))
{ {
return true; return true;
} }

View File

@ -267,9 +267,10 @@ fn collect_fn_metadata(
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
ctx.engine() ctx.engine()
.global_sub_modules .global_sub_modules
.iter() .as_deref()
.flat_map(|m| m.values()) .into_iter()
.flat_map(|m| m.iter_script_fn()) .flatten()
.flat_map(|(_, m)| m.iter_script_fn())
.filter(|(ns, a, n, p, f)| filter(*ns, *a, n, *p, f)) .filter(|(ns, a, n, p, f)| filter(*ns, *a, n, *p, f))
.for_each(|(.., f)| { .for_each(|(.., f)| {
list.push( list.push(

View File

@ -142,7 +142,8 @@ impl<'e, 's> ParseState<'e, 's> {
( (
self.stack self.stack
.iter() .as_deref()
.into_iter()
.flat_map(|s| s.iter_rev_raw()) .flat_map(|s| s.iter_rev_raw())
.enumerate() .enumerate()
.find(|&(.., (n, ..))| { .find(|&(.., (n, ..))| {
@ -196,9 +197,10 @@ impl<'e, 's> ParseState<'e, 's> {
&& index == 0 && index == 0
&& !self && !self
.external_vars .external_vars
.iter() .as_deref()
.flat_map(|v| v.iter()) .into_iter()
.any(|v| v.as_str() == name) .flatten()
.any(|v| v.name == name)
{ {
self.external_vars self.external_vars
.get_or_insert_with(Default::default) .get_or_insert_with(Default::default)
@ -234,8 +236,9 @@ impl<'e, 's> ParseState<'e, 's> {
#[must_use] #[must_use]
pub fn find_module(&self, name: &str) -> Option<NonZeroUsize> { pub fn find_module(&self, name: &str) -> Option<NonZeroUsize> {
self.imports self.imports
.iter() .as_deref()
.flat_map(|m| m.iter()) .into_iter()
.flatten()
.rev() .rev()
.enumerate() .enumerate()
.find(|(.., n)| n.as_str() == name) .find(|(.., n)| n.as_str() == name)
@ -602,8 +605,9 @@ impl Engine {
&& !is_global && !is_global
&& !state && !state
.global_imports .global_imports
.iter() .as_deref()
.flat_map(|m| m.iter()) .into_iter()
.flatten()
.any(|m| m.as_str() == root) .any(|m| m.as_str() == root)
&& !self && !self
.global_sub_modules .global_sub_modules
@ -677,8 +681,9 @@ impl Engine {
&& !is_global && !is_global
&& !state && !state
.global_imports .global_imports
.iter() .as_deref()
.flat_map(|m| m.iter()) .into_iter()
.flatten()
.any(|m| m.as_str() == root) .any(|m| m.as_str() == root)
&& !self && !self
.global_sub_modules .global_sub_modules
@ -1433,7 +1438,7 @@ impl Engine {
new_state new_state
.global_imports .global_imports
.get_or_insert_with(Default::default) .get_or_insert_with(Default::default)
.extend(state.imports.iter().flat_map(|m| m.iter()).cloned()); .extend(state.imports.as_deref().into_iter().flatten().cloned());
} }
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
@ -1468,8 +1473,9 @@ impl Engine {
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
new_state new_state
.external_vars .external_vars
.iter() .as_deref()
.flat_map(|v| v.iter()) .into_iter()
.flatten()
.try_for_each(|Ident { name, pos }| { .try_for_each(|Ident { name, pos }| {
let (index, is_func) = state.access_var(name, lib, *pos); let (index, is_func) = state.access_var(name, lib, *pos);
@ -1883,8 +1889,9 @@ impl Engine {
&& !is_global && !is_global
&& !state && !state
.global_imports .global_imports
.iter() .as_deref()
.flat_map(|m| m.iter()) .into_iter()
.flatten()
.any(|m| m.as_str() == root) .any(|m| m.as_str() == root)
&& !self && !self
.global_sub_modules .global_sub_modules
@ -3312,7 +3319,7 @@ impl Engine {
new_state new_state
.global_imports .global_imports
.get_or_insert_with(Default::default) .get_or_insert_with(Default::default)
.extend(state.imports.iter().flat_map(|m| m.iter()).cloned()); .extend(state.imports.as_deref().into_iter().flatten().cloned());
} }
let options = self.options | (settings.options & LangOptions::STRICT_VAR); let options = self.options | (settings.options & LangOptions::STRICT_VAR);

View File

@ -171,7 +171,7 @@ pub fn gen_metadata_to_json(
let mut global = ModuleMetadata::new(); let mut global = ModuleMetadata::new();
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
for (name, m) in engine.global_sub_modules.iter().flat_map(|m| m.iter()) { for (name, m) in engine.global_sub_modules.as_deref().into_iter().flatten() {
global.modules.insert(name, m.as_ref().into()); global.modules.insert(name, m.as_ref().into());
} }

36444
t.txt Normal file

File diff suppressed because it is too large Load Diff