public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rust: impl_flags: add method to return underlying integer
@ 2026-02-12 16:02 Andreas Hindborg
  2026-02-12 16:53 ` Daniel Almeida
  2026-02-14  0:13 ` Gary Guo
  0 siblings, 2 replies; 6+ messages in thread
From: Andreas Hindborg @ 2026-02-12 16:02 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 method to return the underlying integer used for flags. This is
useful when using the flags with C APIs.

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

diff --git a/rust/kernel/impl_flags.rs b/rust/kernel/impl_flags.rs
index e2bd7639da12e..951598497de1d 100644
--- a/rust/kernel/impl_flags.rs
+++ b/rust/kernel/impl_flags.rs
@@ -267,6 +267,12 @@ pub fn contains_any(self, flags: $flags) -> bool {
             pub fn contains_all(self, flags: $flags) -> bool {
                 (self.0 & flags.0) == flags.0
             }
+
+            /// Return a copy of the inner representation of the flags.
+            #[inline]
+            pub fn into_inner(self) -> $ty {
+                self.0
+            }
         }
     };
 }

---
base-commit: b8d687c7eeb52d0353ac27c4f71594a2e6aa365f
change-id: 20260212-impl-flags-inner-c61974b27b18

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



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

* Re: [PATCH] rust: impl_flags: add method to return underlying integer
  2026-02-12 16:02 [PATCH] rust: impl_flags: add method to return underlying integer Andreas Hindborg
@ 2026-02-12 16:53 ` Daniel Almeida
  2026-02-14  0:13 ` Gary Guo
  1 sibling, 0 replies; 6+ messages in thread
From: Daniel Almeida @ 2026-02-12 16: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, Andreas Hindborg




> On 12 Feb 2026, at 13:03, Andreas Hindborg <a.hindborg@kernel.org> wrote:
> 
> Add a method to return the underlying integer used for flags. This is
> useful when using the flags with C APIs.
> 
> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
> ---
> rust/kernel/impl_flags.rs | 6 ++++++
> 1 file changed, 6 insertions(+)
> 
> diff --git a/rust/kernel/impl_flags.rs b/rust/kernel/impl_flags.rs
> index e2bd7639da12e..951598497de1d 100644
> --- a/rust/kernel/impl_flags.rs
> +++ b/rust/kernel/impl_flags.rs
> @@ -267,6 +267,12 @@ pub fn contains_any(self, flags: $flags) -> bool {
>             pub fn contains_all(self, flags: $flags) -> bool {
>                 (self.0 & flags.0) == flags.0
>             }
> +
> +            /// Return a copy of the inner representation of the flags.
> +            #[inline]
> +            pub fn into_inner(self) -> $ty {
> +                self.0
> +            }
>         }
>     };
> }
> 
> ---
> base-commit: b8d687c7eeb52d0353ac27c4f71594a2e6aa365f
> change-id: 20260212-impl-flags-inner-c61974b27b18
> 
> Best regards,
> --
> Andreas Hindborg <a.hindborg@kernel.org>
> 
> 
> 

Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>

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

