• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /// Asserts that a given configuration is set.
2 ///
3 /// # Examples
4 ///
5 /// A project will simply fail to compile if the given configuration is not set.
6 ///
7 /// ```
8 /// # #[macro_use] extern crate static_assertions; fn main() {}
9 /// // We're not masochists
10 /// # #[cfg(not(target_pointer_width = "16"))] // Just in case
11 /// assert_cfg!(not(target_pointer_width = "16"));
12 /// ```
13 ///
14 /// If a project does not support a set of configurations, you may want to
15 /// report why. There is the option of providing a compile error message string:
16 ///
17 /// ```
18 /// # #[macro_use] extern crate static_assertions; fn main() {}
19 /// # #[cfg(any(unix, windows))]
20 /// assert_cfg!(any(unix, windows), "There is only support for Unix or Windows");
21 ///
22 /// // User needs to specify a database back-end
23 /// # #[cfg(target_pointer_width = "0")] // Impossible
24 /// assert_cfg!(all(not(all(feature = "mysql", feature = "mongodb")),
25 ///                 any(    feature = "mysql", feature = "mongodb")),
26 ///             "Must exclusively use MySQL or MongoDB as database back-end");
27 /// ```
28 ///
29 /// Some configurations are impossible. For example, we can't be compiling for
30 /// both macOS _and_ Windows simultaneously:
31 ///
32 /// ```compile_fail
33 /// # #[macro_use] extern crate static_assertions; fn main() {}
34 /// assert_cfg!(all(target_os = "macos",
35 ///                 target_os = "windows"),
36 ///             "No, that's not how it works! ಠ_ಠ");
37 /// ```
38 #[macro_export]
39 macro_rules! assert_cfg {
40     () => {};
41     ($($cfg:meta)+, $msg:expr $(,)?) => {
42         #[cfg(not($($cfg)+))]
43         compile_error!($msg);
44     };
45     ($($cfg:tt)*) => {
46         #[cfg(not($($cfg)*))]
47         compile_error!(concat!("Cfg does not pass: ", stringify!($($cfg)*)));
48     };
49 }
50