I know we have the crate bitflags, but I personally think this is slightly inconvenient.
Would it be possible to have (like I was used to in C#) to have native support for enum flags in the language?
This would enable and and or constructions as well as contains() etc.
Of course only possible if the enum is boiling down to a simple unsigned int value.
(BTW: not sure if this should be posted here or on the other forum)
I did not study bitflags in depth.
But something like this, from the top of my head things like this.
#[bitflags]
#[repr(u8)]
enum Test
{
Nothing = 0,
One = 1,
Two = 2,
Three = 4,
Four = 8,
OneTwo = Self::One | Self::Two,
};
let t: Test = Test::One | Test::Two;
if (t.contains(Test::Two)) {}
Modulo slight syntax differences, this is pretty much what the crate does: bitflags - Rust
bitflags! {
pub struct Flags: u32 {
const A = 0b00000001;
const B = 0b00000010;
const C = 0b00000100;
}
}
// union
let ab = Flags::A | Flags::B;
// intersection
let a = ab & Flags::A;
// difference
let b = ab - Flags::A;
// complement
let c = !ab;
I also wrote a crate (which I've never pushed to crates.io yet)
which has some of this stuff, and other things like NonZeroRepr for enums...
It is still missing some features though...
Unlike bitflags it is all based on derive macros on enums...
Hasn't seen much work recently but i'm happy to put some more effort into it,
if there is interest...