* Re: [PATCH] rust: impl_flags: add method to return underlying integer
  2026-02-12 16:02 [PATCH] rust: impl_flags: add method to return underlying integer Andreas Hindborg
  2026-02-12 16:53 ` Daniel Almeida
@ 2026-02-14  0:13 ` Gary Guo
  2026-02-14  8:02   ` Andreas Hindborg
  1 sibling, 1 reply; 6+ messages in thread
From: Gary Guo @ 2026-02-14  0:13 UTC (permalink / raw)
  To: Andreas Hindborg
  Cc: Miguel Ojeda, Boqun Feng, Björn Roy Baron, Benno Lossin,
	Alice Ryhl, Trevor Gross, Danilo Krummrich, rust-for-linux,
	linux-kernel

On 2026-02-12 16:02, Andreas Hindborg wrote:
> Add a method to return the underlying integer used for flags. This is
> useful when using the flags with C APIs.
> 
> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>

I am not sure if we want to actually expose the underlying representation
for all flags. It should be something up to the caller of `impl_flags` to
decide.

Would it make sense to add visibility the wrapped raw type so that you
can do

impl_flags! {
    pub struct Foo(pub u32);
    ...
}

to make the inner accessible?

Best,
Gary

> ---
>  rust/kernel/impl_flags.rs | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/rust/kernel/impl_flags.rs b/rust/kernel/impl_flags.rs
> index e2bd7639da12e..951598497de1d 100644
> --- a/rust/kernel/impl_flags.rs
> +++ b/rust/kernel/impl_flags.rs
> @@ -267,6 +267,12 @@ pub fn contains_any(self, flags: $flags) -> bool {
>              pub fn contains_all(self, flags: $flags) -> bool {
>                  (self.0 & flags.0) == flags.0
>              }
> +
> +            /// Return a copy of the inner representation of the flags.
> +            #[inline]
> +            pub fn into_inner(self) -> $ty {
> +                self.0
> +            }
>          }
>      };
>  }
> 
> ---
> base-commit: b8d687c7eeb52d0353ac27c4f71594a2e6aa365f
> change-id: 20260212-impl-flags-inner-c61974b27b18
> 
> Best regards,

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

* Re: [PATCH] rust: impl_flags: add method to return underlying integer
  2026-02-14  0:13 ` Gary Guo
@ 2026-02-14  8:02   ` Andreas Hindborg
  2026-02-14  9:36     ` Gary Guo
  0 siblings, 1 reply; 6+ messages in thread
From: Andreas Hindborg @ 2026-02-14  8:02 UTC (permalink / raw)
  To: Gary Guo
  Cc: Miguel Ojeda, Boqun Feng, Björn Roy Baron, Benno Lossin,
	Alice Ryhl, Trevor Gross, Danilo Krummrich, rust-for-linux,
	linux-kernel

Gary Guo <gary@garyguo.net> writes:

> On 2026-02-12 16:02, Andreas Hindborg wrote:
>> Add a method to return the underlying integer used for flags. This is
>> useful when using the flags with C APIs.
>> 
>> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
>
> I am not sure if we want to actually expose the underlying representation
> for all flags. It should be something up to the caller of `impl_flags` to
> decide.
>
> Would it make sense to add visibility the wrapped raw type so that you
> can do
>
> impl_flags! {
>     pub struct Foo(pub u32);
>     ...
> }
>
> to make the inner accessible?

I don't see how that is better?

                bindings::blk_mq_tag_set {
                    ...
                    flags: flags.into_inner(),
                    ...
                }

vs

                bindings::blk_mq_tag_set {
                    ...
                    flags: flags.0,
                    ...
                }


I would prefer the first one.

We are going to need to be able to pass these flags into C APIs. I feel
like adding a method to do this is the best way to go about this. We can
call it something else if that communicates the purpose better.

Best regards,
Andreas Hindborg



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

