rust: prelude: add bit function
PATCH 1/1 rust: prelude: add bit function
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
---
This patch is needed for further patches porting the rockchip phy driver to rust.
--- rust/kernel/prelude.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
rust/kernel/prelude.rs +16 -0
@@ -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,
Signature · 1 line
Christina Quast <[email protected]>
Quoted 7 lines · Christina Quast
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:
usekernel::prelude::bitSigned-off-by Christina Quast <[email protected]>
Quoted 37 lines
---
This patch is needed for further patches porting the rockchip phy driver to rust.--- rust/kernel/prelude.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)rust/kernel/prelude.rs +16
-1@@ -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-99dd6d3a0536Best regards,
Quoted 3 lines · Christina Quast
In order to create masks easily, the define
BIT()is used in C code. This commit adds the same functionality to the rust kernel.
The rust branch has a generic bit function that allows the returned bit to be used with or converted to several numeric types.
https://github.com/Rust-for-Linux/linux/blob/rust/rust/kernel/types.rs#L344
Quoted 10 lines · Wedson Almeida Filho
Quoted 3 lines · Christina Quast
In order to create masks easily, the define
BIT()is used in C code. This commit adds the same functionality to the rust kernel.The
rustbranch has a genericbitfunction that allows the returned bit to be used with or converted to several numeric types.https://github.com/Rust-for-Linux/linux/blob/rust/rust/kernel/types.rs#L344
Plus, I think it's better to put the implementation at somewhere like:
kernel::type::bitops
Of course, it's OK to prelude it, but we shouldn't put implementation in prelude.rs
Regards,
Boqun
Hi Christina,
Thanks for the patch! Please see below.
Quoted 6 lines · Christina Quast
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:
usekernel::prelude::bit
This is the prelude, i.e. the point is that it does not need to be used.
Did you check the rust branch? We had something like this there.
rust/kernel/prelude.rs
Quoted 4 lines
rust/kernel/prelude.rs +0
-0
Please note that the prelude is meant to re-export existing functionality elsewhere in the kernel crate, not to define items there.
rust/kernel/prelude.rs line 46
Quoted 3 lines
rust/kernel/prelude.rs +3
-0+/// # Arguments +/// +/// * `n` - A `u32` that specifies the bit position (zero-based index)
We don't use "Arguments"-like sections. Please see other functions' documentation to see how we usually do it.
Quoted 1 line
+/// # Example
Please use the plural.
rust/kernel/prelude.rs line 52
Quoted 3 lines
rust/kernel/prelude.rs +3
-0+/// ``` +/// let b = bit(2); +/// assert_eq!(b, 4);
The example is not closed.
rust/kernel/prelude.rs line 56
Quoted 4 lines
rust/kernel/prelude.rs +4
-0+#[inline] +pub const fn bit (n:u32 ) ->u32 { +1 << n +}
Should it be a generic?
Cheers,
Miguel