Introduce LocalInternedString::intern.

`LocalInternedString::intern(x)` is preferable to
`Symbol::intern(x).as_str()`, because the former involves one call to
`with_interner` while the latter involves two.
This commit is contained in:
Nicholas Nethercote 2019-05-14 15:13:42 +10:00
parent 257eaf523f
commit c06cdbeac5
4 changed files with 24 additions and 12 deletions

View file

@ -1029,6 +1029,17 @@ pub struct LocalInternedString {
}
impl LocalInternedString {
/// Maps a string to its interned representation.
pub fn intern(string: &str) -> Self {
let string = with_interner(|interner| {
let symbol = interner.intern(string);
interner.strings[symbol.0.as_usize()]
});
LocalInternedString {
string: unsafe { std::mem::transmute::<&str, &str>(string) }
}
}
pub fn as_interned_str(self) -> InternedString {
InternedString {
symbol: Symbol::intern(self.string)
@ -1105,7 +1116,7 @@ impl fmt::Display for LocalInternedString {
impl Decodable for LocalInternedString {
fn decode<D: Decoder>(d: &mut D) -> Result<LocalInternedString, D::Error> {
Ok(Symbol::intern(&d.read_str()?).as_str())
Ok(LocalInternedString::intern(&d.read_str()?))
}
}