blob: 7cd8a243c58dd2793021de08d6af2ec0c262fe3c [file] [log] [blame]
Alex Crichton5d6cf052015-09-11 14:52:34 -07001/// A macro for defining #[cfg] if-else statements.
2///
3/// This is similar to the `if/elif` C preprocessor macro by allowing definition
4/// of a cascade of `#[cfg]` cases, emitting the implementation which matches
5/// first.
6///
7/// This allows you to conveniently provide a long list #[cfg]'d blocks of code
8/// without having to rewrite each clause multiple times.
Alex Crichton31504842015-09-10 23:43:41 -07009macro_rules! cfg_if {
10 ($(
11 if #[cfg($($meta:meta),*)] { $($it:item)* }
12 ) else * else {
13 $($it2:item)*
14 }) => {
15 __cfg_if_items! {
16 () ;
17 $( ( ($($meta),*) ($($it)*) ), )*
18 ( () ($($it2)*) ),
19 }
20 }
21}
22
Alex Crichton31504842015-09-10 23:43:41 -070023macro_rules! __cfg_if_items {
24 (($($not:meta,)*) ; ) => {};
25 (($($not:meta,)*) ; ( ($($m:meta),*) ($($it:item)*) ), $($rest:tt)*) => {
26 __cfg_if_apply! { cfg(all($($m,)* not(any($($not),*)))), $($it)* }
27 __cfg_if_items! { ($($not,)* $($m,)*) ; $($rest)* }
28 }
29}
30
Alex Crichton31504842015-09-10 23:43:41 -070031macro_rules! __cfg_if_apply {
32 ($m:meta, $($it:item)*) => {
33 $(#[$m] $it)*
34 }
35}
36
Alex Crichton5d6cf052015-09-11 14:52:34 -070037macro_rules! s {
38 ($(pub struct $i:ident { $($field:tt)* })*) => ($(
39 __item! {
40 #[repr(C)]
41 pub struct $i { $($field)* }
42 }
Alex Crichton24abc4f2015-09-16 23:54:56 -070043 impl ::dox::Copy for $i {}
44 impl ::dox::Clone for $i {
Alex Crichton5d6cf052015-09-11 14:52:34 -070045 fn clone(&self) -> $i { *self }
46 }
47 )*)
48}
49
Alex Crichton07d3a0d2015-10-30 10:21:32 -070050macro_rules! f {
51 ($(pub fn $i:ident($($arg:ident: $argty:ty),*) -> $ret:ty {
52 $($body:stmt);*
53 })*) => ($(
54 #[inline]
55 #[cfg(not(dox))]
56 pub unsafe extern fn $i($($arg: $argty),*) -> $ret {
57 $($body);*
58 }
59
60 #[cfg(dox)]
61 #[allow(dead_code)]
62 pub unsafe extern fn $i($($arg: $argty),*) -> $ret {
63 loop {}
64 }
65 )*)
66}
67
Alex Crichton5d6cf052015-09-11 14:52:34 -070068macro_rules! __item {
69 ($i:item) => ($i)
70}
71
Alex Crichton31504842015-09-10 23:43:41 -070072#[cfg(test)]
73mod tests {
74 cfg_if! {
75 if #[cfg(test)] {
76 use std::option::Option as Option2;
77 fn works1() -> Option2<u32> { Some(1) }
78 } else {
79 fn works1() -> Option<u32> { None }
80 }
81 }
82
83 cfg_if! {
84 if #[cfg(foo)] {
85 fn works2() -> bool { false }
86 } else if #[cfg(test)] {
87 fn works2() -> bool { true }
88 } else {
89 fn works2() -> bool { false }
90 }
91 }
92
93 cfg_if! {
94 if #[cfg(foo)] {
95 fn works3() -> bool { false }
96 } else {
97 fn works3() -> bool { true }
98 }
99 }
100
101 #[test]
102 fn it_works() {
103 assert!(works1().is_some());
104 assert!(works2());
105 assert!(works3());
106 }
107}