Satisfy Clippy.

This commit is contained in:
Stephen Chung
2022-11-23 13:24:14 +08:00
parent 2c73d403f7
commit 31292e683d
29 changed files with 193 additions and 158 deletions

View File

@@ -18,6 +18,7 @@ use std::{
/// Options for calling a script-defined function via [`Engine::call_fn_with_options`].
#[derive(Debug, Hash)]
#[non_exhaustive]
#[must_use]
pub struct CallFnOptions<'t> {
/// A value for binding to the `this` pointer (if any).
pub this_ptr: Option<&'t mut Dynamic>,
@@ -120,7 +121,7 @@ impl Engine {
name: impl AsRef<str>,
args: impl FuncArgs,
) -> RhaiResultOf<T> {
self.call_fn_with_options(Default::default(), scope, ast, name, args)
self.call_fn_with_options(CallFnOptions::default(), scope, ast, name, args)
}
/// Call a script function defined in an [`AST`] with multiple [`Dynamic`] arguments.
///
@@ -255,20 +256,23 @@ impl Engine {
#[cfg(not(feature = "no_closure"))]
crate::func::ensure_no_data_race(name, args, false).map(|_| Dynamic::UNIT)?;
if let Some(fn_def) = ast.shared_lib().get_script_fn(name, args.len()) {
self.call_script_fn(
global,
caches,
scope,
this_ptr,
fn_def,
args,
rewind_scope,
Position::NONE,
ast.shared_lib()
.get_script_fn(name, args.len())
.map_or_else(
|| Err(ERR::ErrorFunctionNotFound(name.into(), Position::NONE).into()),
|fn_def| {
self.call_script_fn(
global,
caches,
scope,
this_ptr,
fn_def,
args,
rewind_scope,
Position::NONE,
)
},
)
} else {
Err(ERR::ErrorFunctionNotFound(name.into(), Position::NONE).into())
}
});
#[cfg(feature = "debugging")]

View File

@@ -1,7 +1,7 @@
//! Module that defines the public compilation API of [`Engine`].
use crate::parser::{ParseResult, ParseState};
use crate::{Engine, OptimizationLevel, Scope, AST};
use crate::{Engine, OptimizationLevel, Scope, StringsInterner, AST};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@@ -221,7 +221,7 @@ impl Engine {
scripts.as_ref(),
self.token_mapper.as_ref().map(<_>::as_ref),
);
let mut state = ParseState::new(self, scope, Default::default(), tokenizer_control);
let mut state = ParseState::new(self, scope, StringsInterner::default(), tokenizer_control);
let mut _ast = self.parse(&mut stream.peekable(), &mut state, optimization_level)?;
#[cfg(feature = "metadata")]
_ast.set_doc(state.tokenizer_control.borrow().global_comments.join("\n"));
@@ -294,7 +294,7 @@ impl Engine {
self.lex_raw(&scripts, self.token_mapper.as_ref().map(<_>::as_ref));
let mut peekable = stream.peekable();
let mut state = ParseState::new(self, scope, Default::default(), tokenizer_control);
let mut state = ParseState::new(self, scope, StringsInterner::default(), tokenizer_control);
self.parse_global_expr(&mut peekable, &mut state, |_| {}, self.optimization_level)
}
}

View File

