mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
refactor(allocator, ast, span, ast_tools): use allocator as var name for Allocator (#4900)
We mostly use `allocator` as var name for an `Allocator`, but in some places used the shorter name `alloc`. Use `allocator` everywhere for consistency.
This commit is contained in:
parent
0d7912217a
commit
90d0b2ba65
6 changed files with 1611 additions and 1446 deletions
|
|
@ -10,8 +10,8 @@ use crate::{Allocator, Box, Vec};
|
|||
/// ```
|
||||
/// impl<'old_alloc, 'new_alloc> CloneIn<'new_alloc> for Struct<'old_alloc> {
|
||||
/// type Cloned = Struct<'new_alloc>;
|
||||
/// fn clone_in(&self, alloc: &'new_alloc Allocator) -> Self::Cloned {
|
||||
/// Struct { a: self.a.clone_in(alloc), b: self.b.clone_in(alloc) }
|
||||
/// fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
|
||||
/// Struct { a: self.a.clone_in(allocator), b: self.b.clone_in(allocator) }
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
|
|
@ -22,7 +22,7 @@ use crate::{Allocator, Box, Vec};
|
|||
pub trait CloneIn<'new_alloc>: Sized {
|
||||
type Cloned;
|
||||
|
||||
fn clone_in(&self, alloc: &'new_alloc Allocator) -> Self::Cloned;
|
||||
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned;
|
||||
}
|
||||
|
||||
impl<'alloc, T, C> CloneIn<'alloc> for Option<T>
|
||||
|
|
@ -30,8 +30,8 @@ where
|
|||
T: CloneIn<'alloc, Cloned = C>,
|
||||
{
|
||||
type Cloned = Option<C>;
|
||||
fn clone_in(&self, alloc: &'alloc Allocator) -> Self::Cloned {
|
||||
self.as_ref().map(|it| it.clone_in(alloc))
|
||||
fn clone_in(&self, allocator: &'alloc Allocator) -> Self::Cloned {
|
||||
self.as_ref().map(|it| it.clone_in(allocator))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -40,8 +40,8 @@ where
|
|||
T: CloneIn<'new_alloc, Cloned = C>,
|
||||
{
|
||||
type Cloned = Box<'new_alloc, C>;
|
||||
fn clone_in(&self, alloc: &'new_alloc Allocator) -> Self::Cloned {
|
||||
Box::new_in(self.as_ref().clone_in(alloc), alloc)
|
||||
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
|
||||
Box::new_in(self.as_ref().clone_in(allocator), allocator)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -50,8 +50,8 @@ where
|
|||
T: CloneIn<'new_alloc, Cloned = C>,
|
||||
{
|
||||
type Cloned = Vec<'new_alloc, C>;
|
||||
fn clone_in(&self, alloc: &'new_alloc Allocator) -> Self::Cloned {
|
||||
Vec::from_iter_in(self.iter().map(|it| it.clone_in(alloc)), alloc)
|
||||
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
|
||||
Vec::from_iter_in(self.iter().map(|it| it.clone_in(allocator)), allocator)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -64,8 +64,8 @@ impl<'alloc, T: Copy> CloneIn<'alloc> for Cell<T> {
|
|||
|
||||
impl<'old_alloc, 'new_alloc> CloneIn<'new_alloc> for &'old_alloc str {
|
||||
type Cloned = &'new_alloc str;
|
||||
fn clone_in(&self, alloc: &'new_alloc Allocator) -> Self::Cloned {
|
||||
alloc.alloc_str(self)
|
||||
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
|
||||
allocator.alloc_str(self)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,14 +6,14 @@ use crate::{Allocator, Box};
|
|||
/// implementation containing blanket implementation for `IntoIn`, reflective implementation and a
|
||||
/// bunch of primitive conversions from Rust types to their arena equivalent.
|
||||
pub trait FromIn<'a, T>: Sized {
|
||||
fn from_in(value: T, alloc: &'a Allocator) -> Self;
|
||||
fn from_in(value: T, allocator: &'a Allocator) -> Self;
|
||||
}
|
||||
|
||||
/// This trait works similarly to the standard library `Into` trait.
|
||||
/// It is similar to `FromIn` is reflective, A `FromIn` implementation also implicitly implements
|
||||
/// `IntoIn` for the opposite type.
|
||||
pub trait IntoIn<'a, T>: Sized {
|
||||
fn into_in(self, alloc: &'a Allocator) -> T;
|
||||
fn into_in(self, allocator: &'a Allocator) -> T;
|
||||
}
|
||||
|
||||
/// `FromIn` is reflective
|
||||
|
|
@ -30,8 +30,8 @@ where
|
|||
U: FromIn<'a, T>,
|
||||
{
|
||||
#[inline]
|
||||
fn into_in(self, alloc: &'a Allocator) -> U {
|
||||
U::from_in(self, alloc)
|
||||
fn into_in(self, allocator: &'a Allocator) -> U {
|
||||
U::from_in(self, allocator)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -39,28 +39,28 @@ where
|
|||
|
||||
impl<'a> FromIn<'a, String> for crate::String<'a> {
|
||||
#[inline(always)]
|
||||
fn from_in(value: String, alloc: &'a Allocator) -> Self {
|
||||
crate::String::from_str_in(value.as_str(), alloc)
|
||||
fn from_in(value: String, allocator: &'a Allocator) -> Self {
|
||||
crate::String::from_str_in(value.as_str(), allocator)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> FromIn<'a, String> for &'a str {
|
||||
#[inline(always)]
|
||||
fn from_in(value: String, alloc: &'a Allocator) -> Self {
|
||||
crate::String::from_str_in(value.as_str(), alloc).into_bump_str()
|
||||
fn from_in(value: String, allocator: &'a Allocator) -> Self {
|
||||
crate::String::from_str_in(value.as_str(), allocator).into_bump_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> FromIn<'a, T> for Box<'a, T> {
|
||||
#[inline(always)]
|
||||
fn from_in(value: T, alloc: &'a Allocator) -> Self {
|
||||
Box::new_in(value, alloc)
|
||||
fn from_in(value: T, allocator: &'a Allocator) -> Self {
|
||||
Box::new_in(value, allocator)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> FromIn<'a, Option<T>> for Option<Box<'a, T>> {
|
||||
#[inline(always)]
|
||||
fn from_in(value: Option<T>, alloc: &'a Allocator) -> Self {
|
||||
value.map(|it| Box::new_in(it, alloc))
|
||||
fn from_in(value: Option<T>, allocator: &'a Allocator) -> Self {
|
||||
value.map(|it| Box::new_in(it, allocator))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -702,10 +702,10 @@ impl<'a> Statement<'a> {
|
|||
}
|
||||
|
||||
impl<'a> FromIn<'a, Expression<'a>> for Statement<'a> {
|
||||
fn from_in(expression: Expression<'a>, alloc: &'a oxc_allocator::Allocator) -> Self {
|
||||
fn from_in(expression: Expression<'a>, allocator: &'a oxc_allocator::Allocator) -> Self {
|
||||
Statement::ExpressionStatement(Box::from_in(
|
||||
ExpressionStatement { span: expression.span(), expression },
|
||||
alloc,
|
||||
allocator,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -62,8 +62,8 @@ impl<'a> Atom<'a> {
|
|||
impl<'old_alloc, 'new_alloc> CloneIn<'new_alloc> for Atom<'old_alloc> {
|
||||
type Cloned = Atom<'new_alloc>;
|
||||
|
||||
fn clone_in(&self, alloc: &'new_alloc Allocator) -> Self::Cloned {
|
||||
Atom::from_in(self.as_str(), alloc)
|
||||
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
|
||||
Atom::from_in(self.as_str(), allocator)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -74,26 +74,26 @@ impl<'a, 'b> FromIn<'a, &'b Atom<'a>> for Atom<'a> {
|
|||
}
|
||||
|
||||
impl<'a, 'b> FromIn<'a, &'b str> for Atom<'a> {
|
||||
fn from_in(s: &'b str, alloc: &'a Allocator) -> Self {
|
||||
Self::from(oxc_allocator::String::from_str_in(s, alloc).into_bump_str())
|
||||
fn from_in(s: &'b str, allocator: &'a Allocator) -> Self {
|
||||
Self::from(oxc_allocator::String::from_str_in(s, allocator).into_bump_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> FromIn<'a, String> for Atom<'a> {
|
||||
fn from_in(s: String, alloc: &'a Allocator) -> Self {
|
||||
Self::from_in(s.as_str(), alloc)
|
||||
fn from_in(s: String, allocator: &'a Allocator) -> Self {
|
||||
Self::from_in(s.as_str(), allocator)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> FromIn<'a, &String> for Atom<'a> {
|
||||
fn from_in(s: &String, alloc: &'a Allocator) -> Self {
|
||||
Self::from_in(s.as_str(), alloc)
|
||||
fn from_in(s: &String, allocator: &'a Allocator) -> Self {
|
||||
Self::from_in(s.as_str(), allocator)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b> FromIn<'a, Cow<'b, str>> for Atom<'a> {
|
||||
fn from_in(s: Cow<'b, str>, alloc: &'a Allocator) -> Self {
|
||||
Self::from_in(&*s, alloc)
|
||||
fn from_in(s: Cow<'b, str>, allocator: &'a Allocator) -> Self {
|
||||
Self::from_in(&*s, allocator)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -64,12 +64,12 @@ fn derive_enum(def: &EnumDef) -> TokenStream {
|
|||
quote!(Self :: #ident => #ty_ident :: #ident)
|
||||
} else {
|
||||
used_alloc = true;
|
||||
quote!(Self :: #ident(it) => #ty_ident :: #ident(it.clone_in(alloc)))
|
||||
quote!(Self :: #ident(it) => #ty_ident :: #ident(it.clone_in(allocator)))
|
||||
}
|
||||
})
|
||||
.collect_vec();
|
||||
|
||||
let alloc_ident = if used_alloc { format_ident!("alloc") } else { format_ident!("_") };
|
||||
let alloc_ident = if used_alloc { format_ident!("allocator") } else { format_ident!("_") };
|
||||
let body = quote! {
|
||||
match self {
|
||||
#(#matches),*
|
||||
|
|
@ -89,10 +89,10 @@ fn derive_struct(def: &StructDef) -> TokenStream {
|
|||
let ident = field.ident();
|
||||
match field.markers.derive_attributes.clone_in {
|
||||
CloneInAttribute::Default => quote!(#ident: Default::default()),
|
||||
CloneInAttribute::None => quote!(#ident: self.#ident.clone_in(alloc)),
|
||||
CloneInAttribute::None => quote!(#ident: self.#ident.clone_in(allocator)),
|
||||
}
|
||||
});
|
||||
(format_ident!("alloc"), quote!(#ty_ident { #(#fields),* }))
|
||||
(format_ident!("allocator"), quote!(#ty_ident { #(#fields),* }))
|
||||
};
|
||||
|
||||
impl_clone_in(&ty_ident, def.has_lifetime, &alloc_ident, &body)
|
||||
|
|
|
|||
Loading…
Reference in a new issue