1 //! A macro for defining `#[cfg]` if-else statements. 2 //! 3 //! The macro provided by this crate, `cfg_if`, is similar to the `if/elif` C 4 //! preprocessor macro by allowing definition of a cascade of `#[cfg]` cases, 5 //! emitting the implementation which matches 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. 9 //! 10 //! # Example 11 //! 12 //! ``` 13 //! cfg_if::cfg_if! { 14 //! if #[cfg(unix)] { 15 //! fn foo() { /* unix specific functionality */ } 16 //! } else if #[cfg(target_pointer_width = "32")] { 17 //! fn foo() { /* non-unix, 32-bit functionality */ } 18 //! } else { 19 //! fn foo() { /* fallback implementation */ } 20 //! } 21 //! } 22 //! 23 //! # fn main() {} 24 //! ``` 25 26 #![no_std] 27 #![doc(html_root_url = "https://docs.rs/cfg-if")] 28 #![deny(missing_docs)] 29 #![cfg_attr(test, deny(warnings))] 30 31 // ANDROID: Use std to allow building as a dylib. 32 #[cfg(android_dylib)] 33 extern crate std; 34 35 /// The main macro provided by this crate. See crate documentation for more 36 /// information. 37 #[macro_export] 38 macro_rules! cfg_if { 39 // match if/else chains with a final `else` 40 ($( 41 if #[cfg($meta:meta)] { $($tokens:tt)* } 42 ) else * else { 43 $($tokens2:tt)* 44 }) => { 45 $crate::cfg_if! { 46 @__items 47 () ; 48 $( ( ($meta) ($($tokens)*) ), )* 49 ( () ($($tokens2)*) ), 50 } 51 }; 52 53 // match if/else chains lacking a final `else` 54 ( 55 if #[cfg($i_met:meta)] { $($i_tokens:tt)* } 56 $( 57 else if #[cfg($e_met:meta)] { $($e_tokens:tt)* } 58 )* 59 ) => { 60 $crate::cfg_if! { 61 @__items 62 () ; 63 ( ($i_met) ($($i_tokens)*) ), 64 $( ( ($e_met) ($($e_tokens)*) ), )* 65 ( () () ), 66 } 67 }; 68 69 // Internal and recursive macro to emit all the items 70 // 71 // Collects all the negated cfgs in a list at the beginning and after the 72 // semicolon is all the remaining items 73 (@__items ($($not:meta,)*) ; ) => {}; 74 (@__items ($($not:meta,)*) ; ( ($($m:meta),*) ($($tokens:tt)*) ), $($rest:tt)*) => { 75 // Emit all items within one block, applying an appropriate #[cfg]. The 76 // #[cfg] will require all `$m` matchers specified and must also negate 77 // all previous matchers. 78 #[cfg(all($($m,)* not(any($($not),*))))] $crate::cfg_if! { @__identity $($tokens)* } 79 80 // Recurse to emit all other items in `$rest`, and when we do so add all 81 // our `$m` matchers to the list of `$not` matchers as future emissions 82 // will have to negate everything we just matched as well. 83 $crate::cfg_if! { @__items ($($not,)* $($m,)*) ; $($rest)* } 84 }; 85 86 // Internal macro to make __apply work out right for different match types, 87 // because of how macros matching/expand stuff. 88 (@__identity $($tokens:tt)*) => { 89 $($tokens)* 90 }; 91 } 92 93 #[cfg(test)] 94 mod tests { 95 cfg_if! { 96 if #[cfg(test)] { 97 use core::option::Option as Option2; 98 fn works1() -> Option2<u32> { Some(1) } 99 } else { 100 fn works1() -> Option<u32> { None } 101 } 102 } 103 104 cfg_if! { 105 if #[cfg(foo)] { 106 fn works2() -> bool { false } 107 } else if #[cfg(test)] { 108 fn works2() -> bool { true } 109 } else { 110 fn works2() -> bool { false } 111 } 112 } 113 114 cfg_if! { 115 if #[cfg(foo)] { 116 fn works3() -> bool { false } 117 } else { 118 fn works3() -> bool { true } 119 } 120 } 121 122 cfg_if! { 123 if #[cfg(test)] { 124 use core::option::Option as Option3; 125 fn works4() -> Option3<u32> { Some(1) } 126 } 127 } 128 129 cfg_if! { 130 if #[cfg(foo)] { 131 fn works5() -> bool { false } 132 } else if #[cfg(test)] { 133 fn works5() -> bool { true } 134 } 135 } 136 137 #[test] it_works()138 fn it_works() { 139 assert!(works1().is_some()); 140 assert!(works2()); 141 assert!(works3()); 142 assert!(works4().is_some()); 143 assert!(works5()); 144 } 145 146 #[test] 147 #[allow(clippy::assertions_on_constants)] test_usage_within_a_function()148 fn test_usage_within_a_function() { 149 cfg_if! {if #[cfg(debug_assertions)] { 150 // we want to put more than one thing here to make sure that they 151 // all get configured properly. 152 assert!(cfg!(debug_assertions)); 153 assert_eq!(4, 2+2); 154 } else { 155 assert!(works1().is_some()); 156 assert_eq!(10, 5+5); 157 }} 158 } 159 160 trait Trait { blah(&self)161 fn blah(&self); 162 } 163 164 #[allow(dead_code)] 165 struct Struct; 166 167 impl Trait for Struct { 168 cfg_if! { 169 if #[cfg(feature = "blah")] { 170 fn blah(&self) { 171 unimplemented!(); 172 } 173 } else { 174 fn blah(&self) { 175 unimplemented!(); 176 } 177 } 178 } 179 } 180 } 181