bitflags icon indicating copy to clipboard operation
bitflags copied to clipboard

Using bitflags in static structs

Open naim94a opened this issue 5 years ago • 2 comments

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,
};

naim94a avatar Jan 31 '20 14:01 naim94a

I'm guessing this issue is blocked on https://github.com/rust-lang/rfcs/pull/2632...

naim94a avatar Feb 02 '20 15:02 naim94a

For anyone who ended up on this issue, the "hacky" solution provided here works.

Anders429 avatar Mar 28 '21 17:03 Anders429

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)].

KodrAus avatar Mar 13 '23 23:03 KodrAus