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 use generated bitflags to initialise a const #180
Comments
So this is a little unfortunate. It seems like a limitation in Rust's const evaluation where operators on primitive types are specially understood as being available in a constant context. We can't mark our One workaround is to do something like this: const SECOND_SEAL: FLAGS = FLAGS::from_bits_truncate(FLAGS::MAMMAL.bits() | FLAGS::AQUATIC.bits()); |
Thanks. I wondered whether it was possible to mark |
If it is a Rust limitation, should an issue about it be reported on their github? |
There's some experimental work to support const trait impls that should enable using bitops here: rust-lang/rust#67792 but it sounds like there's still a lot of work to be done before that will reach us. In the meantime, we just merged in #244 which includes some nicer const set operators that avoids the need to wrap integers. So your second example could become: const SECOND_SEAL: FLAGS = FLAGS::MAMMAL.union(FLAGS::AQUATIC); |
This works in plain Rust
but the equivalent when using bitflags does not
You get an error error[E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants when trying to BITOR the two flags.
It would be nice if it did, because in my program I have a large initial array of flags to create and there are many different combinations making it impractical to spell them all out inside the
bitflags!
call.Edit: Simplified the repro.
The text was updated successfully, but these errors were encountered: