Skip to content

Commit 4c28d68

Browse files
committed
move move stuff into declare_arena!
1 parent e60205f commit 4c28d68

File tree

2 files changed

+101
-102
lines changed

2 files changed

+101
-102
lines changed

src/libarena/lib.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,5 +569,105 @@ impl DropArena {
569569
}
570570
}
571571

572+
#[macro_export]
573+
macro_rules! arena_for_type {
574+
([][$ty:ty]) => {
575+
$crate::TypedArena<$ty>
576+
};
577+
([few $(, $attrs:ident)*][$ty:ty]) => {
578+
::std::marker::PhantomData<$ty>
579+
};
580+
([$ignore:ident $(, $attrs:ident)*]$args:tt) => {
581+
$crate::arena_for_type!([$($attrs),*]$args)
582+
};
583+
}
584+
585+
#[macro_export]
586+
macro_rules! which_arena_for_type {
587+
([][$arena:expr]) => {
588+
::std::option::Option::Some($arena)
589+
};
590+
([few$(, $attrs:ident)*][$arena:expr]) => {
591+
::std::option::Option::None
592+
};
593+
([$ignore:ident$(, $attrs:ident)*]$args:tt) => {
594+
$crate::which_arena_for_type!([$($attrs),*]$args)
595+
};
596+
}
597+
598+
#[macro_export]
599+
macro_rules! declare_arena {
600+
([], [$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) => {
601+
#[derive(Default)]
602+
pub struct Arena<$tcx> {
603+
pub dropless: $crate::DroplessArena,
604+
drop: $crate::DropArena,
605+
$($name: $crate::arena_for_type!($a[$ty]),)*
606+
}
607+
608+
#[marker]
609+
pub trait ArenaAllocatable {}
610+
611+
impl<T: Copy> ArenaAllocatable for T {}
612+
613+
unsafe trait ArenaField<'tcx>: Sized {
614+
/// Returns a specific arena to allocate from.
615+
/// If `None` is returned, the `DropArena` will be used.
616+
fn arena<'a>(arena: &'a Arena<'tcx>) -> Option<&'a $crate::TypedArena<Self>>;
617+
}
618+
619+
unsafe impl<'tcx, T> ArenaField<'tcx> for T {
620+
#[inline]
621+
default fn arena<'a>(_: &'a Arena<'tcx>) -> Option<&'a $crate::TypedArena<Self>> {
622+
panic!()
623+
}
624+
}
625+
626+
$(
627+
impl ArenaAllocatable for $ty {}
628+
unsafe impl<$tcx> ArenaField<$tcx> for $ty {
629+
#[inline]
630+
fn arena<'a>(_arena: &'a Arena<$tcx>) -> Option<&'a $crate::TypedArena<Self>> {
631+
$crate::which_arena_for_type!($a[&_arena.$name])
632+
}
633+
}
634+
)*
635+
636+
impl<'tcx> Arena<'tcx> {
637+
#[inline]
638+
pub fn alloc<T: ArenaAllocatable>(&self, value: T) -> &mut T {
639+
if !::std::mem::needs_drop::<T>() {
640+
return self.dropless.alloc(value);
641+
}
642+
match <T as ArenaField<'tcx>>::arena(self) {
643+
::std::option::Option::Some(arena) => arena.alloc(value),
644+
::std::option::Option::None => unsafe { self.drop.alloc(value) },
645+
}
646+
}
647+
648+
#[inline]
649+
pub fn alloc_slice<T: ::std::marker::Copy>(&self, value: &[T]) -> &mut [T] {
650+
if value.is_empty() {
651+
return &mut [];
652+
}
653+
self.dropless.alloc_slice(value)
654+
}
655+
656+
pub fn alloc_from_iter<T: ArenaAllocatable>(
657+
&'a self,
658+
iter: impl ::std::iter::IntoIterator<Item = T>,
659+
) -> &'a mut [T] {
660+
if !::std::mem::needs_drop::<T>() {
661+
return self.dropless.alloc_from_iter(iter);
662+
}
663+
match <T as ArenaField<'tcx>>::arena(self) {
664+
::std::option::Option::Some(arena) => arena.alloc_from_iter(iter),
665+
::std::option::Option::None => unsafe { self.drop.alloc_from_iter(iter) },
666+
}
667+
}
668+
}
669+
}
670+
}
671+
572672
#[cfg(test)]
573673
mod tests;

