Skip to content

Commit

Permalink
feat(span): add impl From<ArenaString> for Atom (#7973)
Browse files Browse the repository at this point in the history
Add conversion method from `oxc_allocator::String` to `Atom`. This is a zero-cost conversion, because the string is already in the arena.
  • Loading branch information
overlookmotel committed Dec 18, 2024
1 parent ff9d1b3 commit c30a982
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/ast_builder_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion crates/oxc_span/src/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -101,6 +101,12 @@ impl<'a> From<&'a str> for Atom<'a> {
}
}

impl<'alloc> From<oxc_allocator::String<'alloc>> for Atom<'alloc> {
fn from(s: oxc_allocator::String<'alloc>) -> Self {
Self::from(s.into_bump_str())
}
}

impl<'a> From<Atom<'a>> for &'a str {
fn from(s: Atom<'a>) -> Self {
s.as_str()
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/common/helper_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/es2022/class_static_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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..]);

Expand Down

0 comments on commit c30a982

Please sign in to comment.