-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Introduce an arena type which may be used to allocate a list of types with destructors #59536
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
Changes from 1 commit
002c70f
e835d27
4ccb9ae
43e33ea
04762dd
223f1c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
… with destructors
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use arena::{TypedArena, DroplessArena}; | ||
|
||
#[macro_export] | ||
macro_rules! arena_types { | ||
($macro:path, $args:tt, $tcx:lifetime) => ( | ||
$macro!($args, [ | ||
[] vtable_method: Option<( | ||
rustc::hir::def_id::DefId, | ||
rustc::ty::subst::SubstsRef<$tcx> | ||
)>, | ||
[decode] specialization_graph: rustc::traits::specialization_graph::Graph, | ||
], $tcx); | ||
) | ||
} | ||
|
||
macro_rules! declare_arena { | ||
([], [$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) => { | ||
#[derive(Default)] | ||
pub struct Arena<$tcx> { | ||
dropless: DroplessArena, | ||
$($name: TypedArena<$ty>,)* | ||
} | ||
} | ||
} | ||
|
||
macro_rules! impl_specialized_decodable { | ||
([decode] $ty:ty, $tcx:lifetime) => { | ||
impl<$tcx> serialize::UseSpecializedDecodable for &$tcx $ty {} | ||
}; | ||
([] $ty:ty, $tcx:lifetime) => {}; | ||
} | ||
|
||
macro_rules! impl_arena_allocatable { | ||
([], [$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) => { | ||
$( | ||
impl_specialized_decodable!($a $ty, $tcx); | ||
|
||
impl<$tcx> ArenaAllocatable<$tcx> for $ty { | ||
#[inline] | ||
fn arena<'a>(arena: &'a Arena<$tcx>) -> Option<&'a TypedArena<Self>> { | ||
Some(&arena.$name) | ||
} | ||
} | ||
)* | ||
} | ||
} | ||
|
||
arena_types!(declare_arena, [], 'tcx); | ||
|
||
arena_types!(impl_arena_allocatable, [], 'tcx); | ||
|
||
pub trait ArenaAllocatable<'tcx>: Sized { | ||
/// Returns a specific arena to allocate from if the type requires destructors. | ||
/// Otherwise it will return `None` to be allocated from the dropless arena. | ||
fn arena<'a>(arena: &'a Arena<'tcx>) -> Option<&'a TypedArena<Self>>; | ||
} | ||
|
||
impl<'tcx, T: Copy> ArenaAllocatable<'tcx> for T { | ||
#[inline] | ||
default fn arena<'a>(_: &'a Arena<'tcx>) -> Option<&'a TypedArena<Self>> { | ||
None | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This impl can conflict with the types in the list :(
Do we have some way to express an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh wow, it would be nice to be able to rely on sets and maps never being There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found a way around this using a marker trait. |
||
} | ||
|
||
impl<'tcx> Arena<'tcx> { | ||
#[inline] | ||
pub fn alloc<T: ArenaAllocatable<'tcx>>(&self, value: T) -> &mut T { | ||
match T::arena(self) { | ||
Some(arena) => { | ||
arena.alloc(value) | ||
} | ||
None => { | ||
self.dropless.alloc(value) | ||
} | ||
} | ||
} | ||
|
||
pub fn alloc_from_iter< | ||
T: ArenaAllocatable<'tcx>, | ||
I: IntoIterator<Item = T> | ||
>( | ||
&self, | ||
iter: I | ||
) -> &mut [T] { | ||
match T::arena(self) { | ||
Some(arena) => { | ||
arena.alloc_from_iter(iter) | ||
} | ||
None => { | ||
self.dropless.alloc_from_iter(iter) | ||
} | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
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.
The list of types are declared here with a macro.