Adds if_std! macro (#2979)

This macro compiles and executes the given code only when `std` feature
is enabled. This can be useful for debugging without needing to worry
that your code does not compile on `no_std`.
This commit is contained in:
Bastian Köcher
2019-06-29 20:12:46 +02:00
committed by GitHub
parent 7b1e8dc3df
commit 18be937cb5
+26
View File
@@ -30,6 +30,32 @@ macro_rules! map {
)
}
/// Feature gate some code that should only be run when `std` feature is enabled.
///
/// # Example
///
/// ```
/// use sr_std::if_std;
///
/// if_std! {
/// // This code is only being compiled and executed when the `std` feature is enabled.
/// println!("Hello native world");
/// }
/// ```
#[cfg(feature = "std")]
#[macro_export]
macro_rules! if_std {
( $( $code:tt )* ) => {
$( $code )*
}
}
#[cfg(not(feature = "std"))]
#[macro_export]
macro_rules! if_std {
( $( $code:tt )* ) => {}
}
#[cfg(feature = "std")]
include!("../with_std.rs");