Using bitflags in static structs
I need to develop a plugin for a program, which requires me to export a certain structure which contains a bitfield. When exporting the struct with bitflags I get the following error:
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
--> src/lib.rs:18:12
|
18 | flags: PluginFlags::A | PluginFlags::C,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I would prefer to use this crate over plain constants... Is there a way to solve this?
code to reproduce:
use bitflags::bitflags; // 1.2.1
bitflags!{
pub struct PluginFlags: u32 {
const None = 0;
const A = 0x0001;
const B = 0x0002;
const C = 0x0004;
}
}
pub struct Plugin {
flags: PluginFlags,
}
#[no_mangle]
static DATA_EXPORT: Plugin = Plugin {
flags: PluginFlags::A | PluginFlags::C,
};
I'm guessing this issue is blocked on https://github.com/rust-lang/rfcs/pull/2632...
For anyone who ended up on this issue, the "hacky" solution provided here works.
Let's track support for const traits that will let bit operators to work on flags types over in #223.
The sort of thing we need to be able to do is:
impl const BitOr for $BitFlags {
..
}
which is gated by #![feature(const_trait_impl)].