Skip to contentSkip to conversation

rust: impl_flags: add method to return underlying integer

PATCH Superseded Andreas Hindborg 3 participants 6 messages Feb 12 – Feb 14 Rust

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Andreas Hindborg

author reviewer
#0 lore raw

PATCH 1/1 rust: impl_flags: add method to return underlying integer

Add a method to return the underlying integer used for flags. This is useful when using the flags with C APIs.

---
 rust/kernel/impl_flags.rs | 6 ++++++
 1 file changed, 6 insertions(+)
rust/kernel/impl_flags.rs +6 -0
index e2bd7639da12e..951598497de1d 100644
@@ -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,

Signature · 1 line
Andreas Hindborg <[email protected]>
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 35 lines · Andreas Hindborg

Add a method to return the underlying integer used for flags. This is useful when using the flags with C APIs.

---
rust/kernel/impl_flags.rs | 6 ++++++

1 file changed, 6 insertions(+)
rust/kernel/impl_flags.rs +6 -1
index e2bd7639da12e..951598497de1d 100644
@@ -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

Quoted 6 lines

Best regards,
--
Andreas Hindborg <[email protected]>

reviewer
#2 lore raw
In reply to Andreas Hindborg
Quoted 4 lines · Andreas Hindborg

Add a method to return the underlying integer used for flags. This is useful when using the flags with C APIs.

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

Quoted 27 lines
---
 rust/kernel/impl_flags.rs | 6 ++++++
 1 file changed, 6 insertions(+)
rust/kernel/impl_flags.rs +6 -1
index e2bd7639da12e..951598497de1d 100644
@@ -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,

author reviewer
#3 lore raw
Quoted 13 lines · Gary Guo
Quoted 4 lines · Andreas Hindborg

Add a method to return the underlying integer used for flags. This is useful when using the flags with C APIs.

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

Quoted 6 lines
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

reviewer
#4 lore raw
Quoted 32 lines · Andreas Hindborg
Quoted 13 lines · Gary Guo
Quoted 4 lines · Andreas Hindborg

Add a method to return the underlying integer used for flags. This is useful when using the flags with C APIs.

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

Quoted 6 lines
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

Quoted 8 lines
                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.

Quoted 4 lines

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

Quoted 3 lines

Best regards,
Andreas Hindborg

author reviewer
#5 lore raw
Quoted 40 lines · Gary Guo
Quoted 39 lines · Andreas Hindborg
Quoted 13 lines · Gary Guo
Quoted 4 lines · Andreas Hindborg

Add a method to return the underlying integer used for flags. This is useful when using the flags with C APIs.

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

Quoted 6 lines
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,
                    ...
                }
Quoted 8 lines
Quoted 1 line

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?

Quoted 8 lines
Quoted 5 lines

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