From 18be937cb51b315082014c982138b487dbcf5e9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Sat, 29 Jun 2019 20:12:46 +0200 Subject: [PATCH] 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`. --- substrate/core/sr-std/src/lib.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/substrate/core/sr-std/src/lib.rs b/substrate/core/sr-std/src/lib.rs index b9874bcc20..24c137c285 100644 --- a/substrate/core/sr-std/src/lib.rs +++ b/substrate/core/sr-std/src/lib.rs @@ -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");