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

@@ -8,12 +8,11 @@ use std::prelude::v1::*;
/// _(internals)_ A factory of identifiers from text strings.
/// Exported under the `internals` feature only.
///
/// Normal identifiers are not interned since they use `SmartString` because most identifiers in
/// Rhai are short and ASCII-based. Thus copying is relatively fast.
///
/// Property getters and setters are interned separately.
/// Normal identifiers, property getters and setters are interned separately.
#[derive(Debug, Clone, Default, Hash)]
pub struct StringsInterner {
/// Normal strings.
strings: std::collections::BTreeMap<Identifier, ImmutableString>,
/// Property getters.
#[cfg(not(feature = "no_object"))]
getters: std::collections::BTreeMap<Identifier, ImmutableString>,
@@ -28,6 +27,7 @@ impl StringsInterner {
#[must_use]
pub fn new() -> Self {
Self {
strings: std::collections::BTreeMap::new(),
#[cfg(not(feature = "no_object"))]
getters: std::collections::BTreeMap::new(),
#[cfg(not(feature = "no_object"))]
@@ -36,39 +36,37 @@ impl StringsInterner {
}
/// Get an identifier from a text string and prefix, adding it to the interner if necessary.
///
/// # Prefix
///
/// Currently recognized prefixes are:
///
/// * `""` - None (normal string)
/// * `"get$"` - Property getter, not available under `no_object`
/// * `"set$"` - Property setter, not available under `no_object`
///
/// # Panics
///
/// Panics if the prefix is not recognized.
#[inline]
#[must_use]
pub fn get(
&mut self,
prefix: &'static str,
text: impl AsRef<str> + Into<Identifier> + Into<ImmutableString>,
) -> ImmutableString {
#[cfg(not(feature = "no_object"))]
{
let (dict, mapper) = match prefix {
"" => return text.into(),
FN_GET => (&mut self.getters, make_getter as fn(&str) -> String),
FN_SET => (&mut self.setters, make_setter as fn(&str) -> String),
_ => unreachable!("unsupported prefix {}", prefix),
};
pub fn get(&mut self, prefix: impl AsRef<str>, text: impl AsRef<str>) -> ImmutableString {
let (dict, mapper): (_, fn(&str) -> Identifier) = match prefix.as_ref() {
"" => (&mut self.strings, |s| s.into()),
if dict.contains_key(text.as_ref()) {
self.getters.get(text.as_ref()).expect("exists").clone()
} else {
let value: ImmutableString = mapper(text.as_ref()).into();
let text = text.into();
dict.insert(text, value.clone());
value
}
}
#[cfg(not(feature = "no_object"))]
FN_GET => (&mut self.getters, |s| make_getter(s)),
#[cfg(not(feature = "no_object"))]
FN_SET => (&mut self.setters, |s| make_setter(s)),
#[cfg(feature = "no_object")]
match prefix {
"" => return text.into(),
_ => unreachable!("unsupported prefix {}", prefix),
_ => unreachable!("unsupported prefix {}", prefix.as_ref()),
};
if dict.contains_key(text.as_ref()) {
dict.get(text.as_ref()).expect("exists").clone()
} else {
let value: ImmutableString = mapper(text.as_ref()).into();
dict.insert(text.as_ref().into(), value.clone());
value
}
}
}
@@ -76,27 +74,24 @@ impl StringsInterner {
impl AddAssign<Self> for StringsInterner {
#[inline(always)]
fn add_assign(&mut self, rhs: Self) {
let _rhs = rhs;
self.strings.extend(rhs.strings.into_iter());
#[cfg(not(feature = "no_object"))]
{
self.getters.extend(_rhs.getters.into_iter());
self.setters.extend(_rhs.setters.into_iter());
}
self.getters.extend(rhs.getters.into_iter());
#[cfg(not(feature = "no_object"))]
self.setters.extend(rhs.setters.into_iter());
}
}
impl AddAssign<&Self> for StringsInterner {
#[inline(always)]
fn add_assign(&mut self, rhs: &Self) {
let _rhs = rhs;
self.strings
.extend(rhs.strings.iter().map(|(k, v)| (k.clone(), v.clone())));
#[cfg(not(feature = "no_object"))]
{
self.getters
.extend(_rhs.getters.iter().map(|(k, v)| (k.clone(), v.clone())));
self.setters
.extend(_rhs.setters.iter().map(|(k, v)| (k.clone(), v.clone())));
}
self.getters
.extend(rhs.getters.iter().map(|(k, v)| (k.clone(), v.clone())));
#[cfg(not(feature = "no_object"))]
self.setters
.extend(rhs.setters.iter().map(|(k, v)| (k.clone(), v.clone())));
}
}