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

const_bitflag_or macro to enable const bitflags before const traits are available #223

Closed
joshtriplett opened this issue Jul 27, 2020 · 3 comments

Comments

@joshtriplett
Copy link

Until const traits become usable on stable, it'd be nice to have a macro along the lines of const_bitflag_or, which would expand to use Flags::NAME.bits() and Flags::from_bits_truncate:

const_bitflag_or!(Flags, A, B, C)
would become
Flags::from_bits_truncate(Flags::A.bits() | Flags::B.bits() | Flags::C.bits())

@Evian-Zhang
Copy link

If we write const_bitflags_or as (since the delimiter can be anything):

macro_rules! const_bitflags_or {
    ( $Flags: ty, $( $flag: ident )|+ ) => {
        <$Flags>::from_bits_truncate(0 $(| <$Flags>::$flag.bits())+)
    };
}

Then we can use it like

const_bitflags_or!(Flags, A | B | C);

which may be more graceful.

@KodrAus
Copy link
Member

KodrAus commented Mar 13, 2023

Ideally we'll be able to just mark those traits as const when #![feature(const_trait_impl)] stabilizes. In the meantime, you can do:

Flags::A.union(Flags::B).union(Flags::C)

in const evaluation.

@KodrAus
Copy link
Member

KodrAus commented Apr 11, 2023

Just coming back through some triage. I think this isn't something we're going to support in bitflags through a macro, but will absolutely jump on const_trait_impl once it stabilizes. Thanks for the input everyone!

@KodrAus KodrAus closed this as completed Apr 11, 2023
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

3 participants