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

Can not have an impl block for BitFlags<MyFlags> #61

Open
de-vri-es opened this issue Oct 14, 2024 · 0 comments
Open

Can not have an impl block for BitFlags<MyFlags> #61

de-vri-es opened this issue Oct 14, 2024 · 0 comments

Comments

@de-vri-es
Copy link

Because the BitFlags type comes from the enumflags2 crate, we can not add our own impl blocks for it or impl traits for it.

Would you be interested in an additional macro that allows you to define your own BitFlags type for a specific BitFlag enum?

#[bitflags]
#[repr(u8)]
#[derive(Copy, Clone, Debug, PartialEq)]
enum Flags {
    A = 0b0001,
    B = 0b0010,
    C, // unspecified variants pick unused bits automatically
    D = 0b1000,
}

#[flagset(Flags)]
struct Status;

// Yaay, we can do this:
impl Status {
  pub fn priority(&self) -> u8 {
     if self.contains(Flags::A) {
       1
     } else {
       2
     }
  }
}
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