Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(allocator): introduce FromIn and IntoIn traits. #4088

Merged
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
66 changes: 66 additions & 0 deletions crates/oxc_allocator/src/convert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#![allow(clippy::inline_always)]

use crate::{Allocator, Box};

/// This trait works similarly to the standard library `From` trait, It comes with a similar
/// 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;
}

/// 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;
}

/// `FromIn` is reflective
impl<'a, T> FromIn<'a, T> for T {
#[inline(always)]
fn from_in(t: T, _: &'a Allocator) -> T {
t
}
}

/// `FromIn` implicitly implements `IntoIn`.
impl<'a, T, U> IntoIn<'a, U> for T
where
U: FromIn<'a, T>,
{
#[inline]
fn into_in(self, alloc: &'a Allocator) -> U {
U::from_in(self, alloc)
}
}

// ---------------- 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)
}
}

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()
}
}

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)
}
}

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))
}
}
5 changes: 4 additions & 1 deletion crates/oxc_allocator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ use std::{
};

mod arena;
mod convert;

pub use arena::{Box, String, Vec};
use bumpalo::Bump;

pub use arena::{Box, String, Vec};
pub use convert::{FromIn, IntoIn};

#[derive(Default)]
pub struct Allocator {
bump: Bump,
Expand Down
Loading