]> git.lizzy.rs Git - rust.git/blob - src/librustc_data_structures/macros.rs
Rollup merge of #69038 - yaahc:backtrace-debug, r=dtolnay
[rust.git] / src / librustc_data_structures / macros.rs
1 /// A simple static assertion macro.
2 #[macro_export]
3 #[allow_internal_unstable(type_ascription)]
4 macro_rules! static_assert {
5     ($test:expr) => {
6         // Use the bool to access an array such that if the bool is false, the access
7         // is out-of-bounds.
8         #[allow(dead_code)]
9         const _: () = [()][!($test: bool) as usize];
10     };
11 }
12
13 /// Type size assertion. The first argument is a type and the second argument is its expected size.
14 #[macro_export]
15 macro_rules! static_assert_size {
16     ($ty:ty, $size:expr) => {
17         const _: [(); $size] = [(); ::std::mem::size_of::<$ty>()];
18     };
19 }
20
21 #[macro_export]
22 macro_rules! enum_from_u32 {
23     ($(#[$attr:meta])* pub enum $name:ident {
24         $($variant:ident = $e:expr,)*
25     }) => {
26         $(#[$attr])*
27         pub enum $name {
28             $($variant = $e),*
29         }
30
31         impl $name {
32             pub fn from_u32(u: u32) -> Option<$name> {
33                 $(if u == $name::$variant as u32 {
34                     return Some($name::$variant)
35                 })*
36                 None
37             }
38         }
39     };
40     ($(#[$attr:meta])* pub enum $name:ident {
41         $($variant:ident,)*
42     }) => {
43         $(#[$attr])*
44         pub enum $name {
45             $($variant,)*
46         }
47
48         impl $name {
49             pub fn from_u32(u: u32) -> Option<$name> {
50                 $(if u == $name::$variant as u32 {
51                     return Some($name::$variant)
52                 })*
53                 None
54             }
55         }
56     }
57 }