Change try_for_each to for.

This commit is contained in:
Stephen Chung 2022-12-29 13:35:42 +08:00
parent 858a6ad588
commit 1a00ca0905
4 changed files with 79 additions and 97 deletions

View File

@ -286,7 +286,7 @@ impl Engine {
Expr::InterpolatedString(x, _) => {
let mut concat = SmartString::new_const();
x.iter().try_for_each(|expr| -> RhaiResultOf<()> {
for expr in &**x {
let item = &mut self
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), expr)?
.flatten();
@ -304,9 +304,7 @@ impl Engine {
#[cfg(not(feature = "unchecked"))]
self.throw_on_size((0, 0, concat.len()))
.map_err(|err| err.fill_position(pos))?;
Ok(())
})?;
}
Ok(self.get_interned_string(concat).into())
}
@ -318,7 +316,7 @@ impl Engine {
#[cfg(not(feature = "unchecked"))]
let mut total_data_sizes = (0, 0, 0);
x.iter().try_for_each(|item_expr| -> RhaiResultOf<()> {
for item_expr in &**x {
let value = self
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), item_expr)?
.flatten();
@ -337,9 +335,7 @@ impl Engine {
}
array.push(value);
Ok(())
})?;
}
Ok(Dynamic::from_array(array))
}
@ -351,28 +347,25 @@ impl Engine {
#[cfg(not(feature = "unchecked"))]
let mut total_data_sizes = (0, 0, 0);
x.0.iter()
.try_for_each(|(key, value_expr)| -> RhaiResultOf<()> {
let value = self
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), value_expr)?
.flatten();
for (key, value_expr) in &x.0 {
let value = self
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), value_expr)?
.flatten();
#[cfg(not(feature = "unchecked"))]
if self.has_data_size_limit() {
let delta = value.calc_data_sizes(true);
total_data_sizes = (
total_data_sizes.0 + delta.0,
total_data_sizes.1 + delta.1 + 1,
total_data_sizes.2 + delta.2,
);
self.throw_on_size(total_data_sizes)
.map_err(|err| err.fill_position(value_expr.position()))?;
}
#[cfg(not(feature = "unchecked"))]
if self.has_data_size_limit() {
let delta = value.calc_data_sizes(true);
total_data_sizes = (
total_data_sizes.0 + delta.0,
total_data_sizes.1 + delta.1 + 1,
total_data_sizes.2 + delta.2,
);
self.throw_on_size(total_data_sizes)
.map_err(|err| err.fill_position(value_expr.position()))?;
}
*map.get_mut(key.as_str()).unwrap() = value;
Ok(())
})?;
*map.get_mut(key.as_str()).unwrap() = value;
}
Ok(Dynamic::from_map(map))
}

View File

