Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(atoms): Improve atoms #5066

Merged
merged 3 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 22 additions & 3 deletions crates/swc_atoms/src/lib.rs
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
}
}
1 change: 1 addition & 0 deletions crates/swc_common/Cargo.toml
Expand Up @@ -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 }
Expand Down
7 changes: 7 additions & 0 deletions crates/swc_common/src/eq.rs
Expand Up @@ -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,
Expand Down