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
.engine
.global_sub_modules
.iter()
.flat_map(|m| m.iter())
.as_deref()
.into_iter()
.flatten()
.map(move |(name, module)| {
(
name.to_string(),

View File

@ -743,7 +743,7 @@ impl Engine {
signatures.extend(self.global_namespace().gen_fn_signatures());
#[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}")));
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -236,7 +236,9 @@ impl Engine {
// Then check imported modules
global.contains_qualified_fn(hash_script)
// 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) {
// Do not cache "one-hit wonders"

View File

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

View File

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

View File

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

View File

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

View File

@ -171,7 +171,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.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());
}

36444
t.txt Normal file

File diff suppressed because it is too large Load Diff