Skip to content

Commit

Permalink
feat(allocator): Initialize package (#9195)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Jul 10, 2024
1 parent 634db77 commit f3681cb
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 2 deletions.
11 changes: 9 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = [
"xtask",
"crates/swc_allocator",
"crates/swc_core",
"crates/swc_cli_impl",
"crates/dbg-swc",
Expand Down Expand Up @@ -51,6 +52,7 @@ resolver = "2"
base64 = "0.21.0"
bitflags = "2.5.0"
browserslist-rs = "0.16.0"
bumpalo = "3.16.0"
cfg-if = "1.0.0"
chrono = "0.4.38"
codspeed-criterion-compat = "2.6.0"
Expand Down
12 changes: 12 additions & 0 deletions crates/swc_allocator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "A thin wrapper for bumpalo"
documentation = "https://rustdoc.swc.rs/swc_allocator/"
edition = "2021"
license = "Apache-2.0"
name = "swc_allocator"
repository = { workspace = true }
version = "0.1.0"

[dependencies]
bumpalo = { workspace = true, features = ["boxed", "collections"] }
140 changes: 140 additions & 0 deletions crates/swc_allocator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
use std::ops::{Deref, DerefMut};

use bumpalo::Bump;

#[derive(Default)]
pub struct Allocator {
alloc: Bump,
}

impl From<Bump> for Allocator {
fn from(alloc: Bump) -> Self {
Self { alloc }
}
}

impl Deref for Allocator {
type Target = Bump;

fn deref(&self) -> &Bump {
&self.alloc
}
}

impl DerefMut for Allocator {
fn deref_mut(&mut self) -> &mut Bump {
&mut self.alloc
}
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct Box<'alloc, T>(bumpalo::boxed::Box<'alloc, T>);

impl<'alloc, T> Box<'alloc, T> {
#[inline(always)]
pub fn new(alloc: &'alloc Allocator, value: T) -> Self {
Self(bumpalo::boxed::Box::new_in(value, alloc))
}
}

impl<'alloc, T> Deref for Box<'alloc, T> {
type Target = T;

fn deref(&self) -> &T {
&self.0
}
}

impl<'alloc, T> DerefMut for Box<'alloc, T> {
fn deref_mut(&mut self) -> &mut T {
&mut self.0
}
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct Vec<'alloc, T>(bumpalo::collections::Vec<'alloc, T>);

impl<'alloc, T> Vec<'alloc, T> {
#[inline(always)]
pub fn new(alloc: &'alloc Allocator) -> Self {
Self(bumpalo::collections::Vec::new_in(alloc))
}

#[inline(always)]
pub fn with_capacity(alloc: &'alloc Allocator, capacity: usize) -> Self {
Self(bumpalo::collections::Vec::with_capacity_in(capacity, alloc))
}
}

impl<'alloc, T> Deref for Vec<'alloc, T> {
type Target = [T];

fn deref(&self) -> &[T] {
&self.0
}
}

impl<'alloc, T> DerefMut for Vec<'alloc, T> {
fn deref_mut(&mut self) -> &mut [T] {
&mut self.0
}
}

impl<'alloc, T> IntoIterator for Vec<'alloc, T> {
type IntoIter = bumpalo::collections::vec::IntoIter<'alloc, T>;
type Item = T;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct String<'alloc>(bumpalo::collections::String<'alloc>);

impl<'alloc> String<'alloc> {
#[inline(always)]
pub fn new(alloc: &'alloc Allocator) -> Self {
Self(bumpalo::collections::String::new_in(alloc))
}

#[inline(always)]
pub fn with_capacity(alloc: &'alloc Allocator, capacity: usize) -> Self {
Self(bumpalo::collections::String::with_capacity_in(
capacity, alloc,
))
}
}

impl Deref for String<'_> {
type Target = str;

fn deref(&self) -> &str {
&self.0
}
}

impl DerefMut for String<'_> {
fn deref_mut(&mut self) -> &mut str {
&mut self.0
}
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum CowStr<'alloc> {
Borrowed(&'alloc str),
Owned(String<'alloc>),
}

impl Deref for CowStr<'_> {
type Target = str;

fn deref(&self) -> &str {
match self {
CowStr::Borrowed(s) => s,
CowStr::Owned(s) => s,
}
}
}

0 comments on commit f3681cb

Please sign in to comment.