rhai/src/types/interner.rs

91 lines
2.9 KiB
Rust
Raw Normal View History

2021-12-27 15:28:11 +01:00
#[cfg(not(feature = "no_object"))]
2021-12-27 14:56:50 +01:00
use crate::engine::{make_getter, make_setter, FN_GET, FN_SET};
use crate::{Identifier, ImmutableString};
2021-12-27 15:28:11 +01:00
use std::ops::AddAssign;
2021-12-27 14:56:50 +01:00
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
/// _(internals)_ A factory of identifiers from text strings.
/// Exported under the `internals` feature only.
///
/// Since [`SmartString`](https://crates.io/crates/smartstring) is used as [`Identifier`],
/// this just returns a copy because most identifiers in Rhai are short and ASCII-based.
///
/// Property getters and setters are interned separately.
#[derive(Debug, Clone, Default, Hash)]
pub struct StringsInterner {
/// Property getters.
2021-12-27 15:28:11 +01:00
#[cfg(not(feature = "no_object"))]
getters: std::collections::BTreeMap<Identifier, ImmutableString>,
2021-12-27 14:56:50 +01:00
/// Property setters.
2021-12-27 15:28:11 +01:00
#[cfg(not(feature = "no_object"))]
setters: std::collections::BTreeMap<Identifier, ImmutableString>,
2021-12-27 14:56:50 +01:00
}
impl StringsInterner {
/// Create a new [`IdentifierBuilder`].
#[inline]
#[must_use]
pub fn new() -> Self {
Self {
2021-12-27 15:28:11 +01:00
#[cfg(not(feature = "no_object"))]
getters: std::collections::BTreeMap::new(),
#[cfg(not(feature = "no_object"))]
setters: std::collections::BTreeMap::new(),
2021-12-27 14:56:50 +01:00
}
}
/// Get an identifier from a text string and prefix, adding it to the interner if necessary.
///
/// # 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 {
2021-12-27 15:28:11 +01:00
#[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),
};
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(feature = "no_object")]
match prefix {
2021-12-27 14:56:50 +01:00
"" => return text.into(),
_ => unreachable!("unsupported prefix {}", prefix),
}
}
/// Merge another [`IdentifierBuilder`] into this.
#[inline(always)]
pub fn merge(&mut self, _other: &Self) {}
}
impl AddAssign for StringsInterner {
#[inline(always)]
fn add_assign(&mut self, rhs: Self) {
2021-12-27 15:28:11 +01:00
let _rhs = rhs;
#[cfg(not(feature = "no_object"))]
{
self.getters.extend(_rhs.getters.into_iter());
self.setters.extend(_rhs.setters.into_iter());
}
2021-12-27 14:56:50 +01:00
}
}