]> git.lizzy.rs Git - rust.git/commitdiff
move more logic into declare_arena!
authorMazdak Farrokhzad <twingoow@gmail.com>
Sat, 21 Mar 2020 00:05:45 +0000 (01:05 +0100)
committerMazdak Farrokhzad <twingoow@gmail.com>
Sat, 21 Mar 2020 21:18:57 +0000 (22:18 +0100)
src/librustc/arena.rs

index 897a587fce8a0357ed9019d8f3596293e8976814..6805742fe44f9ec269fd155f59b3fd5837379ad8 100644 (file)
@@ -179,6 +179,18 @@ macro_rules! arena_for_type {
     };
 }
 
+macro_rules! which_arena_for_type {
+    ([][$arena:expr]) => {
+        Some($arena)
+    };
+    ([few$(, $attrs:ident)*][$arena:expr]) => {
+        None
+    };
+    ([$ignore:ident$(, $attrs:ident)*]$args:tt) => {
+        which_arena_for_type!([$($attrs),*]$args)
+    };
+}
+
 macro_rules! declare_arena {
     ([], [$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) => {
         #[derive(Default)]
@@ -188,6 +200,24 @@ pub struct Arena<$tcx> {
             $($name: arena_for_type!($a[$ty]),)*
         }
 
+        #[marker]
+        pub trait ArenaAllocatable {}
+
+        impl<T: Copy> ArenaAllocatable for T {}
+
+        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 {
+            #[inline]
+            default fn arena<'a>(_: &'a Arena<'tcx>) -> Option<&'a TypedArena<Self>> {
+                panic!()
+            }
+        }
+
         $(
             impl ArenaAllocatable for $ty {}
             unsafe impl<$tcx> ArenaField<$tcx> for $ty {
@@ -197,71 +227,41 @@ fn arena<'a>(_arena: &'a Arena<$tcx>) -> Option<&'a TypedArena<Self>> {
                 }
             }
         )*
-    }
-}
-
-macro_rules! which_arena_for_type {
-    ([][$arena:expr]) => {
-        Some($arena)
-    };
-    ([few$(, $attrs:ident)*][$arena:expr]) => {
-        None
-    };
-    ([$ignore:ident$(, $attrs:ident)*]$args:tt) => {
-        which_arena_for_type!([$($attrs),*]$args)
-    };
-}
-
-arena_types!(declare_arena, [], 'tcx);
-
-#[marker]
-pub trait ArenaAllocatable {}
-
-impl<T: Copy> ArenaAllocatable for T {}
 
-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 {
-    #[inline]
-    default fn arena<'a>(_: &'a Arena<'tcx>) -> Option<&'a TypedArena<Self>> {
-        panic!()
-    }
-}
-
-impl<'tcx> Arena<'tcx> {
-    #[inline]
-    pub fn alloc<T: ArenaAllocatable>(&self, value: T) -> &mut T {
-        if !mem::needs_drop::<T>() {
-            return self.dropless.alloc(value);
-        }
-        match <T as ArenaField<'tcx>>::arena(self) {
-            Some(arena) => arena.alloc(value),
-            None => unsafe { self.drop.alloc(value) },
-        }
-    }
+        impl<'tcx> Arena<'tcx> {
+            #[inline]
+            pub fn alloc<T: ArenaAllocatable>(&self, value: T) -> &mut T {
+                if !mem::needs_drop::<T>() {
+                    return self.dropless.alloc(value);
+                }
+                match <T as ArenaField<'tcx>>::arena(self) {
+                    Some(arena) => arena.alloc(value),
+                    None => unsafe { self.drop.alloc(value) },
+                }
+            }
 
-    #[inline]
-    pub fn alloc_slice<T: Copy>(&self, value: &[T]) -> &mut [T] {
-        if value.is_empty() {
-            return &mut [];
-        }
-        self.dropless.alloc_slice(value)
-    }
+            #[inline]
+            pub fn alloc_slice<T: Copy>(&self, value: &[T]) -> &mut [T] {
+                if value.is_empty() {
+                    return &mut [];
+                }
+                self.dropless.alloc_slice(value)
+            }
 
-    pub fn alloc_from_iter<T: ArenaAllocatable, I: IntoIterator<Item = T>>(
-        &'a self,
-        iter: I,
-    ) -> &'a mut [T] {
-        if !mem::needs_drop::<T>() {
-            return self.dropless.alloc_from_iter(iter);
-        }
-        match <T as ArenaField<'tcx>>::arena(self) {
-            Some(arena) => arena.alloc_from_iter(iter),
-            None => unsafe { self.drop.alloc_from_iter(iter) },
+            pub fn alloc_from_iter<T: ArenaAllocatable, I: IntoIterator<Item = T>>(
+                &'a self,
+                iter: I,
+            ) -> &'a mut [T] {
+                if !mem::needs_drop::<T>() {
+                    return self.dropless.alloc_from_iter(iter);
+                }
+                match <T as ArenaField<'tcx>>::arena(self) {
+                    Some(arena) => arena.alloc_from_iter(iter),
+                    None => unsafe { self.drop.alloc_from_iter(iter) },
+                }
+            }
         }
     }
 }
+
+arena_types!(declare_arena, [], 'tcx);