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

is_all() vs. from_bits_unchecked() #208

Closed
niklasf opened this issue Jan 22, 2020 · 0 comments
Closed

is_all() vs. from_bits_unchecked() #208

niklasf opened this issue Jan 22, 2020 · 0 comments

Comments

@niklasf
Copy link
Contributor

niklasf commented Jan 22, 2020

unsafe from_bits_unchecked() allows creating instances with extra bits. The caller of the bitflags! macro can decide if this is allowed for their type. Let's assume it is for Example. I checked the provided methods for surprising interactions with extra bits, and found (only) this:

is_all() returns false when there are more than "all" flags. This does not match the documentation:

Returns true if all flags are currently set.

Should we update the documentation or the implementation?


use bitflags::bitflags;

bitflags! {
    struct Example: u32 {
        const A = 1;
    }
}

fn main() {
    unsafe {
        assert!(Example::from_bits_unchecked(1).is_all()); // true
        assert!(Example::from_bits_unchecked(3).is_all()); // false
    }
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=cda2672387dd0ff4ba629b1317a9c57c

@niklasf niklasf changed the title is_all() vs. from_flags_unchecked() is_all() vs. from_bits_unchecked() Jan 22, 2020
KodrAus added a commit that referenced this issue May 16, 2021
Let is_all() ignore extra bits (fixes #208)
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

1 participant