Code Monkey home page Code Monkey logo

enumflags's Issues

Warning on nightly about implicit imports

extern crate enumflags;
#[macro_use]
extern crate enumflags_derive;

#[derive(EnumFlags, Copy, Clone)]
enum Foo {
    A = 1,
}
warning: cannot find type `Foo` in this scope
 --> src/main.rs:5:10
  |
5 | #[derive(EnumFlags, Copy, Clone)]
  |          ^^^^^^^^^ names from parent modules are not accessible without an explicit import
  |
  = note: #[warn(proc_macro_derive_resolution_fallback)] on by default
  = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  = note: for more information, see issue #50504 <https://github.com/rust-lang/rust/issues/50504>

Specifying bits using bitshifting

Currently constants cannot be specified using bit shifts.
I personally find it much more readable in almost every case.

The following does not compile:

#[repr(u8)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, EnumFlags)]
pub enum CtlCode {
    Banned        = 1 << 0,
    Administrator = 1 << 1,
    Common        = 1 << 2,
}

With the error:

At least one variant was not initialized explicity with a value.

Make missing flags detection optional

Some C/C++ APIs have missing flags reserved for things like future expansion. It would be nice if could use enumflags to wrap these kind interfaces without translating from a continuous enum without missing flags to the actual API.

Example:

#[derive(EnumFlags, Copy, Clone, Debug)]
#[repr(u8)]
pub enum Test {
//  A = 0b0001, MISSING
    B = 0b0010,
    D = 0b0100,
    A = 0b1000,
}

Support Zero (0) value for discriminant

I'm trying to represent ICMP Type field as an enum using EnumFlags. Type 0 is "Echo Reply".

If I try to represent this, however, it throws this compile time exception

 --> src/layer4/icmp.rs:6:10
  |
6 | #[derive(EnumFlags, Copy, Clone, Debug)]
  |          ^^^^^^^^^
  |
  = help: message: Null flag is not allowed

From this code:

#[derive(EnumFlags, Copy, Clone, Debug)]
#[repr(u8)]
pub enum IcmpType {
    EchoReply = 0,
    DestinationUnreachable = 3,
    SourceQuench = 4,
...

I'm trying to understand this logic, and whether it's strictly necessary. And what is the work-around when you're representing constants outside of your control (i.e. I can't just start at a value of 1 because if you cast back to the u8 value it will be incorrect)?

proc-macro derive panicked with #[repr(u64)]: assertion failed: index < std::u32::MAX as usize

Repro:

extern crate enumflags;
#[macro_use]
extern crate enumflags_derive;

#[derive(EnumFlags, Copy, Clone, Debug)]
#[repr(u64)]
enum Bork {
    X = 0x40_0000_0000
}

Error:

error: proc-macro derive panicked
 --> src/lib.rs:5:10
  |
5 | #[derive(EnumFlags, Copy, Clone, Debug)]
  |          ^^^^^^^^^
error: proc-macro derive panicked
 --> src/lib.rs:5:10
  |
5 | #[derive(EnumFlags, Copy, Clone, Debug)]
  |          ^^^^^^^^^
  |
  |
  = help: message: assertion failed: index < std::u32::MAX as usize
  = help: message: assertion failed: index < std::u32::MAX as usize

Multiple Bits for Flag

#[derive(EnumFlags, Copy, Clone, Debug)]
#[repr(u8)]
pub enum GeneralStatus {
    INDICATOR_IDENTIFY    = 0b01000000,
    INDICATOR_MUTE        = 0b10000000,
    INDICATOR_NORMAL      = 0b11000000,

    PORT_AUTH_PANEL       = 0b00010000,
    PORT_AUTH_NETWORK     = 0b00100000,

    BOOT_ROM              = 0b00000100,
    RDM_CAPABLE           = 0b00000010,
    UBEA_PRESENT          = 0b00000001,
}

Gives the error:

The following flags are not unique: ["GeneralStatus::INDICATOR_IDENTIFY = 0b1000000", "GeneralStatus::INDICATOR_MUTE = 0b10000000", "GeneralStatus::IND
ICATOR_NORMAL = 0b11000000"]

Is this not possible? I can't change how these bits are laid out as they are part of an existing network protocol.

Feature request: Add a macro to specify multiple flags without repeating the type name

While using this crate, I found it rather useful to define a macro like this:

// public domain
macro_rules! flags {
    ( $($ns:ident::)* {} ) => (
        $($ns::)*empty_bitflag()
    );

    ( $($ns:ident::)* {$tail:ident} ) => (
        $crate::BitFlags::from($($ns::)*$tail)
    );

    // Use `{}` instead of `()` to avoid `unused_parens` warning
    ( $($ns:ident::)* {$head:ident | $($rest:tt)*} ) => ({
        flags![$($ns::)*{$head}] |
        flags![$($ns::)*{$($rest)*}]
    })
}

The usage is shown below:

#[derive(EnumFlags, Copy, Clone, PartialEq, Eq, Hash, Debug)]
#[repr(u8)]
pub enum Test { A = 0b0001, B = 0b0010 }

let flags0 = flags![Test::{}];
let flags1 = flags![Test::{A}];
let flags2 = flags![Test::{A | B}];

assert_eq!(flags0, enumflags::BitFlags::empty());
assert_eq!(flags1, Test::A.into());
assert_eq!(flags2, Test::A | Test::B);

Perhaps could you consider including this macro in enumflags?

support type aliases

rust-lang/rust#10374

Because of that bug, we can't really use enumflags when we need to serialize to an architecture dependent size. For example: std::os::raw::c_ulong. We can either try to get it fixed in the compiler or we can try to work around it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.