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

Using bitflags in static structs #210

Closed
naim94a opened this issue Jan 31, 2020 · 3 comments
Closed

Using bitflags in static structs #210

naim94a opened this issue Jan 31, 2020 · 3 comments

Comments

@naim94a
Copy link

naim94a commented Jan 31, 2020

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
Copy link
Author

naim94a commented Feb 2, 2020

I'm guessing this issue is blocked on rust-lang/rfcs#2632...

@Anders429
Copy link

Anders429 commented Mar 28, 2021

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

@KodrAus
Copy link
Member

KodrAus commented Mar 13, 2023

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 KodrAus closed this as completed Mar 13, 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

Successfully merging a pull request may close this issue.

3 participants