-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to use custom arena with better types than id-arena
- Loading branch information
1 parent
891c3e6
commit 090e716
Showing
7 changed files
with
161 additions
and
98 deletions.
There are no files selected for viewing
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
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,48 @@ | ||
use std::{marker::PhantomData, ops::Index}; | ||
|
||
#[derive(Debug)] | ||
pub struct Arena<Id: IdLike, T> { | ||
inner: Vec<T>, | ||
_phantom: PhantomData<Id>, | ||
} | ||
|
||
impl<Id: IdLike, T> Arena<Id, T> { | ||
pub fn new() -> Self { | ||
Self { | ||
inner: vec![], | ||
_phantom: PhantomData, | ||
} | ||
} | ||
|
||
pub fn len(&self) -> usize { | ||
self.inner.len() | ||
} | ||
|
||
pub fn is_empty(&self) -> bool { | ||
self.inner.is_empty() | ||
} | ||
|
||
pub fn push(&mut self, x: T) -> Id { | ||
self.inner.push(x); | ||
Id::from_raw(self.inner.len() - 1) | ||
} | ||
} | ||
|
||
impl<Id: IdLike, T> Default for Arena<Id, T> { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl<Id: IdLike, T> Index<Id> for Arena<Id, T> { | ||
type Output = T; | ||
|
||
fn index(&self, index: Id) -> &Self::Output { | ||
&self.inner[index.into_raw()] | ||
} | ||
} | ||
|
||
pub trait IdLike { | ||
fn from_raw(index: usize) -> Self; | ||
fn into_raw(self) -> usize; | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod arena; | ||
pub mod ast; | ||
pub mod ctxt; | ||
pub mod diagnostic; | ||
|
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
Oops, something went wrong.