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

@@ -56,7 +56,7 @@ impl FnPtr {
#[inline(always)]
#[must_use]
pub fn fn_name(&self) -> &str {
self.fn_name_raw().as_ref()
self.fn_name_raw().as_str()
}
/// Get the name of the function.
#[inline(always)]
@@ -223,7 +223,7 @@ impl FnPtr {
args_data = StaticVec::with_capacity(self.curry().len() + arg_values.len());
args_data.extend(self.curry().iter().cloned());
args_data.extend(arg_values.iter_mut().map(mem::take));
arg_values = args_data.as_mut();
arg_values = &mut *args_data;
};
let is_method = this_ptr.is_some();

View File

@@ -51,7 +51,10 @@ impl StringsInterner<'_> {
#[inline]
#[must_use]
pub fn get(&mut self, prefix: impl AsRef<str>, text: impl AsRef<str>) -> ImmutableString {
let (dict, mapper): (_, fn(&str) -> Identifier) = match prefix.as_ref() {
let prefix = prefix.as_ref();
let text = text.as_ref();
let (dict, mapper): (_, fn(&str) -> Identifier) = match prefix {
"" => (&mut self.strings, |s| s.into()),
#[cfg(not(feature = "no_object"))]
@@ -59,14 +62,14 @@ impl StringsInterner<'_> {
#[cfg(not(feature = "no_object"))]
crate::engine::FN_SET => (&mut self.setters, crate::engine::make_setter),
_ => unreachable!("unsupported prefix {}", prefix.as_ref()),
_ => unreachable!("unsupported prefix {}", prefix),
};
if !dict.is_empty() && dict.contains_key(text.as_ref()) {
dict.get(text.as_ref()).unwrap().clone()
if !dict.is_empty() && dict.contains_key(text) {
dict.get(text).unwrap().clone()
} else {
let value: ImmutableString = mapper(text.as_ref()).into();
dict.insert(text.as_ref().into(), value.clone());
let value: ImmutableString = mapper(text).into();
dict.insert(text.into(), value.clone());
value
}
}

View File

@@ -132,6 +132,21 @@ impl IntoIterator for Scope<'_> {
}
}
impl<'a> IntoIterator for &'a Scope<'_> {
type Item = (&'a Identifier, &'a Dynamic, &'a Vec<Identifier>);
type IntoIter = Box<dyn Iterator<Item = Self::Item> + 'a>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
Box::new(
self.values
.iter()
.zip(self.names.iter().zip(self.aliases.iter()))
.map(|(value, (name, alias))| (name, value, alias)),
)
}
}
impl Scope<'_> {
/// Create a new [`Scope`].
///
@@ -686,7 +701,7 @@ impl Scope<'_> {
self.names
.iter()
.zip(self.values.iter())
.map(|(name, value)| (name.as_ref(), value.is_read_only(), value))
.map(|(name, value)| (name.as_str(), value.is_read_only(), value))
}
/// Get a reverse iterator to entries in the [`Scope`].
/// Shared values are not expanded.
@@ -696,7 +711,7 @@ impl Scope<'_> {
.iter()
.rev()
.zip(self.values.iter().rev())
.map(|(name, value)| (name.as_ref(), value.is_read_only(), value))
.map(|(name, value)| (name.as_str(), value.is_read_only(), value))
}
/// Remove a range of entries within the [`Scope`].
///