use std::cmp::Ordering;
use std::collections::HashMap;
use std::fmt::{self, Debug, Formatter};
use std::sync::RwLock;
use once_cell::sync::Lazy;
static INTERNER: Lazy<RwLock<Interner>> =
Lazy::new(|| RwLock::new(Interner { to_id: HashMap::new(), from_id: Vec::new() }));
struct Interner {
to_id: HashMap<&'static str, PicoStr>,
from_id: Vec<&'static str>,
}
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct PicoStr(u32);
impl PicoStr {
pub fn new(string: &str) -> Self {
let mut interner = INTERNER.write().unwrap();
if let Some(&id) = interner.to_id.get(string) {
return id;
}
let num = interner.from_id.len().try_into().expect("out of string ids");
let id = Self(num);
let string = Box::leak(string.to_string().into_boxed_str());
interner.to_id.insert(string, id);
interner.from_id.push(string);
id
}
pub fn resolve(&self) -> &'static str {
INTERNER.read().unwrap().from_id[self.0 as usize]
}
}
impl Debug for PicoStr {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.resolve().fmt(f)
}
}
impl Ord for PicoStr {
fn cmp(&self, other: &Self) -> Ordering {
self.resolve().cmp(other.resolve())
}
}
impl PartialOrd for PicoStr {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl AsRef<str> for PicoStr {
fn as_ref(&self) -> &str {
self.resolve()
}
}
impl From<&str> for PicoStr {
fn from(value: &str) -> Self {
Self::new(value)
}
}