From: Areej <areejhamid8560@gmail.com>
To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Viresh Kumar" <viresh.kumar@linaro.org>,
"Tamir Duberstein" <tamird@gmail.com>,
"Xiangfei Ding" <dingxiangfei2009@gmail.com>,
Areej <areejhamid8560@gmail.com>
Subject: [PATCH] rust: lib: add if_cfg! macro for conditional compilation
Date: Thu, 14 Aug 2025 01:38:26 +0500 [thread overview]
Message-ID: <20250813203826.3145553-1-areejhamid8560@gmail.com> (raw)
Add a new if_cfg! macro to simplify conditional compilation using
cfg attributes. This macro expands to paired #[cfg(cond)] and
#[cfg(not(cond))] blocks, allowing compile-time selection between
code branches in both expression and statement contexts.
Suggested-by: Benno Lossin <lossin@kernel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/1183
Signed-off-by: Areej Hamid <areejhamid8560@gmail.com>
---
rust/kernel/lib.rs | 37 +++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+)
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index ed53169e795c..47e73949392d 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -294,6 +294,42 @@ macro_rules! asm {
};
}
+/// Conditionally compiles and executes code based on a `#[cfg]` condition.
+///
+/// Expands to `#[cfg(cond)] { ... }` and `#[cfg(not(cond))] { ... }`,
+/// allowing conditional compilation in both expression and statement positions.
+///
+/// This macro is useful when both branches must be valid Rust code and the
+/// selection between them is done at compile time via a config option.
+/// # Examples
+/// ```
+/// # use kernel::if_cfg;
+/// // Select a value depending on CONFIG_64BIT.
+/// let x = if_cfg!(if CONFIG_64BIT {
+/// 64
+/// } else {
+/// 32
+/// });
+///
+/// // `x` will be 64 if CONFIG_64BIT is enabled, otherwise 32.
+/// assert!(x == 64 || x == 32);
+/// ```
+#[macro_export]
+macro_rules! if_cfg {
+ (if $cond:tt { $($then:tt)* } else { $($else:tt)* }) => {{
+ #[cfg($cond)]
+ { $($then)* }
+ #[cfg(not($cond))]
+ { $($else)* }
+ }};
+ (if $cond:tt { $($then:tt)* }) => {{
+ #[cfg($cond)]
+ { $($then)* }
+ #[cfg(not($cond))]
+ { () }
+ }};
+}
+
/// Gets the C string file name of a [`Location`].
///
/// If `file_with_nul()` is not available, returns a string that warns about it.
@@ -337,3 +373,4 @@ pub fn file_from_location<'a>(loc: &'a core::panic::Location<'a>) -> &'a core::f
c"<Location::file_with_nul() not supported>"
}
}
+
--
2.43.0
next reply other threads:[~2025-08-13 20:39 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-13 20:38 Areej [this message]
2025-08-13 20:59 ` [PATCH] rust: lib: add if_cfg! macro for conditional compilation Thomas Weißschuh
[not found] ` <CAMjM9+b60ate7cpHfm6uPHFauuYyPA1KcukqMgLjCQcC_zak+A@mail.gmail.com>
2025-08-14 5:07 ` Thomas Weißschuh
2025-08-14 16:22 ` [PATCH v2] " Areej Hamid
2025-08-14 18:49 ` Miguel Ojeda
2025-08-14 18:54 ` Miguel Ojeda
2025-08-15 8:16 ` Benno Lossin
2025-08-14 5:55 ` [PATCH] " Greg Kroah-Hartman
[not found] <CAMjM9+aqeh4iEyfAKidYig_5zEV-CMZuJpmtzhqjKHiVTiBpYA@mail.gmail.com>
2025-08-12 21:37 ` Miguel Ojeda
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250813203826.3145553-1-areejhamid8560@gmail.com \
--to=areejhamid8560@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=dingxiangfei2009@gmail.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@gmail.com \
--cc=tmgross@umich.edu \
--cc=viresh.kumar@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).