src/librustc/arena.rs

Lines changed: 1 addition & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
use arena::{DropArena, DroplessArena, TypedArena};
2-
use std::marker::PhantomData;
3-
use std::mem;
4-
51
/// This declares a list of types which can be allocated by `Arena`.
62
///
73
/// The `few` modifier will cause allocation to use the shared arena and recording the destructor.
@@ -167,101 +163,4 @@ macro_rules! arena_types {
167163
)
168164
}
169165

170-
macro_rules! arena_for_type {
171-
([][$ty:ty]) => {
172-
TypedArena<$ty>
173-
};
174-
([few $(, $attrs:ident)*][$ty:ty]) => {
175-
PhantomData<$ty>
176-
};
177-
([$ignore:ident $(, $attrs:ident)*]$args:tt) => {
178-
arena_for_type!([$($attrs),*]$args)
179-
};
180-
}
181-
182-
macro_rules! which_arena_for_type {
183-
([][$arena:expr]) => {
184-
Some($arena)
185-
};
186-
([few$(, $attrs:ident)*][$arena:expr]) => {
187-
None
188-
};
189-
([$ignore:ident$(, $attrs:ident)*]$args:tt) => {
190-
which_arena_for_type!([$($attrs),*]$args)
191-
};
192-
}
193-
194-
macro_rules! declare_arena {
195-
([], [$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) => {
196-
#[derive(Default)]
197-
pub struct Arena<$tcx> {
198-
pub dropless: DroplessArena,
199-
drop: DropArena,
200-
$($name: arena_for_type!($a[$ty]),)*
201-
}
202-
203-
#[marker]
204-
pub trait ArenaAllocatable {}
205-
206-
impl<T: Copy> ArenaAllocatable for T {}
207-
208-
unsafe trait ArenaField<'tcx>: Sized {
209-
/// Returns a specific arena to allocate from.
210-
/// If `None` is returned, the `DropArena` will be used.
211-
fn arena<'a>(arena: &'a Arena<'tcx>) -> Option<&'a TypedArena<Self>>;
212-
}
213-
214-
unsafe impl<'tcx, T> ArenaField<'tcx> for T {
215-
#[inline]
216-
default fn arena<'a>(_: &'a Arena<'tcx>) -> Option<&'a TypedArena<Self>> {
217-
panic!()
218-
}
219-
}
220-
221-
$(
222-
impl ArenaAllocatable for $ty {}
223-
unsafe impl<$tcx> ArenaField<$tcx> for $ty {
224-
#[inline]
225-
fn arena<'a>(_arena: &'a Arena<$tcx>) -> Option<&'a TypedArena<Self>> {
226-
which_arena_for_type!($a[&_arena.$name])
227-
}
228-
}
229-
)*
230-
231-
impl<'tcx> Arena<'tcx> {
232-
#[inline]
233-
pub fn alloc<T: ArenaAllocatable>(&self, value: T) -> &mut T {
234-
if !mem::needs_drop::<T>() {
235-
return self.dropless.alloc(value);
236-
}
237-
match <T as ArenaField<'tcx>>::arena(self) {
238-
Some(arena) => arena.alloc(value),
239-
None => unsafe { self.drop.alloc(value) },
240-
}
241-
}
242-
243-
#[inline]
244-
pub fn alloc_slice<T: Copy>(&self, value: &[T]) -> &mut [T] {
245-
if value.is_empty() {
246-
return &mut [];
247-
}
248-
self.dropless.alloc_slice(value)
249-
}
250-
251-
pub fn alloc_from_iter<T: ArenaAllocatable, I: IntoIterator<Item = T>>(
252-
&'a self,
253-
iter: I,
254-
) -> &'a mut [T] {
255-
if !mem::needs_drop::<T>() {
256-
return self.dropless.alloc_from_iter(iter);
257-
}
258-
match <T as ArenaField<'tcx>>::arena(self) {
259-
Some(arena) => arena.alloc_from_iter(iter),
260-
None => unsafe { self.drop.alloc_from_iter(iter) },
261-
}
262-
}
263-
}
264-
}
265-
}
266-
267-
arena_types!(declare_arena, [], 'tcx);
166+
arena_types!(arena::declare_arena, [], 'tcx);

0 commit comments

Comments
 (0)