Is it possible to refer to other flags within an enum using enumflags2?
Closed this issue · 2 comments
fgimian commented
Hey there, thanks so much for the great library.
I have searched through the various examples in the codebase and can't seem to find a way to do something similar to that below (using bitflags for demonstration):
bitflags! {
struct Flags: u8 {
const A = 1;
const B = 1 << 1;
const AB = Flags::A.bits() | Flags::B.bits(); // Is this possible with enumflags2?
}
}
Thanks heaps
Fotis
meithecatte commented
You could write it like this:
#[bitflags]
#[derive(Clone, Copy)]
#[repr(u8)]
enum Flags {
A = 1 << 0, // or omit the values if you don't care about the exact bits used
B = 1 << 1,
}
impl Flags {
const AB: BitFlags<Self> = make_bitflags!(Self::{A | B});
}
fgimian commented
You could write it like this:
#[bitflags] #[derive(Clone, Copy)] #[repr(u8)] enum Flags { A = 1 << 0, // or omit the values if you don't care about the exact bits used B = 1 << 1, } impl Flags { const AB: BitFlags<Self> = make_bitflags!(Self::{A | B}); }
Ah beautiful, thank you so much for your help. I'll close this issue now.
Cheers
Fotis