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't store an or-ing of Bitflags in a const or static #243

Closed
sam0x17 opened this issue May 23, 2021 · 4 comments
Closed

can't store an or-ing of Bitflags in a const or static #243

sam0x17 opened this issue May 23, 2021 · 4 comments

Comments

@sam0x17
Copy link

sam0x17 commented May 23, 2021

const LEFT_SHARED_MASK: Buttons4 = Buttons4::MINUS | Buttons4::L_STICK | Buttons4::CAPTURE;

results in:

error[E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants
   --> src\main.rs:151:36
    |
151 | const LEFT_SHARED_MASK: Buttons4 = Buttons4::MINUS | Buttons4::L_STICK | Buttons4::CAPTURE;
    |                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@sam0x17
Copy link
Author

sam0x17 commented Jun 3, 2021

btw I got around this using the lazy_static crate

@nickelc
Copy link

nickelc commented Jun 22, 2021

You can use Flags::from_bits_truncate and Flags::A.bits or Flags::A.bits() for this.

const MASK: Flags = Flags::from_bits_truncate(Flags::A.bits | Flags::B.bits | Flags::C.bits);
const MASK: Flags = Flags::from_bits_truncate(Flags::A.bits() | Flags::B.bits() | Flags::C.bits());

@KodrAus
Copy link
Member

KodrAus commented Jul 19, 2021

We've also got #244 currently open that would make this a little nicer to write until we can use | in const:

const LEFT_SHARED_MASK: Buttons4 = Buttons4::MINUS.union(Buttons4::L_STICK).union(Buttons4::CAPTURE);

@KodrAus
Copy link
Member

KodrAus commented Aug 11, 2021

We've just released 1.3.0 that supports the above syntax. The issue of being able to use real operators will be blocked upstream for a while, so I'll go ahead and close this one, but one day we should be able to support it!

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