Add a method to get the unknown bits from a flags value #442

Closed
opened 2025-04-16 22:54:55 -05:00 by FuuuOverclocking · 4 comments
FuuuOverclocking commented 2025-04-16 22:54:55 -05:00 (Migrated from github.com)

I recommend to provide the following API to users:

    use bitflags::{bitflags, Flags};
    
    pub struct SomeFlags(u32);
    bitflags! {
        impl SomeFlags: u32 {
            const A = 1 << 0;
            const B = 1 << 1;
        }
    }

    let flags = SomeFlags::from_bits_retain(0b100);
    if !flags.unknown_bits().empty() {
        bail!("unknown bits: {:?}", flags.unknown_bits());
    }

Usually, when we found the unknown bits, we want to report them.

I recommend to provide the following API to users: ```rs use bitflags::{bitflags, Flags}; pub struct SomeFlags(u32); bitflags! { impl SomeFlags: u32 { const A = 1 << 0; const B = 1 << 1; } } let flags = SomeFlags::from_bits_retain(0b100); if !flags.unknown_bits().empty() { bail!("unknown bits: {:?}", flags.unknown_bits()); } ``` Usually, when we found the unknown bits, we want to report them.
rusty-snake commented 2025-04-24 12:14:24 -05:00 (Migrated from github.com)

@FuuuOverclocking can you further clarify why the already provided contains_unknown_bits is not enough for you or what additions (bail! macro?) you want.

@FuuuOverclocking can you further clarify why the already provided [`contains_unknown_bits`](https://docs.rs/bitflags/latest/bitflags/trait.Flags.html#method.contains_unknown_bits) is not enough for you or what additions (`bail!` macro?) you want.
FuuuOverclocking commented 2025-04-24 22:47:53 -05:00 (Migrated from github.com)

Hi RS! We've noticed a common need: when a function gets some unknown bits passed in, it wants to tell users exactly which bits aren't supported in this version, making the problem clear. While contains_unknown_bits can tell us that there are unknown bits in there, it can't tell us which ones. That's why we think adding an unknown_bits() method would be really helpful. Once we have unknown_bits(), we can actually implement contains_unknown_bits as !unknown_bits().empty(), making it a combination of basic methods.

By the way, bail! is an anyhow macro that's used for returning errors.

Hi RS! We've noticed a common need: when a function gets some unknown bits passed in, it wants to tell users exactly which bits aren't supported in this version, making the problem clear. While [contains_unknown_bits](https://docs.rs/bitflags/latest/bitflags/trait.Flags.html#method.contains_unknown_bits) can tell us that there are unknown bits in there, it can't tell us which ones. That's why we think adding an `unknown_bits()` method would be really helpful. Once we have `unknown_bits()`, we can actually implement `contains_unknown_bits` as `!unknown_bits().empty()`, making it a combination of basic methods. By the way, `bail!` is an anyhow macro that's used for returning errors.
rusty-snake commented 2025-04-25 00:15:57 -05:00 (Migrated from github.com)
flags.bits() & !Flags::all().bits()
```rust flags.bits() & !Flags::all().bits() ```
KodrAus commented 2025-05-14 16:32:09 -05:00 (Migrated from github.com)

I think a pair of methods on the Flags trait that complement the existing bits method would be ok here:

fn known_bits(&self) -> Self::Bits { .. }
fn unknown_bits(&self) -> Self::Bits { .. }

I'd be in favour of a PR that added those in 👍

I think a pair of methods on the `Flags` trait that complement the existing `bits` method would be ok here: ```rust fn known_bits(&self) -> Self::Bits { .. } fn unknown_bits(&self) -> Self::Bits { .. } ``` I'd be in favour of a PR that added those in :+1:
Sign in to join this conversation.
No labels
bug
good first issue
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-bitflags/bitflags#442
No description provided.