feat(atoms): Improve atoms (#5066)

This commit is contained in:
Donny/강동윤 2022-06-29 21:02:33 +09:00 committed by GitHub
parent 1a6997fb49
commit 34f4f5a3bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 3 deletions

1
Cargo.lock generated
View File

@ -3114,6 +3114,7 @@ dependencies = [
"siphasher",
"sourcemap",
"string_cache",
"swc_atoms",
"swc_eq_ignore_macros",
"swc_visit",
"termcolor",

View File

@ -117,8 +117,13 @@ pub struct AtomGenerator {
}
impl AtomGenerator {
pub fn gen(&mut self, s: &str) -> Atom {
if let Some(v) = self.inner.get(s).cloned() {
pub fn gen<S>(&mut self, s: S) -> Atom
where
Arc<str>: From<S>,
S: Eq + Hash,
S: AsRef<str>,
{
if let Some(v) = self.inner.get(s.as_ref()).cloned() {
return v;
}
@ -154,6 +159,20 @@ macro_rules! atom {
static CACHE: $crate::once_cell::sync::Lazy<$crate::Atom> =
$crate::once_cell::sync::Lazy::new(|| $crate::Atom::new_bad($s));
$crate::Atom::clone(*CACHE)
$crate::Atom::clone(&*CACHE)
}};
}
#[test]
fn _assert() {
let mut g = AtomGenerator::default();
g.gen("str");
g.gen(String::new());
}
impl PartialEq<Atom> for str {
fn eq(&self, other: &Atom) -> bool {
*self == **other
}
}

View File

@ -48,6 +48,7 @@ serde = { version = "1.0.119", features = ["derive"] }
siphasher = "0.3.9"
sourcemap = { version = "6", optional = true }
string_cache = "0.8.4"
swc_atoms = { version = "0.2.12", path = "../swc_atoms" }
swc_eq_ignore_macros = { version = "0.1", path = "../swc_eq_ignore_macros" }
swc_visit = { version = "0.3.0", path = "../swc_visit" }
termcolor = { version = "1.0", optional = true }

View File

@ -18,6 +18,13 @@ impl EqIgnoreSpan for Span {
}
}
impl EqIgnoreSpan for swc_atoms::Atom {
#[inline]
fn eq_ignore_span(&self, r: &Self) -> bool {
self == r
}
}
impl<T> EqIgnoreSpan for [T]
where
T: EqIgnoreSpan,