@ -861,25 +861,23 @@ impl Engine {
// Share statement
#[cfg(not(feature = "no_closure"))]
Stmt::Share(x) => {
x.iter()
.try_for_each(|(name, index, pos)| {
index
.map(|n| scope.len() - n.get())
.or_else(|| scope.search(name))
.map_or_else(
|| Err(ERR::ErrorVariableNotFound(name.to_string(), *pos).into()),
|index| {
let val = scope.get_mut_by_index(index);
for (name, index, pos) in &**x {
if let Some(index) = index
.map(|n| scope.len() - n.get())
.or_else(|| scope.search(name))
{
let val = scope.get_mut_by_index(index);
if !val.is_shared() {
// Replace the variable with a shared value.
*val = std::mem::take(val).into_shared();
}
Ok(())
},
)
})
.map(|_| Dynamic::UNIT)
if !val.is_shared() {
// Replace the variable with a shared value.
*val = std::mem::take(val).into_shared();
}
} else {
return Err(ERR::ErrorVariableNotFound(name.to_string(), *pos).into());
}
}
Ok(Dynamic::UNIT)
}
_ => unreachable!("statement cannot be evaluated: {:?}", stmt),

View File

@ -1118,12 +1118,11 @@ impl Engine {
let mut fn_ptr = arg_value.cast::<FnPtr>();
// Append the new curried arguments to the existing list.
a_expr.iter().try_for_each(|expr| -> Result<_, RhaiError> {
for expr in a_expr {
let (value, ..) =
self.get_arg_value(global, caches, scope, this_ptr.as_deref_mut(), expr)?;
fn_ptr.add_curry(value);
Ok(())
})?;
}
return Ok(fn_ptr.into());
}
@ -1229,14 +1228,11 @@ impl Engine {
// If so, do it separately because we cannot convert the first argument (if it is a simple
// variable access) to &mut because `scope` is needed.
if capture_scope && !scope.is_empty() {
first_arg
.iter()
.copied()
.chain(a_expr.iter())
.try_for_each(|expr| {
self.get_arg_value(global, caches, scope, this_ptr.as_deref_mut(), expr)
.map(|(value, ..)| arg_values.push(value.flatten()))
})?;
for expr in first_arg.iter().copied().chain(a_expr.iter()) {
let (value, ..) =
self.get_arg_value(global, caches, scope, this_ptr.as_deref_mut(), expr)?;
arg_values.push(value.flatten());
}
args.extend(curry.iter_mut());
args.extend(arg_values.iter_mut());
@ -1265,10 +1261,11 @@ impl Engine {
self.run_debugger(global, caches, scope, this_ptr.as_deref_mut(), first_expr)?;
// func(x, ...) -> x.func(...)
a_expr.iter().try_for_each(|expr| {
self.get_arg_value(global, caches, scope, this_ptr.as_deref_mut(), expr)
.map(|(value, ..)| arg_values.push(value.flatten()))
})?;
for expr in a_expr {
let (value, ..) =
self.get_arg_value(global, caches, scope, this_ptr.as_deref_mut(), expr)?;
arg_values.push(value.flatten());
}
let mut target =
self.search_namespace(global, caches, scope, this_ptr, first_expr)?;
@ -1289,13 +1286,11 @@ impl Engine {
}
} else {
// func(..., ...)
first_arg
.into_iter()
.chain(a_expr.iter())
.try_for_each(|expr| {
self.get_arg_value(global, caches, scope, this_ptr.as_deref_mut(), expr)
.map(|(value, ..)| arg_values.push(value.flatten()))
})?;
for expr in first_arg.into_iter().chain(a_expr.iter()) {
let (value, ..) =
self.get_arg_value(global, caches, scope, this_ptr.as_deref_mut(), expr)?;
arg_values.push(value.flatten());
}
args.extend(curry.iter_mut());
}
@ -1345,10 +1340,11 @@ impl Engine {
// func(x, ...) -> x.func(...)
arg_values.push(Dynamic::UNIT);
args_expr.iter().skip(1).try_for_each(|expr| {
self.get_arg_value(global, caches, scope, this_ptr.as_deref_mut(), expr)
.map(|(value, ..)| arg_values.push(value.flatten()))
})?;
for expr in args_expr.iter().skip(1) {
let (value, ..) =
self.get_arg_value(global, caches, scope, this_ptr.as_deref_mut(), expr)?;
arg_values.push(value.flatten());
}
// Get target reference to first argument
let first_arg = &args_expr[0];
@ -1374,10 +1370,11 @@ impl Engine {
}
} else {
// func(..., ...) or func(mod::x, ...)
args_expr.iter().try_for_each(|expr| {
self.get_arg_value(global, caches, scope, this_ptr.as_deref_mut(), expr)
.map(|(value, ..)| arg_values.push(value.flatten()))
})?;
for expr in args_expr {
let (value, ..) =
self.get_arg_value(global, caches, scope, this_ptr.as_deref_mut(), expr)?;
arg_values.push(value.flatten());
}
args.extend(arg_values.iter_mut());
}
}

View File

@ -1468,28 +1468,22 @@ impl Engine {
let (expr, fn_def) = result?;
#[cfg(not(feature = "no_closure"))]
new_state
.external_vars
.as_deref()
.into_iter()
.flatten()
.try_for_each(|Ident { name, pos }| {
let (index, is_func) = state.access_var(name, lib, *pos);
for Ident { name, pos } in new_state.external_vars.as_deref().into_iter().flatten()
{
let (index, is_func) = state.access_var(name, lib, *pos);
if !is_func
&& index.is_none()
&& !settings.has_flag(ParseSettingFlags::CLOSURE_SCOPE)
&& settings.has_option(LangOptions::STRICT_VAR)
&& !state.scope.contains(name)
{
// If the parent scope is not inside another capturing closure
// then we can conclude that the captured variable doesn't exist.
// Under Strict Variables mode, this is not allowed.
Err(PERR::VariableUndefined(name.to_string()).into_err(*pos))
} else {
Ok(())
}
})?;
if !is_func
&& index.is_none()
&& !settings.has_flag(ParseSettingFlags::CLOSURE_SCOPE)
&& settings.has_option(LangOptions::STRICT_VAR)
&& !state.scope.contains(name)
{
// If the parent scope is not inside another capturing closure
// then we can conclude that the captured variable doesn't exist.
// Under Strict Variables mode, this is not allowed.
return Err(PERR::VariableUndefined(name.to_string()).into_err(*pos));
}
}
let hash_script = calc_fn_hash(None, &fn_def.name, fn_def.params.len());
lib.insert(hash_script, fn_def);