rust: impl_flags: add method to return underlying integer
Andreas Hindborg
reviewerPATCH 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
@@ -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]>
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.
Signed-off-by Andreas Hindborg <[email protected]>---
rust/kernel/impl_flags.rs| 6 ++++++1 file changed, 6 insertions(+)rust/kernel/impl_flags.rs +6
-1@@ -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]>
Gary Guo
reviewerQuoted 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.
Signed-off-by Andreas Hindborg <[email protected]>
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@@ -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-c61974b27b18Best regards,
Andreas Hindborg
reviewerQuoted 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.
Signed-off-by Andreas Hindborg <[email protected]>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_flagsto 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
Gary Guo
reviewerQuoted 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.
Signed-off-by Andreas Hindborg <[email protected]>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_flagsto 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
Andreas Hindborg
reviewerQuoted 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.
Signed-off-by Andreas Hindborg <[email protected]>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_flagsto 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
pubthere, 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
bitflagscrate call thisbits, which I think is indeed slightly better thaninto_inner.
Sounds good.
Best regards,
Andreas Hindborg