From b466d581926500216e61f3cae6d3f5c4cdf02368 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 1 Mar 2021 15:58:11 +0800 Subject: [PATCH] Remove unnecessary AsRef and impl IntoIterator. --- CHANGELOG.md | 1 + src/engine.rs | 19 +++++++++++++------ src/engine_api.rs | 22 +++++++--------------- src/fn_call.rs | 10 +++++----- src/fn_native.rs | 8 ++++---- src/module/mod.rs | 8 ++------ src/packages/iter_basic.rs | 4 ++-- src/syntax.rs | 2 +- 8 files changed, 35 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d16a72aa..8d9d2e94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ Breaking changes * `EvalAltResult::clear_position` is renamed `EvalAltResult::take_position` and returns the position taken. * `private` functions in an `AST` can now be called with `call_fn` etc. * `NativeCallContext::call_fn_dynamic_raw` no longer has the `pub_only` parameter. +* `Module::update_fn_metadata` input parameter is changed. Enhancements ------------ diff --git a/src/engine.rs b/src/engine.rs index 3dc81871..1a8191fb 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1726,9 +1726,16 @@ impl Engine { .map(|(val, _)| val.take_or_clone()), // Statement block - Expr::Stmt(x, _) => { - self.eval_stmt_block(scope, mods, state, lib, this_ptr, x.as_ref(), true, level) - } + Expr::Stmt(x, _) => self.eval_stmt_block( + scope, + mods, + state, + lib, + this_ptr, + x.as_ref().as_ref(), + true, + level, + ), // lhs[idx_expr] #[cfg(not(feature = "no_index"))] @@ -1857,14 +1864,14 @@ impl Engine { } /// Evaluate a statements block. - pub(crate) fn eval_stmt_block<'a>( + pub(crate) fn eval_stmt_block( &self, scope: &mut Scope, mods: &mut Imports, state: &mut State, lib: &[&Module], this_ptr: &mut Option<&mut Dynamic>, - statements: impl IntoIterator, + statements: &[Stmt], restore: bool, level: usize, ) -> Result> { @@ -1877,7 +1884,7 @@ impl Engine { state.scope_level += 1; } - let result = statements.into_iter().try_fold(Dynamic::UNIT, |_, stmt| { + let result = statements.iter().try_fold(Dynamic::UNIT, |_, stmt| { #[cfg(not(feature = "no_module"))] match stmt { Stmt::Import(_, _, _) => { diff --git a/src/engine_api.rs b/src/engine_api.rs index 9fcd266d..058e8ee0 100644 --- a/src/engine_api.rs +++ b/src/engine_api.rs @@ -764,29 +764,25 @@ impl Engine { /// # } /// ``` #[cfg(not(feature = "no_module"))] - pub fn register_static_module( - &mut self, - name: impl AsRef, - module: Shared, - ) -> &mut Self { + pub fn register_static_module(&mut self, name: &str, module: Shared) -> &mut Self { fn register_static_module_raw( root: &mut crate::stdlib::collections::HashMap>, - name: impl AsRef, + name: &str, module: Shared, ) { let separator = crate::token::Token::DoubleColon.syntax(); - if !name.as_ref().contains(separator.as_ref()) { + if !name.contains(separator.as_ref()) { if !module.is_indexed() { // Index the module (making a clone copy if necessary) if it is not indexed let mut module = crate::fn_native::shared_take_or_clone(module); module.build_index(); - root.insert(name.as_ref().trim().into(), module.into()); + root.insert(name.trim().into(), module.into()); } else { - root.insert(name.as_ref().trim().into(), module); + root.insert(name.trim().into(), module); } } else { - let mut iter = name.as_ref().splitn(2, separator.as_ref()); + let mut iter = name.splitn(2, separator.as_ref()); let sub_module = iter.next().unwrap().trim(); let remainder = iter.next().unwrap().trim(); @@ -817,11 +813,7 @@ impl Engine { #[cfg(not(feature = "no_module"))] #[inline(always)] #[deprecated = "use `register_static_module` instead"] - pub fn register_module( - &mut self, - name: impl AsRef, - module: impl Into>, - ) -> &mut Self { + pub fn register_module(&mut self, name: &str, module: impl Into>) -> &mut Self { self.register_static_module(name, module.into()) } /// Compile a string into an [`AST`], which can be used later for evaluation. diff --git a/src/fn_call.rs b/src/fn_call.rs index 9bae00a6..7eafbcfb 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -545,7 +545,7 @@ impl Engine { state: Option<&mut State>, lib: &[&Module], fn_name: &str, - arg_types: impl AsRef<[TypeId]>, + arg_types: &[TypeId], ) -> bool { let arg_types = arg_types.as_ref(); let hash_fn = calc_native_fn_hash(empty(), fn_name, arg_types.iter().cloned()); @@ -812,12 +812,12 @@ impl Engine { /// Evaluate a list of statements with no `this` pointer. /// This is commonly used to evaluate a list of statements in an [`AST`] or a script function body. #[inline] - pub(crate) fn eval_global_statements<'a>( + pub(crate) fn eval_global_statements( &self, scope: &mut Scope, mods: &mut Imports, state: &mut State, - statements: impl IntoIterator, + statements: &[Stmt], lib: &[&Module], level: usize, ) -> Result> { @@ -1022,7 +1022,7 @@ impl Engine { lib: &[&Module], this_ptr: &mut Option<&mut Dynamic>, fn_name: &str, - args_expr: impl AsRef<[Expr]>, + args_expr: &[Expr], mut hash_script: Option, pos: Position, capture_scope: bool, @@ -1260,7 +1260,7 @@ impl Engine { this_ptr: &mut Option<&mut Dynamic>, namespace: Option<&NamespaceRef>, fn_name: &str, - args_expr: impl AsRef<[Expr]>, + args_expr: &[Expr], hash_script: NonZeroU64, pos: Position, level: usize, diff --git a/src/fn_native.rs b/src/fn_native.rs index 23ac2b50..65805e36 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -97,13 +97,13 @@ impl<'e, 'n, 'm, M: AsRef<[&'m Module]> + ?Sized> From<(&'e Engine, &'n str, &'m impl<'e, 'n, 's, 'a, 'm> NativeCallContext<'e, 'n, 's, 'a, 'm> { /// Create a new [`NativeCallContext`]. #[inline(always)] - pub fn new(engine: &'e Engine, fn_name: &'n str, lib: &'m impl AsRef<[&'m Module]>) -> Self { + pub fn new(engine: &'e Engine, fn_name: &'n str, lib: &'m [&'m Module]) -> Self { Self { engine, fn_name, source: None, mods: None, - lib: lib.as_ref(), + lib, } } /// _(INTERNALS)_ Create a new [`NativeCallContext`]. @@ -116,14 +116,14 @@ impl<'e, 'n, 's, 'a, 'm> NativeCallContext<'e, 'n, 's, 'a, 'm> { fn_name: &'n str, source: &'s Option<&str>, imports: &'a mut Imports, - lib: &'m impl AsRef<[&'m Module]>, + lib: &'m [&'m Module], ) -> Self { Self { engine, fn_name, source: source.clone(), mods: Some(imports), - lib: lib.as_ref(), + lib, } } /// The current [`Engine`]. diff --git a/src/module/mod.rs b/src/module/mod.rs index 5d61bb68..ec086242 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -640,13 +640,9 @@ impl Module { /// The _last entry_ in the list should be the _return type_ of the function. /// In other words, the number of entries should be one larger than the number of parameters. #[inline(always)] - pub fn update_fn_metadata<'a>( - &mut self, - hash_fn: NonZeroU64, - arg_names: impl AsRef<[&'a str]>, - ) -> &mut Self { + pub fn update_fn_metadata(&mut self, hash_fn: NonZeroU64, arg_names: &[&str]) -> &mut Self { if let Some(f) = self.functions.get_mut(&hash_fn) { - f.param_names = arg_names.as_ref().iter().map(|&n| n.into()).collect(); + f.param_names = arg_names.iter().map(|&n| n.into()).collect(); } self } diff --git a/src/packages/iter_basic.rs b/src/packages/iter_basic.rs index 3fc39d50..3d927e9f 100644 --- a/src/packages/iter_basic.rs +++ b/src/packages/iter_basic.rs @@ -71,7 +71,7 @@ macro_rules! reg_range { $( $lib.set_iterator::>(); let hash = $lib.set_fn_2($x, get_range::<$y>); - $lib.update_fn_metadata(hash, [ + $lib.update_fn_metadata(hash, &[ concat!("from: ", stringify!($y)), concat!("to: ", stringify!($y)), concat!("Iterator") @@ -85,7 +85,7 @@ macro_rules! reg_stepped_range { $( $lib.set_iterator::>(); let hash = $lib.set_fn_3($x, get_step_range::<$y>); - $lib.update_fn_metadata(hash, [ + $lib.update_fn_metadata(hash, &[ concat!("from: ", stringify!($y)), concat!("to: ", stringify!($y)), concat!("step: ", stringify!($y)), diff --git a/src/syntax.rs b/src/syntax.rs index 96498a2c..4f276370 100644 --- a/src/syntax.rs +++ b/src/syntax.rs @@ -111,7 +111,7 @@ impl Engine { /// current [`Scope`][crate::Scope] will be _popped_. Do not randomly remove variables. pub fn register_custom_syntax + Into>( &mut self, - keywords: impl AsRef<[S]>, + keywords: &[S], new_vars: isize, func: impl Fn(&mut EvalContext, &[Expression]) -> Result> + SendSync