Re: [PATCH] rust: prelude: add bit function

From: Carlos Bilbao
Date: Tue Jan 30 2024 - 14:59:32 EST


On 1/30/24 13:47, Christina Quast wrote:
In order to create masks easily, the define BIT() is used in C code.
This commit adds the same functionality to the rust kernel.

To use it, include the following into your rust file:
use kernel::prelude::bit

Signed-off-by: Christina Quast <contact@xxxxxxxxxxxxxxxxxx>

Reviewed-by: Carlos Bilbao <carlos.bilbao@xxxxxxx>

---
This patch is needed for further patches porting the rockchip phy
driver to rust.
---
rust/kernel/prelude.rs | 16 ++++++++++++++++
1 file changed, 16 insertions(+)

diff --git a/rust/kernel/prelude.rs b/rust/kernel/prelude.rs
index ae21600970b3..16e483de2f27 100644
--- a/rust/kernel/prelude.rs
+++ b/rust/kernel/prelude.rs
@@ -38,3 +38,19 @@
pub use super::init::{InPlaceInit, Init, PinInit};
pub use super::current;
+
+/// Returns a `u32` number that has only the `n`th bit set.
+///
+/// # Arguments
+///
+/// * `n` - A `u32` that specifies the bit position (zero-based index)
+///
+/// # Example
+///
+/// ```
+/// let b = bit(2);
+/// assert_eq!(b, 4);
+#[inline]
+pub const fn bit(n: u32) -> u32 {
+ 1 << n
+}

---
base-commit: 6613476e225e090cc9aad49be7fa504e290dd33d
change-id: 20240130-rust-bit-99dd6d3a0536

Best regards,