Remove unnecessary AsRef and impl IntoIterator.

This commit is contained in:
Stephen Chung 2021-03-01 15:58:11 +08:00
parent 061fce1f02
commit b466d58192
8 changed files with 35 additions and 39 deletions

View File

@ -18,6 +18,7 @@ Breaking changes
* `EvalAltResult::clear_position` is renamed `EvalAltResult::take_position` and returns the position taken. * `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. * `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. * `NativeCallContext::call_fn_dynamic_raw` no longer has the `pub_only` parameter.
* `Module::update_fn_metadata` input parameter is changed.
Enhancements Enhancements
------------ ------------

View File

@ -1726,9 +1726,16 @@ impl Engine {
.map(|(val, _)| val.take_or_clone()), .map(|(val, _)| val.take_or_clone()),
// Statement block // Statement block
Expr::Stmt(x, _) => { Expr::Stmt(x, _) => self.eval_stmt_block(
self.eval_stmt_block(scope, mods, state, lib, this_ptr, x.as_ref(), true, level) scope,
} mods,
state,
lib,
this_ptr,
x.as_ref().as_ref(),
true,
level,
),
// lhs[idx_expr] // lhs[idx_expr]
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
@ -1857,14 +1864,14 @@ impl Engine {
} }
/// Evaluate a statements block. /// Evaluate a statements block.
pub(crate) fn eval_stmt_block<'a>( pub(crate) fn eval_stmt_block(
&self, &self,
scope: &mut Scope, scope: &mut Scope,
mods: &mut Imports, mods: &mut Imports,
state: &mut State, state: &mut State,
lib: &[&Module], lib: &[&Module],
this_ptr: &mut Option<&mut Dynamic>, this_ptr: &mut Option<&mut Dynamic>,
statements: impl IntoIterator<Item = &'a Stmt>, statements: &[Stmt],
restore: bool, restore: bool,
level: usize, level: usize,
) -> Result<Dynamic, Box<EvalAltResult>> { ) -> Result<Dynamic, Box<EvalAltResult>> {
@ -1877,7 +1884,7 @@ impl Engine {
state.scope_level += 1; 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"))] #[cfg(not(feature = "no_module"))]
match stmt { match stmt {
Stmt::Import(_, _, _) => { Stmt::Import(_, _, _) => {

View File

@ -764,29 +764,25 @@ impl Engine {
/// # } /// # }
/// ``` /// ```
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
pub fn register_static_module( pub fn register_static_module(&mut self, name: &str, module: Shared<Module>) -> &mut Self {
&mut self,
name: impl AsRef<str>,
module: Shared<Module>,
) -> &mut Self {
fn register_static_module_raw( fn register_static_module_raw(
root: &mut crate::stdlib::collections::HashMap<crate::ImmutableString, Shared<Module>>, root: &mut crate::stdlib::collections::HashMap<crate::ImmutableString, Shared<Module>>,
name: impl AsRef<str>, name: &str,
module: Shared<Module>, module: Shared<Module>,
) { ) {
let separator = crate::token::Token::DoubleColon.syntax(); 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() { if !module.is_indexed() {
// Index the module (making a clone copy if necessary) if it is not 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); let mut module = crate::fn_native::shared_take_or_clone(module);
module.build_index(); module.build_index();
root.insert(name.as_ref().trim().into(), module.into()); root.insert(name.trim().into(), module.into());
} else { } else {
root.insert(name.as_ref().trim().into(), module); root.insert(name.trim().into(), module);
} }
} else { } 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 sub_module = iter.next().unwrap().trim();
let remainder = iter.next().unwrap().trim(); let remainder = iter.next().unwrap().trim();
@ -817,11 +813,7 @@ impl Engine {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
#[inline(always)] #[inline(always)]
#[deprecated = "use `register_static_module` instead"] #[deprecated = "use `register_static_module` instead"]
pub fn register_module( pub fn register_module(&mut self, name: &str, module: impl Into<Shared<Module>>) -> &mut Self {
&mut self,
name: impl AsRef<str>,
module: impl Into<Shared<Module>>,
) -> &mut Self {
self.register_static_module(name, module.into()) self.register_static_module(name, module.into())
} }
/// Compile a string into an [`AST`], which can be used later for evaluation. /// Compile a string into an [`AST`], which can be used later for evaluation.

View File

@ -545,7 +545,7 @@ impl Engine {
state: Option<&mut State>, state: Option<&mut State>,
lib: &[&Module], lib: &[&Module],
fn_name: &str, fn_name: &str,
arg_types: impl AsRef<[TypeId]>, arg_types: &[TypeId],
) -> bool { ) -> bool {
let arg_types = arg_types.as_ref(); let arg_types = arg_types.as_ref();
let hash_fn = calc_native_fn_hash(empty(), fn_name, arg_types.iter().cloned()); 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. /// 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. /// This is commonly used to evaluate a list of statements in an [`AST`] or a script function body.
#[inline] #[inline]
pub(crate) fn eval_global_statements<'a>( pub(crate) fn eval_global_statements(
&self, &self,
scope: &mut Scope, scope: &mut Scope,
mods: &mut Imports, mods: &mut Imports,
state: &mut State, state: &mut State,
statements: impl IntoIterator<Item = &'a Stmt>, statements: &[Stmt],
lib: &[&Module], lib: &[&Module],
level: usize, level: usize,
) -> Result<Dynamic, Box<EvalAltResult>> { ) -> Result<Dynamic, Box<EvalAltResult>> {
@ -1022,7 +1022,7 @@ impl Engine {
lib: &[&Module], lib: &[&Module],
this_ptr: &mut Option<&mut Dynamic>, this_ptr: &mut Option<&mut Dynamic>,
fn_name: &str, fn_name: &str,
args_expr: impl AsRef<[Expr]>, args_expr: &[Expr],
mut hash_script: Option<NonZeroU64>, mut hash_script: Option<NonZeroU64>,
pos: Position, pos: Position,
capture_scope: bool, capture_scope: bool,
@ -1260,7 +1260,7 @@ impl Engine {
this_ptr: &mut Option<&mut Dynamic>, this_ptr: &mut Option<&mut Dynamic>,
namespace: Option<&NamespaceRef>, namespace: Option<&NamespaceRef>,
fn_name: &str, fn_name: &str,
args_expr: impl AsRef<[Expr]>, args_expr: &[Expr],
hash_script: NonZeroU64, hash_script: NonZeroU64,
pos: Position, pos: Position,
level: usize, level: usize,

View File

@ -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> { impl<'e, 'n, 's, 'a, 'm> NativeCallContext<'e, 'n, 's, 'a, 'm> {
/// Create a new [`NativeCallContext`]. /// Create a new [`NativeCallContext`].
#[inline(always)] #[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 { Self {
engine, engine,
fn_name, fn_name,
source: None, source: None,
mods: None, mods: None,
lib: lib.as_ref(), lib,
} }
} }
/// _(INTERNALS)_ Create a new [`NativeCallContext`]. /// _(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, fn_name: &'n str,
source: &'s Option<&str>, source: &'s Option<&str>,
imports: &'a mut Imports, imports: &'a mut Imports,
lib: &'m impl AsRef<[&'m Module]>, lib: &'m [&'m Module],
) -> Self { ) -> Self {
Self { Self {
engine, engine,
fn_name, fn_name,
source: source.clone(), source: source.clone(),
mods: Some(imports), mods: Some(imports),
lib: lib.as_ref(), lib,
} }
} }
/// The current [`Engine`]. /// The current [`Engine`].

View File

@ -640,13 +640,9 @@ impl Module {
/// The _last entry_ in the list should be the _return type_ of the function. /// 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. /// In other words, the number of entries should be one larger than the number of parameters.
#[inline(always)] #[inline(always)]
pub fn update_fn_metadata<'a>( pub fn update_fn_metadata(&mut self, hash_fn: NonZeroU64, arg_names: &[&str]) -> &mut Self {
&mut self,
hash_fn: NonZeroU64,
arg_names: impl AsRef<[&'a str]>,
) -> &mut Self {
if let Some(f) = self.functions.get_mut(&hash_fn) { 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 self
} }

View File

@ -71,7 +71,7 @@ macro_rules! reg_range {
$( $(
$lib.set_iterator::<Range<$y>>(); $lib.set_iterator::<Range<$y>>();
let hash = $lib.set_fn_2($x, get_range::<$y>); 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!("from: ", stringify!($y)),
concat!("to: ", stringify!($y)), concat!("to: ", stringify!($y)),
concat!("Iterator<Item=", stringify!($y), ">") concat!("Iterator<Item=", stringify!($y), ">")
@ -85,7 +85,7 @@ macro_rules! reg_stepped_range {
$( $(
$lib.set_iterator::<StepRange<$y>>(); $lib.set_iterator::<StepRange<$y>>();
let hash = $lib.set_fn_3($x, get_step_range::<$y>); 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!("from: ", stringify!($y)),
concat!("to: ", stringify!($y)), concat!("to: ", stringify!($y)),
concat!("step: ", stringify!($y)), concat!("step: ", stringify!($y)),

View File

@ -111,7 +111,7 @@ impl Engine {
/// current [`Scope`][crate::Scope] will be _popped_. Do not randomly remove variables. /// current [`Scope`][crate::Scope] will be _popped_. Do not randomly remove variables.
pub fn register_custom_syntax<S: AsRef<str> + Into<ImmutableString>>( pub fn register_custom_syntax<S: AsRef<str> + Into<ImmutableString>>(
&mut self, &mut self,
keywords: impl AsRef<[S]>, keywords: &[S],
new_vars: isize, new_vars: isize,
func: impl Fn(&mut EvalContext, &[Expression]) -> Result<Dynamic, Box<EvalAltResult>> func: impl Fn(&mut EvalContext, &[Expression]) -> Result<Dynamic, Box<EvalAltResult>>
+ SendSync + SendSync