From c30a982da3b7e2d92c55fa9dcb6169e81d574a5f Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Wed, 18 Dec 2024 02:25:34 +0000 Subject: [PATCH] feat(span): add `impl From for Atom` (#7973) Add conversion method from `oxc_allocator::String` to `Atom`. This is a zero-cost conversion, because the string is already in the arena. --- crates/oxc_ast/src/ast_builder_impl.rs | 2 +- crates/oxc_span/src/atom.rs | 8 +++++++- crates/oxc_transformer/src/common/helper_loader.rs | 2 +- crates/oxc_transformer/src/es2022/class_static_block.rs | 2 +- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/crates/oxc_ast/src/ast_builder_impl.rs b/crates/oxc_ast/src/ast_builder_impl.rs index 6badb10c00811..dd290078388c2 100644 --- a/crates/oxc_ast/src/ast_builder_impl.rs +++ b/crates/oxc_ast/src/ast_builder_impl.rs @@ -84,7 +84,7 @@ impl<'a> AstBuilder<'a> { /// Allocate an [`Atom`] from a string slice. #[inline] pub fn atom(self, value: &str) -> Atom<'a> { - Atom::from(String::from_str_in(value, self.allocator).into_bump_str()) + Atom::from_in(value, self.allocator) } /// # SAFETY diff --git a/crates/oxc_span/src/atom.rs b/crates/oxc_span/src/atom.rs index 6f6af9cedabd6..d8e7a94329039 100644 --- a/crates/oxc_span/src/atom.rs +++ b/crates/oxc_span/src/atom.rs @@ -73,7 +73,7 @@ impl<'alloc> FromIn<'alloc, &Atom<'alloc>> for Atom<'alloc> { impl<'alloc> FromIn<'alloc, &str> for Atom<'alloc> { fn from_in(s: &str, allocator: &'alloc Allocator) -> Self { - Self::from(oxc_allocator::String::from_str_in(s, allocator).into_bump_str()) + Self::from(oxc_allocator::String::from_str_in(s, allocator)) } } @@ -101,6 +101,12 @@ impl<'a> From<&'a str> for Atom<'a> { } } +impl<'alloc> From> for Atom<'alloc> { + fn from(s: oxc_allocator::String<'alloc>) -> Self { + Self::from(s.into_bump_str()) + } +} + impl<'a> From> for &'a str { fn from(s: Atom<'a>) -> Self { s.as_str() diff --git a/crates/oxc_transformer/src/common/helper_loader.rs b/crates/oxc_transformer/src/common/helper_loader.rs index 2d50fb76c7f90..cefbe44f6a35e 100644 --- a/crates/oxc_transformer/src/common/helper_loader.rs +++ b/crates/oxc_transformer/src/common/helper_loader.rs @@ -298,7 +298,7 @@ impl<'a> HelperLoaderStore<'a> { source.push_str(&self.module_name); source.push_str("/helpers/"); source.push_str(helper_name); - Atom::from(source.into_bump_str()) + Atom::from(source) } fn transform_for_external_helper(helper: Helper, ctx: &mut TraverseCtx<'a>) -> Expression<'a> { diff --git a/crates/oxc_transformer/src/es2022/class_static_block.rs b/crates/oxc_transformer/src/es2022/class_static_block.rs index d4877584a9fd8..a3a8dc9a0da2d 100644 --- a/crates/oxc_transformer/src/es2022/class_static_block.rs +++ b/crates/oxc_transformer/src/es2022/class_static_block.rs @@ -269,7 +269,7 @@ impl<'a> Keys<'a> { let mut key = ArenaString::with_capacity_in(num_str.len() + 1, ctx.ast.allocator); key.push('_'); key.push_str(num_str); - let key = Atom::from(key.into_bump_str()); + let key = Atom::from(key); self.numbered.push(&key.as_str()[1..]);