mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] rust: prelude: add bit function
@ 2024-01-30 19:47 Christina Quast
  2024-01-30 19:59 ` Carlos Bilbao
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Christina Quast @ 2024-01-30 19:47 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Alice Ryhl
  Cc: rust-for-linux, linux-kernel, 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:
use kernel::prelude::bit

Signed-off-by: Christina Quast <contact@christina-quast.de>
---
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,
-- 
Christina Quast <contact@christina-quast.de>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: prelude: add bit function
  2024-01-30 19:47 [PATCH] rust: prelude: add bit function Christina Quast
@ 2024-01-30 19:59 ` Carlos Bilbao
  2024-01-30 20:04 ` Wedson Almeida Filho
  2024-01-30 20:10 ` Miguel Ojeda
  2 siblings, 0 replies; 5+ messages in thread
From: Carlos Bilbao @ 2024-01-30 19:59 UTC (permalink / raw)
  To: Christina Quast, Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl
  Cc: rust-for-linux, linux-kernel

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@christina-quast.de>

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

> ---
> 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,

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: prelude: add bit function
  2024-01-30 19:47 [PATCH] rust: prelude: add bit function Christina Quast
  2024-01-30 19:59 ` Carlos Bilbao
@ 2024-01-30 20:04 ` Wedson Almeida Filho
  2024-01-30 20:05   ` Boqun Feng
  2024-01-30 20:10 ` Miguel Ojeda
  2 siblings, 1 reply; 5+ messages in thread
From: Wedson Almeida Filho @ 2024-01-30 20:04 UTC (permalink / raw)
  To: Christina Quast
  Cc: Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	rust-for-linux, linux-kernel

On Tue, 30 Jan 2024 at 16:48, Christina Quast
<contact@christina-quast.de> 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.

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: prelude: add bit function
  2024-01-30 20:04 ` Wedson Almeida Filho
@ 2024-01-30 20:05   ` Boqun Feng
  0 siblings, 0 replies; 5+ messages in thread
From: Boqun Feng @ 2024-01-30 20:05 UTC (permalink / raw)
  To: Wedson Almeida Filho
  Cc: Christina Quast, Miguel Ojeda, Alex Gaynor, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	rust-for-linux, linux-kernel

On Tue, Jan 30, 2024 at 05:04:15PM -0300, Wedson Almeida Filho wrote:
> On Tue, 30 Jan 2024 at 16:48, Christina Quast
> <contact@christina-quast.de> 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.
> 
> 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

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: prelude: add bit function
  2024-01-30 19:47 [PATCH] rust: prelude: add bit function Christina Quast
  2024-01-30 19:59 ` Carlos Bilbao
  2024-01-30 20:04 ` Wedson Almeida Filho
@ 2024-01-30 20:10 ` Miguel Ojeda
  2 siblings, 0 replies; 5+ messages in thread
From: Miguel Ojeda @ 2024-01-30 20:10 UTC (permalink / raw)
  To: Christina Quast
  Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Alice Ryhl, rust-for-linux, linux-kernel

Hi Christina,

Thanks for the patch! Please see below.

On Tue, Jan 30, 2024 at 8:48 PM Christina Quast
<contact@christina-quast.de> 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

This is the prelude, i.e. the point is that it does not need to be `use`d.

Did you check the `rust` branch? We had something like this there.

> 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

Please note that the prelude is meant to re-export existing
functionality elsewhere in the `kernel` crate, not to define items
there.

> +/// # 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.

> +/// # Example

Please use the plural.

> +/// ```
> +/// let b = bit(2);
> +/// assert_eq!(b, 4);

The example is not closed.

> +#[inline]
> +pub const fn bit(n: u32) -> u32 {
> +    1 << n
> +}

Should it be a generic?

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-01-30 20:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-30 19:47 [PATCH] rust: prelude: add bit function Christina Quast
2024-01-30 19:59 ` Carlos Bilbao
2024-01-30 20:04 ` Wedson Almeida Filho
2024-01-30 20:05   ` Boqun Feng
2024-01-30 20:10 ` Miguel Ojeda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®