Reduce usage of as_ref and as_mut.

This commit is contained in:
Stephen Chung
2022-07-05 16:26:38 +08:00
parent 9319f87a7b
commit b6528bd51d
33 changed files with 211 additions and 137 deletions

View File

@@ -781,7 +781,7 @@ impl Module {
#[must_use]
pub fn get_sub_module(&self, name: &str) -> Option<&Module> {
if !self.modules.is_empty() {
self.modules.get(name).map(|m| m.as_ref())
self.modules.get(name).map(|m| &**m)
} else {
None
}
@@ -1006,18 +1006,19 @@ impl Module {
(names, return_type)
};
let hash_fn = calc_native_fn_hash(None, name.as_ref(), &param_types);
let name = name.as_ref();
let hash_fn = calc_native_fn_hash(None, name, &param_types);
if is_dynamic {
self.dynamic_functions
.insert(calc_fn_hash(name.as_ref(), param_types.len()));
.insert(calc_fn_hash(name, param_types.len()));
}
self.functions.insert(
hash_fn,
FuncInfo {
metadata: FnMetadata {
name: name.as_ref().into(),
name: name.into(),
namespace,
access,
params: param_types.len(),
@@ -1549,7 +1550,7 @@ impl Module {
/// Sub-modules are flattened onto the root [`Module`], with higher level overriding lower level.
#[inline]
pub fn combine_flatten(&mut self, other: Self) -> &mut Self {
for (.., m) in other.modules.into_iter() {
for (.., m) in other.modules {
self.combine_flatten(shared_take_or_clone(m));
}
self.variables.extend(other.variables.into_iter());
@@ -1707,7 +1708,7 @@ impl Module {
#[inline]
#[allow(dead_code)]
pub(crate) fn iter_fn(&self) -> impl Iterator<Item = &FuncInfo> {
self.functions.values().map(Box::as_ref)
self.functions.values().map(<_>::as_ref)
}
/// Get an iterator over all script-defined functions in the [`Module`].
@@ -2154,7 +2155,7 @@ impl Module {
#[must_use]
pub(crate) fn get_qualified_iter(&self, id: TypeId) -> Option<&IteratorFn> {
if !self.all_type_iterators.is_empty() {
self.all_type_iterators.get(&id).map(|f| f.as_ref())
self.all_type_iterators.get(&id).map(|f| &**f)
} else {
None
}
@@ -2165,7 +2166,7 @@ impl Module {
#[must_use]
pub(crate) fn get_iter(&self, id: TypeId) -> Option<&IteratorFn> {
if !self.type_iterators.is_empty() {
self.type_iterators.get(&id).map(|f| f.as_ref())
self.type_iterators.get(&id).map(|f| &**f)
} else {
None
}

View File

@@ -1,7 +1,7 @@
use crate::{Engine, Module, ModuleResolver, Position, RhaiResultOf, Shared, ERR};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{ops::AddAssign, vec::IntoIter};
use std::{ops::AddAssign, slice::Iter, vec::IntoIter};
/// [Module] resolution service that holds a collection of module resolvers,
/// to be searched in sequential order.
@@ -116,6 +116,16 @@ impl IntoIterator for ModuleResolversCollection {
}
}
impl<'a> IntoIterator for &'a ModuleResolversCollection {
type Item = &'a Box<dyn ModuleResolver>;
type IntoIter = Iter<'a, Box<dyn ModuleResolver>>;
#[inline(always)]
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
impl ModuleResolver for ModuleResolversCollection {
fn resolve(
&self,

View File

@@ -172,7 +172,7 @@ impl FileModuleResolver {
#[inline(always)]
#[must_use]
pub fn base_path(&self) -> Option<&Path> {
self.base_path.as_ref().map(PathBuf::as_ref)
self.base_path.as_ref().map(<_>::as_ref)
}
/// Set the base path for script files.
#[inline(always)]

View File

@@ -3,7 +3,11 @@ use crate::{
};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{collections::btree_map::IntoIter, collections::BTreeMap, ops::AddAssign};
use std::{
collections::btree_map::{IntoIter, Iter},
collections::BTreeMap,
ops::AddAssign,
};
/// A static [module][Module] resolution service that serves [modules][Module] added into it.
///
@@ -122,11 +126,22 @@ impl IntoIterator for StaticModuleResolver {
type Item = (Identifier, Shared<Module>);
type IntoIter = IntoIter<SmartString, Shared<Module>>;
#[inline(always)]
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<'a> IntoIterator for &'a StaticModuleResolver {
type Item = (&'a Identifier, &'a Shared<Module>);
type IntoIter = Iter<'a, SmartString, Shared<Module>>;
#[inline(always)]
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
impl ModuleResolver for StaticModuleResolver {
#[inline]
fn resolve(