about summary refs log tree commit diff
path: root/src/librustc_data_structures/macros.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/librustc_data_structures/macros.rs')
-rw-r--r--src/librustc_data_structures/macros.rs38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/librustc_data_structures/macros.rs b/src/librustc_data_structures/macros.rs
index 83e6dbedee2..67fbe3058cd 100644
--- a/src/librustc_data_structures/macros.rs
+++ b/src/librustc_data_structures/macros.rs
@@ -17,3 +17,41 @@ macro_rules! static_assert_size {
         const _: [(); $size] = [(); ::std::mem::size_of::<$ty>()];
     };
 }
+
+#[macro_export]
+macro_rules! enum_from_u32 {
+    ($(#[$attr:meta])* pub enum $name:ident {
+        $($variant:ident = $e:expr,)*
+    }) => {
+        $(#[$attr])*
+        pub enum $name {
+            $($variant = $e),*
+        }
+
+        impl $name {
+            pub fn from_u32(u: u32) -> Option<$name> {
+                $(if u == $name::$variant as u32 {
+                    return Some($name::$variant)
+                })*
+                None
+            }
+        }
+    };
+    ($(#[$attr:meta])* pub enum $name:ident {
+        $($variant:ident,)*
+    }) => {
+        $(#[$attr])*
+        pub enum $name {
+            $($variant,)*
+        }
+
+        impl $name {
+            pub fn from_u32(u: u32) -> Option<$name> {
+                $(if u == $name::$variant as u32 {
+                    return Some($name::$variant)
+                })*
+                None
+            }
+        }
+    }
+}