Skip to content

Commit

Permalink
feat: add TraversableAstBuilder type and GAlloc trait. [no ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Apr 15, 2024
1 parent df95968 commit 0a8106f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
12 changes: 12 additions & 0 deletions crates/oxc_ast/src/traverse/ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use oxc_allocator::Allocator;

/// AST builder for creating AST nodes for traversable AST
pub struct TraversableAstBuilder<'a> {
pub allocator: &'a Allocator,
}

impl<'a> TraversableAstBuilder<'a> {
pub fn new(allocator: &'a Allocator) -> Self {
Self { allocator }
}
}
19 changes: 16 additions & 3 deletions crates/oxc_ast/src/traverse/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use std::cell::UnsafeCell;

use oxc_allocator::Allocator;

/// Access token for traversing AST.
#[repr(transparent)]
pub struct Token(());
Expand Down Expand Up @@ -53,9 +55,7 @@ pub struct GCell<T: ?Sized> {
#[allow(dead_code)]
impl<T> GCell<T> {
pub const fn new(value: T) -> Self {
GCell {
value: UnsafeCell::new(value),
}
GCell { value: UnsafeCell::new(value) }
}

pub fn into_inner(self) -> T {
Expand Down Expand Up @@ -154,3 +154,16 @@ pub type SharedBox<'a, T> = &'a GCell<T>;
/// Type alias for a shared Vec
pub type SharedVec<'a, T> = oxc_allocator::Vec<'a, GCell<T>>;

/// Trait to sugar `GCell::from_mut(allocator.alloc(t))` to `allocator.galloc(t)`.
trait GCellAlloc {
#[allow(clippy::mut_from_ref)]
fn galloc<T>(&self, value: T) -> &mut GCell<T>;
}

impl GCellAlloc for Allocator {
/// Allocate `T` into arena and return a `&mut GCell` to it
#[inline]
fn galloc<T>(&self, value: T) -> &mut GCell<T> {
GCell::from_mut(self.alloc(value))
}
}
1 change: 1 addition & 0 deletions crates/oxc_ast/src/traverse/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod ast;
mod cell;
mod orphan;

Expand Down

0 comments on commit 0a8106f

Please sign in to comment.