@@ -179,6 +179,18 @@ macro_rules! arena_for_type {
179179 } ;
180180}
181181
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+
182194macro_rules! declare_arena {
183195 ( [ ] , [ $( $a: tt $name: ident: $ty: ty, ) * ] , $tcx: lifetime) => {
184196 #[ derive( Default ) ]
@@ -188,6 +200,24 @@ macro_rules! declare_arena {
188200 $( $name: arena_for_type!( $a[ $ty] ) , ) *
189201 }
190202
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+
191221 $(
192222 impl ArenaAllocatable for $ty { }
193223 unsafe impl <$tcx> ArenaField <$tcx> for $ty {
@@ -197,71 +227,41 @@ macro_rules! declare_arena {
197227 }
198228 }
199229 ) *
200- }
201- }
202-
203- macro_rules! which_arena_for_type {
204- ( [ ] [ $arena: expr] ) => {
205- Some ( $arena)
206- } ;
207- ( [ few$( , $attrs: ident) * ] [ $arena: expr] ) => {
208- None
209- } ;
210- ( [ $ignore: ident$( , $attrs: ident) * ] $args: tt) => {
211- which_arena_for_type!( [ $( $attrs) ,* ] $args)
212- } ;
213- }
214-
215- arena_types ! ( declare_arena, [ ] , ' tcx) ;
216-
217- #[ marker]
218- pub trait ArenaAllocatable { }
219-
220- impl < T : Copy > ArenaAllocatable for T { }
221230
222- unsafe trait ArenaField < ' tcx > : Sized {
223- /// Returns a specific arena to allocate from.
224- /// If `None` is returned, the `DropArena` will be used.
225- fn arena < ' a > ( arena : & ' a Arena < ' tcx > ) -> Option < & ' a TypedArena < Self > > ;
226- }
227-
228- unsafe impl < ' tcx , T > ArenaField < ' tcx > for T {
229- #[ inline]
230- default fn arena < ' a > ( _: & ' a Arena < ' tcx > ) -> Option < & ' a TypedArena < Self > > {
231- panic ! ( )
232- }
233- }
234-
235- impl < ' tcx > Arena < ' tcx > {
236- #[ inline]
237- pub fn alloc < T : ArenaAllocatable > ( & self , value : T ) -> & mut T {
238- if !mem:: needs_drop :: < T > ( ) {
239- return self . dropless . alloc ( value) ;
240- }
241- match <T as ArenaField < ' tcx > >:: arena ( self ) {
242- Some ( arena) => arena. alloc ( value) ,
243- None => unsafe { self . drop . alloc ( value) } ,
244- }
245- }
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+ }
246242
247- #[ inline]
248- pub fn alloc_slice < T : Copy > ( & self , value : & [ T ] ) -> & mut [ T ] {
249- if value. is_empty ( ) {
250- return & mut [ ] ;
251- }
252- self . dropless . alloc_slice ( value)
253- }
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+ }
254250
255- pub fn alloc_from_iter < T : ArenaAllocatable , I : IntoIterator < Item = T > > (
256- & ' a self ,
257- iter : I ,
258- ) -> & ' a mut [ T ] {
259- if !mem:: needs_drop :: < T > ( ) {
260- return self . dropless . alloc_from_iter ( iter) ;
261- }
262- match <T as ArenaField < ' tcx > >:: arena ( self ) {
263- Some ( arena) => arena. alloc_from_iter ( iter) ,
264- None => unsafe { self . drop . alloc_from_iter ( iter) } ,
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+ }
265263 }
266264 }
267265}
266+
267+ arena_types ! ( declare_arena, [ ] , ' tcx) ;
0 commit comments