public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] rust: impl_flags: add convenience functions
@ 2026-02-15 20:22 Andreas Hindborg
  2026-02-15 20:22 ` [PATCH 1/2] rust: impl_flags: add conversion functions Andreas Hindborg
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Andreas Hindborg @ 2026-02-15 20:22 UTC (permalink / raw)
  To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich
  Cc: rust-for-linux, linux-kernel, Andreas Hindborg

Add a few convenience functions that makes it easier to work with the
`impl_flags` module and C APIs.

Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
Andreas Hindborg (2):
      rust: impl_flags: add conversion functions
      rust: impl_flags: add bitwise operations with the underlying type

 rust/kernel/impl_flags.rs | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
---
base-commit: e9ec05addd1a067fc7cb218f20ecdc1b1b0898c0
change-id: 20260215-impl-flags-additions-0340ffcba5b9
prerequisite-change-id: 20250304-feat-add-bitmask-macro-6424b1c317e2:v8
prerequisite-patch-id: 05dfcf648017dee65c356604e4f99959e76ff64d
prerequisite-change-id: 20260212-impl-flags-inner-c61974b27b18:v1
prerequisite-patch-id: edebc37750dccd8d19db0b8fbd5f69f9877675a2

Best regards,
-- 
Andreas Hindborg <a.hindborg@kernel.org>



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

* [PATCH 1/2] rust: impl_flags: add conversion functions
  2026-02-15 20:22 [PATCH 0/2] rust: impl_flags: add convenience functions Andreas Hindborg
@ 2026-02-15 20:22 ` Andreas Hindborg
  2026-02-15 20:53   ` Miguel Ojeda
  2026-02-15 20:22 ` [PATCH 2/2] rust: impl_flags: add bitwise operations with the underlying type Andreas Hindborg
  2026-02-27 14:20 ` [PATCH 0/2] rust: impl_flags: add convenience functions Gary Guo
  2 siblings, 1 reply; 5+ messages in thread
From: Andreas Hindborg @ 2026-02-15 20:22 UTC (permalink / raw)
  To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich
  Cc: rust-for-linux, linux-kernel, Andreas Hindborg

Add two conversion functions to the `impl_flags!` macro:

 - A `From<_>` implementation to convert from the flag value enum to
   underlying type.

 - A `TryFrom<_> implementation to convert from the underlying
   representation to flag container type.

Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
 rust/kernel/impl_flags.rs | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/rust/kernel/impl_flags.rs b/rust/kernel/impl_flags.rs
index 9160e8ce2e126..f34ecbe870d80 100644
--- a/rust/kernel/impl_flags.rs
+++ b/rust/kernel/impl_flags.rs
@@ -102,6 +102,26 @@ fn from(value: $flags) -> Self {
             }
         }
 