@@ -77,7 +77,6 @@ pub struct DefinitionsConfig {
impl Default for DefinitionsConfig {
#[inline(always)]
#[must_use]
fn default() -> Self {
Self {
write_headers: false,
@@ -105,13 +104,13 @@ impl Definitions<'_> {
/// Headers are always present in content that is expected to be written to a file
/// (i.e. `write_to*` and `*_file` methods).
#[inline(always)]
pub fn with_headers(mut self, headers: bool) -> Self {
pub const fn with_headers(mut self, headers: bool) -> Self {
self.config.write_headers = headers;
self
}
/// Include standard packages when writing definition files.
#[inline(always)]
pub fn include_standard_packages(mut self, include_standard_packages: bool) -> Self {
pub const fn include_standard_packages(mut self, include_standard_packages: bool) -> Self {
self.config.include_standard_packages = include_standard_packages;
self
}
@@ -129,7 +128,6 @@ impl Definitions<'_> {
}
/// Get the configuration.
#[inline(always)]
#[must_use]
pub(crate) const fn config(&self) -> &DefinitionsConfig {
&self.config
}
@@ -177,19 +175,19 @@ impl Definitions<'_> {
let mut def_file = String::from("module static;\n\n");
if config.include_standard_packages {
def_file += &self.builtin_functions_operators_impl(&config);
def_file += &Self::builtin_functions_operators_impl(config);
def_file += "\n";
def_file += &self.builtin_functions_impl(&config);
def_file += &Self::builtin_functions_impl(config);
def_file += "\n";
}
def_file += &self.static_module_impl(&config);
def_file += &self.static_module_impl(config);
def_file += "\n";
#[cfg(not(feature = "no_module"))]
{
use std::fmt::Write;
for (module_name, module_def) in self.modules_impl(&config) {
for (module_name, module_def) in self.modules_impl(config) {
write!(
&mut def_file,
"\nmodule {module_name} {{\n{module_def}\n}}\n"
@@ -199,7 +197,7 @@ impl Definitions<'_> {
def_file += "\n";
}
def_file += &self.scope_items_impl(&config);
def_file += &self.scope_items_impl(config);
def_file += "\n";
@@ -220,11 +218,11 @@ impl Definitions<'_> {
vec![
(
"__builtin__.d.rhai".to_string(),
self.builtin_functions_impl(&config),
Self::builtin_functions_impl(config),
),
(
"__builtin-operators__.d.rhai".to_string(),
self.builtin_functions_operators_impl(&config),
Self::builtin_functions_operators_impl(config),
),
]
} else {
@@ -233,18 +231,18 @@ impl Definitions<'_> {
.into_iter()
.chain(std::iter::once((
"__static__.d.rhai".to_string(),
self.static_module_impl(&config),
self.static_module_impl(config),
)))
.chain(self.scope.iter().map(move |_| {
(
"__scope__.d.rhai".to_string(),
self.scope_items_impl(&config),
self.scope_items_impl(config),
)
}))
.chain(
#[cfg(not(feature = "no_module"))]
{
self.modules_impl(&config)
self.modules_impl(config)
.map(|(name, def)| (format!("{name}.d.rhai"), def))
},
#[cfg(feature = "no_module")]
@@ -258,12 +256,12 @@ impl Definitions<'_> {
#[inline(always)]
#[must_use]
pub fn builtin_functions(&self) -> String {
self.builtin_functions_impl(&self.config)
Self::builtin_functions_impl(self.config)
}
/// Return definitions for all builtin functions.
#[must_use]
fn builtin_functions_impl(&self, config: &DefinitionsConfig) -> String {
fn builtin_functions_impl(config: DefinitionsConfig) -> String {
let def = include_str!("builtin-functions.d.rhai");
if config.write_headers {
@@ -277,12 +275,12 @@ impl Definitions<'_> {
#[inline(always)]
#[must_use]
pub fn builtin_functions_operators(&self) -> String {
self.builtin_functions_operators_impl(&self.config)
Self::builtin_functions_operators_impl(self.config)
}
/// Return definitions for all builtin operators.
#[must_use]
fn builtin_functions_operators_impl(&self, config: &DefinitionsConfig) -> String {
fn builtin_functions_operators_impl(config: DefinitionsConfig) -> String {
let def = include_str!("builtin-operators.d.rhai");
if config.write_headers {
@@ -296,22 +294,22 @@ impl Definitions<'_> {
#[inline(always)]
#[must_use]
pub fn static_module(&self) -> String {
self.static_module_impl(&self.config)
self.static_module_impl(self.config)
}
/// Return definitions for all globally available functions and constants.
#[must_use]
fn static_module_impl(&self, config: &DefinitionsConfig) -> String {
fn static_module_impl(&self, config: DefinitionsConfig) -> String {
let mut s = if config.write_headers {
String::from("module static;\n\n")
} else {
String::new()
};
let exclude_flags = if !self.config.include_standard_packages {
ModuleFlags::STANDARD_LIB
} else {
let exclude_flags = if self.config.include_standard_packages {
ModuleFlags::empty()
} else {
ModuleFlags::STANDARD_LIB
};
self.engine
@@ -333,12 +331,12 @@ impl Definitions<'_> {
#[inline(always)]
#[must_use]
pub fn scope_items(&self) -> String {
self.scope_items_impl(&self.config)
self.scope_items_impl(self.config)
}
/// Return definitions for all items inside the [`Scope`], if any.
#[must_use]
fn scope_items_impl(&self, config: &DefinitionsConfig) -> String {
fn scope_items_impl(&self, config: DefinitionsConfig) -> String {
let mut s = if config.write_headers {
String::from("module static;\n\n")
} else {
@@ -358,14 +356,14 @@ impl Definitions<'_> {
#[cfg(not(feature = "no_module"))]
#[inline(always)]
pub fn modules(&self) -> impl Iterator<Item = (String, String)> + '_ {
self.modules_impl(&self.config)
self.modules_impl(self.config)
}
/// Return a (module name, definitions) pair for each registered static [module][Module].
#[cfg(not(feature = "no_module"))]
fn modules_impl(
&self,
config: &DefinitionsConfig,
config: DefinitionsConfig,
) -> impl Iterator<Item = (String, String)> + '_ {
let mut m = self
.engine

View File

@@ -4,7 +4,8 @@ use crate::eval::{Caches, GlobalRuntimeState};
use crate::parser::ParseState;
use crate::types::dynamic::Variant;
use crate::{
reify, Dynamic, Engine, OptimizationLevel, Position, RhaiResult, RhaiResultOf, Scope, AST, ERR,
reify, Dynamic, Engine, OptimizationLevel, Position, RhaiResult, RhaiResultOf, Scope,
StringsInterner, AST, ERR,
};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@@ -69,7 +70,7 @@ impl Engine {
) -> RhaiResultOf<T> {
let ast = self.compile_with_scope_and_optimization_level(
scope,
&[script],
[script],
self.optimization_level,
)?;
self.eval_ast_with_scope(scope, &ast)
@@ -119,7 +120,7 @@ impl Engine {
let scripts = [script];
let (stream, tokenizer_control) =
self.lex_raw(&scripts, self.token_mapper.as_ref().map(<_>::as_ref));
let mut state = ParseState::new(self, scope, Default::default(), tokenizer_control);
let mut state = ParseState::new(self, scope, StringsInterner::default(), tokenizer_control);
// No need to optimize a lone expression
let ast = self.parse_global_expr(

View File

@@ -3,7 +3,7 @@
use crate::parser::{ParseSettingFlags, ParseState};
use crate::tokenizer::Token;
use crate::{Engine, LexError, Map, OptimizationLevel, RhaiResultOf, Scope};
use crate::{Engine, LexError, Map, OptimizationLevel, RhaiResultOf, Scope, StringsInterner};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@@ -117,7 +117,8 @@ impl Engine {
);
let scope = Scope::new();
let mut state = ParseState::new(self, &scope, Default::default(), tokenizer_control);
let mut state =
ParseState::new(self, &scope, StringsInterner::default(), tokenizer_control);
let ast = self.parse_global_expr(
&mut stream.peekable(),
@@ -165,7 +166,7 @@ pub fn format_map_as_json(map: &Map) -> String {
result.push(':');
if let Some(val) = value.read_lock::<Map>() {
result.push_str(&format_map_as_json(&*val));
result.push_str(&format_map_as_json(&val));
} else if value.is_unit() {
result.push_str("null");
} else {

View File

@@ -191,7 +191,7 @@ impl Engine {
/// Get the default value of the custom state for each evaluation run.
#[inline(always)]
#[must_use]
pub fn default_tag(&self) -> &Dynamic {
pub const fn default_tag(&self) -> &Dynamic {
&self.def_tag
}
/// Get a mutable reference to the default value of the custom state for each evaluation run.

View File

@@ -742,10 +742,10 @@ impl Engine {
signatures.extend(m.gen_fn_signatures().map(|f| format!("{name}::{f}")));
}
let exclude_flags = if !include_packages {
ModuleFlags::INTERNAL | ModuleFlags::STANDARD_LIB
} else {
let exclude_flags = if include_packages {
ModuleFlags::INTERNAL
} else {
ModuleFlags::INTERNAL | ModuleFlags::STANDARD_LIB
};
signatures.extend(

View File

@@ -2,7 +2,7 @@
use crate::eval::{Caches, GlobalRuntimeState};
use crate::parser::ParseState;
use crate::{Engine, RhaiResultOf, Scope, AST};
use crate::{Engine, RhaiResultOf, Scope, StringsInterner, AST};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@@ -58,7 +58,7 @@ impl Engine {
let scripts = [script];
let (stream, tokenizer_control) =
self.lex_raw(&scripts, self.token_mapper.as_ref().map(<_>::as_ref));
let mut state = ParseState::new(self, scope, Default::default(), tokenizer_control);
let mut state = ParseState::new(self, scope, StringsInterner::default(), tokenizer_control);
let ast = self.parse(&mut stream.peekable(), &mut state, self.optimization_level)?;
self.run_ast_with_scope(scope, &ast)
}

View File

@@ -139,8 +139,8 @@ pub fn format_type(typ: &str, is_return_type: bool) -> std::borrow::Cow<str> {
} else {
format!("&mut {r}").into()
};
} else if typ.contains(" ") {
let typ = typ.replace(" ", "");
} else if typ.contains(' ') {
let typ = typ.replace(' ', "");
let r = format_type(&typ, is_return_type);
return r.into_owned().into();
}