move more logic into declare_arena!

This commit is contained in:
Mazdak Farrokhzad 2020-03-21 01:05:45 +01:00
parent 093bcf2e7a
commit e60205fe52

View File

@ -179,27 +179,6 @@ macro_rules! arena_for_type {
};
}
macro_rules! declare_arena {
([], [$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) => {
#[derive(Default)]
pub struct Arena<$tcx> {
pub dropless: DroplessArena,
drop: DropArena,
$($name: arena_for_type!($a[$ty]),)*
}
$(
impl ArenaAllocatable for $ty {}
unsafe impl<$tcx> ArenaField<$tcx> for $ty {
#[inline]
fn arena<'a>(_arena: &'a Arena<$tcx>) -> Option<&'a TypedArena<Self>> {
which_arena_for_type!($a[&_arena.$name])
}
}
)*
}
}
macro_rules! which_arena_for_type {
([][$arena:expr]) => {
Some($arena)
@ -212,27 +191,44 @@ macro_rules! which_arena_for_type {
};
}
arena_types!(declare_arena, [], 'tcx);
macro_rules! declare_arena {
([], [$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) => {
#[derive(Default)]
pub struct Arena<$tcx> {
pub dropless: DroplessArena,
drop: DropArena,
$($name: arena_for_type!($a[$ty]),)*
}
#[marker]
pub trait ArenaAllocatable {}
#[marker]
pub trait ArenaAllocatable {}
impl<T: Copy> ArenaAllocatable for T {}
impl<T: Copy> ArenaAllocatable for T {}
unsafe trait ArenaField<'tcx>: Sized {
unsafe trait ArenaField<'tcx>: Sized {
/// Returns a specific arena to allocate from.
/// If `None` is returned, the `DropArena` will be used.
fn arena<'a>(arena: &'a Arena<'tcx>) -> Option<&'a TypedArena<Self>>;
}
}
unsafe impl<'tcx, T> ArenaField<'tcx> for T {
unsafe impl<'tcx, T> ArenaField<'tcx> for T {
#[inline]
default fn arena<'a>(_: &'a Arena<'tcx>) -> Option<&'a TypedArena<Self>> {
panic!()
}
}
}
impl<'tcx> Arena<'tcx> {
$(
impl ArenaAllocatable for $ty {}
unsafe impl<$tcx> ArenaField<$tcx> for $ty {
#[inline]
fn arena<'a>(_arena: &'a Arena<$tcx>) -> Option<&'a TypedArena<Self>> {
which_arena_for_type!($a[&_arena.$name])
}
}
)*
impl<'tcx> Arena<'tcx> {
#[inline]
pub fn alloc<T: ArenaAllocatable>(&self, value: T) -> &mut T {
if !mem::needs_drop::<T>() {
@ -264,4 +260,8 @@ impl<'tcx> Arena<'tcx> {
None => unsafe { self.drop.alloc_from_iter(iter) },
}
}
}
}
}
arena_types!(declare_arena, [], 'tcx);