-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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): Initialize package #9195
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
authors = ["강동윤 <[email protected]>"] | ||
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should probably reference our code :-(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I depended on
oxc_allocator
but the problem was the lack of trait implementations ofBox
.I prefer depending on
oxc_allocator,
but I created a new package instead of requesting implementing them mainly because ofrkyv
. Traits likePartialEq
may be also useful foroxc
, but AFAIK,oxc
has nothing to do withrkyv
. If you are fine with adding some derives forrkyv
, I'll useoxc_alllocator
instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to have your own abstraction. I was saying you should put a comment saying where the code originated from.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops. I added it with be99ce0