a bunch of stuff mostly

This commit is contained in:
nora 2023-05-22 20:44:18 +02:00
parent 56e7f77a0d
commit b9a2f939c4
29 changed files with 734 additions and 345 deletions

39
parser/src/sym.rs Normal file
View file

@ -0,0 +1,39 @@
use std::{cell::RefCell, fmt::Debug, marker::PhantomData};
use dbg_pls::DebugPls;
use lasso::Spur;
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Symbol {
spur: Spur,
not_send: PhantomData<*const ()>,
}
thread_local! {
static INTERNER: RefCell<lasso::Rodeo> = RefCell::new(lasso::Rodeo::new());
}
impl Symbol {
pub fn intern(s: &str) -> Self {
INTERNER.with(|i| Symbol {
spur: i.borrow_mut().get_or_intern(s),
not_send: PhantomData,
})
}
pub fn as_str<R>(&self, f: impl FnOnce(&str) -> R) -> R {
INTERNER.with(|i| f(i.borrow_mut().resolve(&self.spur)))
}
}
impl Debug for Symbol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
INTERNER.with(|i| f.write_str(i.borrow_mut().resolve(&self.spur)))
}
}
impl DebugPls for Symbol {
fn fmt(&self, f: dbg_pls::Formatter<'_>) {
self.as_str(|s| f.debug_ident(s))
}
}