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

View File

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

View File

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

View File

@ -1468,12 +1468,8 @@ impl Engine {
let (expr, fn_def) = result?; let (expr, fn_def) = result?;
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
new_state for Ident { name, pos } in new_state.external_vars.as_deref().into_iter().flatten()
.external_vars {
.as_deref()
.into_iter()
.flatten()
.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);
if !is_func if !is_func
@ -1485,11 +1481,9 @@ impl Engine {
// If the parent scope is not inside another capturing closure // If the parent scope is not inside another capturing closure
// then we can conclude that the captured variable doesn't exist. // then we can conclude that the captured variable doesn't exist.
// Under Strict Variables mode, this is not allowed. // Under Strict Variables mode, this is not allowed.
Err(PERR::VariableUndefined(name.to_string()).into_err(*pos)) return Err(PERR::VariableUndefined(name.to_string()).into_err(*pos));
} else { }
Ok(())
} }
})?;
let hash_script = calc_fn_hash(None, &fn_def.name, fn_def.params.len()); let hash_script = calc_fn_hash(None, &fn_def.name, fn_def.params.len());
lib.insert(hash_script, fn_def); lib.insert(hash_script, fn_def);