use std::fmt;
use std::str::FromStr;
use std::sync::Mutex;
use indexmap::IndexSet;
use once_cell::sync::Lazy;
static STRINGS: Lazy<Mutex<IndexSet<&'static str>>> = Lazy::new(Default::default);
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Symbol(u32);
impl Symbol {
pub fn as_str(self) -> &'static str {
let i = self.0 as usize;
let strings = STRINGS
.lock()
.unwrap_or_else(|err| panic!("Failed to acquire egg's global string cache: {}", err));
strings.get_index(i).unwrap()
}
}
fn leak(s: &str) -> &'static str {
Box::leak(s.to_owned().into_boxed_str())
}
fn intern(s: &str) -> Symbol {
let mut strings = STRINGS
.lock()
.unwrap_or_else(|err| panic!("Failed to acquire egg's global string cache: {}", err));
let i = match strings.get_full(s) {
Some((i, _)) => i,
None => strings.insert_full(leak(s)).0,
};
Symbol(i as u32)
}
impl<S: AsRef<str>> From<S> for Symbol {
fn from(s: S) -> Self {
intern(s.as_ref())
}
}
impl FromStr for Symbol {
type Err = std::convert::Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(s.into())
}
}
impl fmt::Display for Symbol {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl fmt::Debug for Symbol {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.as_str())
}
}