Fixup AsRef<str> vs &str.

This commit is contained in:
Stephen Chung
2022-01-04 15:22:48 +08:00
parent 1d1e473ac4
commit d99953c101
13 changed files with 181 additions and 181 deletions

View File

@@ -193,10 +193,10 @@ impl Engine {
/// # }
/// ```
#[inline(always)]
pub fn compile_scripts_with_scope(
pub fn compile_scripts_with_scope<S: AsRef<str>>(
&self,
scope: &Scope,
scripts: &[impl AsRef<str>],
scripts: impl AsRef<[S]>,
) -> ParseResult<AST> {
self.compile_with_scope_and_optimization_level(
scope,
@@ -213,14 +213,16 @@ impl Engine {
/// throughout the script _including_ functions. This allows functions to be optimized based on
/// dynamic global constants.
#[inline]
pub(crate) fn compile_with_scope_and_optimization_level(
pub(crate) fn compile_with_scope_and_optimization_level<S: AsRef<str>>(
&self,
scope: &Scope,
scripts: &[impl AsRef<str>],
scripts: impl AsRef<[S]>,
#[cfg(not(feature = "no_optimize"))] optimization_level: crate::OptimizationLevel,
) -> ParseResult<AST> {
let (stream, tokenizer_control) =
self.lex_raw(scripts, self.token_mapper.as_ref().map(Box::as_ref));
let (stream, tokenizer_control) = self.lex_raw(
scripts.as_ref(),
self.token_mapper.as_ref().map(Box::as_ref),
);
let mut state = ParseState::new(self, tokenizer_control);
self.parse(
&mut stream.peekable(),

View File

@@ -209,9 +209,9 @@ impl Engine {
/// Replacing one variable with another (i.e. adding a new variable and removing one variable at
/// the same time so that the total _size_ of the [`Scope`][crate::Scope] is unchanged) also
/// does NOT count, so `false` should be passed.
pub fn register_custom_syntax(
pub fn register_custom_syntax<S: AsRef<str> + Into<Identifier>>(
&mut self,
symbols: &[impl AsRef<str> + Into<Identifier>],
symbols: impl AsRef<[S]>,
scope_may_be_changed: bool,
func: impl Fn(&mut EvalContext, &[Expression]) -> RhaiResult + SendSync + 'static,
) -> ParseResult<&mut Self> {
@@ -219,7 +219,7 @@ impl Engine {
let mut segments = StaticVec::<ImmutableString>::new();
for s in symbols {
for s in symbols.as_ref() {
let s = s.as_ref().trim();
// Skip empty symbols

View File

@@ -213,7 +213,7 @@ impl Engine {
/// ```
pub fn register_custom_operator(
&mut self,
keyword: impl AsRef<str> + Into<Identifier>,
keyword: impl AsRef<str>,
precedence: u8,
) -> Result<&mut Self, String> {
let precedence = Precedence::new(precedence);
@@ -247,7 +247,8 @@ impl Engine {
}
// Add to custom keywords
self.custom_keywords.insert(keyword.into(), precedence);
self.custom_keywords
.insert(keyword.as_ref().into(), precedence);
Ok(self)
}

View File

@@ -165,7 +165,7 @@ impl Engine {
pub fn register_raw_fn<N, T>(
&mut self,
name: N,
arg_types: &[TypeId],
arg_types: impl AsRef<[TypeId]>,
func: impl Fn(NativeCallContext, &mut FnCallArgs) -> RhaiResultOf<T> + SendSync + 'static,
) -> &mut Self
where
@@ -346,7 +346,7 @@ impl Engine {
name: impl AsRef<str>,
get_fn: impl Fn(&mut T) -> V + SendSync + 'static,
) -> &mut Self {
self.register_fn(&crate::engine::make_getter(name.as_ref()), get_fn)
self.register_fn(crate::engine::make_getter(name.as_ref()).as_str(), get_fn)
}
/// Register a getter function for a member of a registered type with the [`Engine`].
///
@@ -395,7 +395,7 @@ impl Engine {
name: impl AsRef<str>,
get_fn: impl Fn(&mut T) -> RhaiResultOf<V> + SendSync + 'static,
) -> &mut Self {
self.register_result_fn(&crate::engine::make_getter(name.as_ref()), get_fn)
self.register_result_fn(crate::engine::make_getter(name.as_ref()).as_str(), get_fn)
}
/// Register a setter function for a member of a registered type with the [`Engine`].
///
@@ -445,7 +445,7 @@ impl Engine {
name: impl AsRef<str>,
set_fn: impl Fn(&mut T, V) + SendSync + 'static,
) -> &mut Self {
self.register_fn(&crate::engine::make_setter(name.as_ref()), set_fn)
self.register_fn(crate::engine::make_setter(name.as_ref()).as_str(), set_fn)
}
/// Register a setter function for a member of a registered type with the [`Engine`].
///
@@ -496,7 +496,7 @@ impl Engine {
name: impl AsRef<str>,
set_fn: impl Fn(&mut T, V) -> RhaiResultOf<()> + SendSync + 'static,
) -> &mut Self {
self.register_result_fn(&crate::engine::make_setter(name.as_ref()), set_fn)
self.register_result_fn(crate::engine::make_setter(name.as_ref()).as_str(), set_fn)
}
/// Short-hand for registering both getter and setter functions
/// of a registered type with the [`Engine`].
@@ -975,17 +975,17 @@ impl Engine {
#[cfg(not(feature = "no_module"))]
pub fn register_static_module(
&mut self,
name: impl AsRef<str> + Into<Identifier>,
name: impl AsRef<str>,
module: Shared<Module>,
) -> &mut Self {
fn register_static_module_raw(
root: &mut std::collections::BTreeMap<Identifier, Shared<Module>>,
name: impl AsRef<str> + Into<Identifier>,
name: &str,
module: Shared<Module>,
) {
let separator = crate::tokenizer::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::func::native::shared_take_or_clone(module);
@@ -995,7 +995,7 @@ impl Engine {
root.insert(name.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().expect("contains separator").trim();
let remainder = iter.next().expect("contains separator").trim();
@@ -1014,7 +1014,7 @@ impl Engine {
}
}
register_static_module_raw(&mut self.global_sub_modules, name, module);
register_static_module_raw(&mut self.global_sub_modules, name.as_ref(), module);
self
}
/// _(metadata)_ Generate a list of all registered functions.