mirror of
https://github.com/swc-project/swc.git
synced 2024-12-25 14:43:33 +03:00
feat(atoms): Improve atoms (#5066)
This commit is contained in:
parent
1a6997fb49
commit
34f4f5a3bc
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -3114,6 +3114,7 @@ dependencies = [
|
||||
"siphasher",
|
||||
"sourcemap",
|
||||
"string_cache",
|
||||
"swc_atoms",
|
||||
"swc_eq_ignore_macros",
|
||||
"swc_visit",
|
||||
"termcolor",
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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 }
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user