Re: [PATCH v8] rust: add new macro for common bitflag operations

From: Daniel Almeida

Date: Thu Jan 15 2026 - 08:39:59 EST




> On 15 Jan 2026, at 09:09, Andreas Hindborg <a.hindborg@xxxxxxxxxx> wrote:
>
> "Filipe Xavier" <felipeaggger@xxxxxxxxx> writes:
>
>> We have seen a proliferation of mod_whatever::foo::Flags
>> being defined with essentially the same implementation
>> for BitAnd, BitOr, contains and etc.
>>
>> This macro aims to bring a solution for this,
>> allowing to generate these methods for user-defined structs.
>> With some use cases in KMS and upcoming GPU drivers.
>>
>> Link: https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/topic/We.20really.20need.20a.20common.20.60Flags.60.20type
>> Suggested-by: Daniel Almeida <daniel.almeida@xxxxxxxxxxxxx>
>> Suggested-by: Lyude Paul <lyude@xxxxxxxxxx>
>> Reviewed-by: Daniel Almeida <daniel.almeida@xxxxxxxxxxxxx>
>> Reviewed-by: Lyude Paul <lyude@xxxxxxxxxx>
>> Signed-off-by: Filipe Xavier <felipeaggger@xxxxxxxxx>
>
> Tested-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
> Reviewed-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
>
>
> I think it would be useful to add:
>
> impl Flags {
> unsafe from_raw(value: Repr) -> {
> ...
> }
> }
>
> impl TryFrom<Repr> for Flag {
> // Succeed if `value` is a valid `Flag` enum bit pattern.
> }
>
> impl TryFrom<Repr> for Flags {
> // Succeed if `value` is the logical OR of valid enum bit patterns.
> }
>
>
>
> Best regards,
> Andreas Hindborg

Ah, true. There is a use-case for that in Tyr as well where we ingest the raw
values from userspace and need to validate and build valid flags from. This may
be somewhat common for others too.