Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More methods to work with uncorresponding bits #327

Open
rusty-snake opened this issue Apr 2, 2023 · 6 comments
Open

More methods to work with uncorresponding bits #327

rusty-snake opened this issue Apr 2, 2023 · 6 comments

Comments

@rusty-snake
Copy link
Contributor

  • complement with from_bits_retain
    FlagsT::from_bits_retain(!FlagsT::all().bits())
  • contains_uncorresponding_bits or has_uncorresponding_bits
@KodrAus
Copy link
Member

KodrAus commented Apr 11, 2023

Hi @rusty-snake 👋 Are you able to share any more details on what you're trying to do here?

@rusty-snake
Copy link
Contributor Author

Yes, my use case are flags of ffi functions where I can only allow known safe flags. Passing known unsafe or flags added by future versions might be unsafe. Instead of silent truncating the bits I want to check and return an error.

@KodrAus
Copy link
Member

KodrAus commented Apr 12, 2023

Ahh, so you're suggesting a function to detect whether or not there are any bits set that don't correspond to a flag? Would it be enough to write?

let bits = Flags::from_bits(possibly_invalid.bits()).ok_or_else(|| "contains unrecognised bits")?;

@rusty-snake
Copy link
Contributor Author

Yes, that would be an alternative way to implement it.

@KodrAus
Copy link
Member

KodrAus commented Jun 27, 2023

Hi @rusty-snake. Just calling out that 2.3.3 slightly alters the behavior of the ! operator that affects flags with bits that only partially correspond to a known flag. I'm not sure if that will affect you, but wanted to draw your attention to it.

In #363 (comment) we're trying to come up with a fully consistent treatment of unknown bits, which might become a 3.0.0 release. That would let us include some new inherent methods for detecting and truncating them if you wish.

@KodrAus
Copy link
Member

KodrAus commented Mar 20, 2024

In #400 we added strict methods to the parser and formatter to work with flags that only want bit patterns that fully contain some number of defined flags. We could consider adding from_bits_strict as a variant that's even tighter than from_bits and from_bits_truncate. The difference only matters for multi-bit flags, but is outlined here.

I've also found myself wanting a .truncate() inherent method on flags types too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants