Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions crates/oxc_allocator/src/clone_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
/// }
/// }
/// ```
Expand All @@ -22,16 +22,16 @@ 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>
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))
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down
24 changes: 12 additions & 12 deletions crates/oxc_allocator/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,37 +30,37 @@ 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)
}
}

// ---------------- Primitive allocations ----------------

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))
}
}
4 changes: 2 additions & 2 deletions crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
))
}
}
Expand Down
Loading