|
1 | | -use arena::{DropArena, DroplessArena, TypedArena}; |
2 | | -use std::marker::PhantomData; |
3 | | -use std::mem; |
4 | | - |
5 | 1 | /// This declares a list of types which can be allocated by `Arena`. |
6 | 2 | /// |
7 | 3 | /// The `few` modifier will cause allocation to use the shared arena and recording the destructor. |
@@ -167,101 +163,4 @@ macro_rules! arena_types { |
167 | 163 | ) |
168 | 164 | } |
169 | 165 |
|
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