+        impl ::core::convert::From<$flag> for $ty {
+            #[inline]
+            fn from(value: $flag) -> Self {
+                // SAFETY: All `$flag` values are valid for use as `$ty`.
+                unsafe { core::mem::transmute::<$flag, $ty>(value) }
+            }
+        }
+
+        impl ::core::convert::TryFrom<$ty> for $flags {
+            type Error = ::kernel::error::Error;
+
+            fn try_from(value: $ty) -> Result<Self, Self::Error> {
+                match value & !$flags::all_bits() == 0 {
+                    // SAFETY: We checked above that `value` is a valid bit pattern for `$flags`.
+                    true => Ok(unsafe {::core::mem::transmute::<$ty, $flags>(value)}),
+                    false => Err(::kernel::error::code::EINVAL),
+                }
+            }
+        }
+
         impl ::core::ops::BitOr for $flags {
             type Output = Self;
             #[inline]

-- 
2.51.2



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

* [PATCH 2/2] rust: impl_flags: add bitwise operations with the underlying type
  2026-02-15 20:22 [PATCH 0/2] rust: impl_flags: add convenience functions Andreas Hindborg
  2026-02-15 20:22 ` [PATCH 1/2] rust: impl_flags: add conversion functions Andreas Hindborg
@ 2026-02-15 20:22 ` Andreas Hindborg
  2026-02-27 14:20 ` [PATCH 0/2] rust: impl_flags: add convenience functions Gary Guo
  2 siblings, 0 replies; 5+ messages in thread
From: Andreas Hindborg @ 2026-02-15 20:22 UTC (permalink / raw)
  To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich
  Cc: rust-for-linux, linux-kernel, Andreas Hindborg

Add bitwise or operations between the flag value enum and the underlying
type. This is useful when manipulating flags from C API without round
tripping into the Rust flag container type:

  let mut lim: bindings::queue_limits = unsafe { core::mem::zeroed() };
  ...
  if self.write_cache {
      lim.features |= request::Feature::WriteCache;
  }

The above code would be needlessly verbose without this direct assignment
option.

Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
 rust/kernel/impl_flags.rs | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/rust/kernel/impl_flags.rs b/rust/kernel/impl_flags.rs
index f34ecbe870d80..f829be815885e 100644
--- a/rust/kernel/impl_flags.rs
+++ b/rust/kernel/impl_flags.rs
@@ -245,6 +245,22 @@ fn bitxor(self, rhs: $flag) -> Self::Output {
             }
         }
 
+        impl ::core::ops::BitOr<$flag> for $ty {
+            type Output = Self;
+
+            #[inline]
+            fn bitor(self, rhs: $flag) -> Self::Output {
+                self | <$flag as Into<Self>>::into(rhs)
+            }
+        }
+
+        impl ::core::ops::BitOrAssign<$flag> for $ty {
+            #[inline]
+            fn bitor_assign(&mut self, rhs: $flag) {
+                *self = *self | <$flag as Into<Self>>::into(rhs)
+            }
+        }
+
         impl $flags {
             /// Returns an empty instance of `type` where no flags are set.
             #[inline]

-- 
2.51.2



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

* Re: [PATCH 1/2] rust: impl_flags: add conversion functions
  2026-02-15 20:22 ` [PATCH 1/2] rust: impl_flags: add conversion functions Andreas Hindborg
@ 2026-02-15 20:53   ` Miguel Ojeda
  0 siblings, 0 replies; 5+ messages in thread
From: Miguel Ojeda @ 2026-02-15 20:53 UTC (permalink / raw)
  To: Andreas Hindborg
  Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	rust-for-linux, linux-kernel

On Sun, Feb 15, 2026 at 9:23 PM Andreas Hindborg <a.hindborg@kernel.org> wrote:
>
> +        impl ::core::convert::From<$flag> for $ty {
> +            #[inline]
> +            fn from(value: $flag) -> Self {
> +                // SAFETY: All `$flag` values are valid for use as `$ty`.
> +                unsafe { core::mem::transmute::<$flag, $ty>(value) }
> +            }
> +        }

Why do we need the transmute instead of a cast? Did you notice a difference?

Same in the other one -- why not constructing the object normally?

Also, you can use `Self` for `$ty` and `$flags` to make it easier to read.

In addition, I would suggest an early return with a simple `if`.

Finally, it wouldn't hurt adding a one-liner example for each at the top.

Thanks!

Cheers,
Miguel

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

* Re: [PATCH 0/2] rust: impl_flags: add convenience functions
  2026-02-15 20:22 [PATCH 0/2] rust: impl_flags: add convenience functions Andreas Hindborg
  2026-02-15 20:22 ` [PATCH 1/2] rust: impl_flags: add conversion functions Andreas Hindborg
  2026-02-15 20:22 ` [PATCH 2/2] rust: impl_flags: add bitwise operations with the underlying type Andreas Hindborg
@ 2026-02-27 14:20 ` Gary Guo
  2 siblings, 0 replies; 5+ messages in thread
From: Gary Guo @ 2026-02-27 14:20 UTC (permalink / raw)
  To: Andreas Hindborg, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross,
	Danilo Krummrich
  Cc: rust-for-linux, linux-kernel

On Sun Feb 15, 2026 at 8:22 PM GMT, Andreas Hindborg wrote:
> Add a few convenience functions that makes it easier to work with the
> `impl_flags` module and C APIs.

I am not sure that we want to encourage direct manipulation of the underlying
bits. Adding these impls would add a surface of misuse.

Best,
Gary

>
> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
> ---
> Andreas Hindborg (2):
>       rust: impl_flags: add conversion functions
>       rust: impl_flags: add bitwise operations with the underlying type
>
>  rust/kernel/impl_flags.rs | 36 ++++++++++++++++++++++++++++++++++++
>  1 file changed, 36 insertions(+)
> ---
> base-commit: e9ec05addd1a067fc7cb218f20ecdc1b1b0898c0
> change-id: 20260215-impl-flags-additions-0340ffcba5b9
> prerequisite-change-id: 20250304-feat-add-bitmask-macro-6424b1c317e2:v8
> prerequisite-patch-id: 05dfcf648017dee65c356604e4f99959e76ff64d
> prerequisite-change-id: 20260212-impl-flags-inner-c61974b27b18:v1
> prerequisite-patch-id: edebc37750dccd8d19db0b8fbd5f69f9877675a2
>
> Best regards,


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

end of thread, other threads:[~2026-02-27 14:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-15 20:22 [PATCH 0/2] rust: impl_flags: add convenience functions Andreas Hindborg
2026-02-15 20:22 ` [PATCH 1/2] rust: impl_flags: add conversion functions Andreas Hindborg
2026-02-15 20:53   ` Miguel Ojeda
2026-02-15 20:22 ` [PATCH 2/2] rust: impl_flags: add bitwise operations with the underlying type Andreas Hindborg
2026-02-27 14:20 ` [PATCH 0/2] rust: impl_flags: add convenience functions Gary Guo

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