* Re: [PATCH] rust: impl_flags: add method to return underlying integer
  2026-02-14  8:02   ` Andreas Hindborg
@ 2026-02-14  9:36     ` Gary Guo
  2026-02-14 11:24       ` Andreas Hindborg
  0 siblings, 1 reply; 6+ messages in thread
From: Gary Guo @ 2026-02-14  9:36 UTC (permalink / raw)
  To: Andreas Hindborg
  Cc: Miguel Ojeda, Boqun Feng, Björn Roy Baron, Benno Lossin,
	Alice Ryhl, Trevor Gross, Danilo Krummrich, rust-for-linux,
	linux-kernel

On 2026-02-14 08:02, Andreas Hindborg wrote:
> Gary Guo <gary@garyguo.net> writes:
> 
>> On 2026-02-12 16:02, Andreas Hindborg wrote:
>>> Add a method to return the underlying integer used for flags. This is
>>> useful when using the flags with C APIs.
>>> 
>>> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
>>
>> I am not sure if we want to actually expose the underlying representation
>> for all flags. It should be something up to the caller of `impl_flags` to
>> decide.
>>
>> Would it make sense to add visibility the wrapped raw type so that you
>> can do
>>
>> impl_flags! {
>>     pub struct Foo(pub u32);
>>     ...
>> }
>>
>> to make the inner accessible?
> 
> I don't see how that is better?
> 
>                 bindings::blk_mq_tag_set {
>                     ...
>                     flags: flags.into_inner(),
>                     ...
>                 }
> 
> vs
> 
>                 bindings::blk_mq_tag_set {
>                     ...
>                     flags: flags.0,
>                     ...
>                 }
> 
> 
> I would prefer the first one.

How about we expand `($inner_vis:vis $ty:ty)` to be

    $inner_vis fn into_inner(self) -> $ty {}

? This way unless you explicitly put `pub` there, users outside the module
cannot query the bit pattern.

> 
> We are going to need to be able to pass these flags into C APIs. I feel
> like adding a method to do this is the best way to go about this. We can
> call it something else if that communicates the purpose better.

I think `bitflags` crate call this `bits`, which I think is indeed
slightly better than `into_inner`.

Best,
Gary

> 
> Best regards,
> Andreas Hindborg

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

* Re: [PATCH] rust: impl_flags: add method to return underlying integer
  2026-02-14  9:36     ` Gary Guo
@ 2026-02-14 11:24       ` Andreas Hindborg
  0 siblings, 0 replies; 6+ messages in thread
From: Andreas Hindborg @ 2026-02-14 11:24 UTC (permalink / raw)
  To: Gary Guo
  Cc: Miguel Ojeda, Boqun Feng, Björn Roy Baron, Benno Lossin,
	Alice Ryhl, Trevor Gross, Danilo Krummrich, rust-for-linux,
	linux-kernel

"Gary Guo" <gary@garyguo.net> writes:

> On 2026-02-14 08:02, Andreas Hindborg wrote:
>> Gary Guo <gary@garyguo.net> writes:
>>
>>> On 2026-02-12 16:02, Andreas Hindborg wrote:
>>>> Add a method to return the underlying integer used for flags. This is
>>>> useful when using the flags with C APIs.
>>>>
>>>> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
>>>
>>> I am not sure if we want to actually expose the underlying representation
>>> for all flags. It should be something up to the caller of `impl_flags` to
>>> decide.
>>>
>>> Would it make sense to add visibility the wrapped raw type so that you
>>> can do
>>>
>>> impl_flags! {
>>>     pub struct Foo(pub u32);
>>>     ...
>>> }
>>>
>>> to make the inner accessible?
>>
>> I don't see how that is better?
>>
>>                 bindings::blk_mq_tag_set {
>>                     ...
>>                     flags: flags.into_inner(),
>>                     ...
>>                 }
>>
>> vs
>>
>>                 bindings::blk_mq_tag_set {
>>                     ...
>>                     flags: flags.0,
>>                     ...
>>                 }
>>
>>
>> I would prefer the first one.
>
> How about we expand `($inner_vis:vis $ty:ty)` to be
>
>     $inner_vis fn into_inner(self) -> $ty {}
>
> ? This way unless you explicitly put `pub` there, users outside the module
> cannot query the bit pattern.

Sounds good. We might get some dead code warnings maybe?

>
>>
>> We are going to need to be able to pass these flags into C APIs. I feel
>> like adding a method to do this is the best way to go about this. We can
>> call it something else if that communicates the purpose better.
>
> I think `bitflags` crate call this `bits`, which I think is indeed
> slightly better than `into_inner`.

Sounds good.


Best regards,
Andreas Hindborg




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

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

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-12 16:02 [PATCH] rust: impl_flags: add method to return underlying integer Andreas Hindborg
2026-02-12 16:53 ` Daniel Almeida
2026-02-14  0:13 ` Gary Guo
2026-02-14  8:02   ` Andreas Hindborg
2026-02-14  9:36     ` Gary Guo
2026-02-14 11:24       ` Andreas Hindborg

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