[][src]Macro flagset::flags

macro_rules! flags {
    () => { ... };
    ($(#[$m:meta])* $p:vis enum $n:ident: $t:ty { $($(#[$a:meta])* $k:ident),+ $(,)* } $($next:tt)*) => { ... };
    ($(#[$m:meta])* $p:vis enum $n:ident: $t:ty { $($(#[$a:meta])*$k:ident = $v:expr),+ $(,)* } $($next:tt)*) => { ... };
}

Define flag value using the enum syntax. See below for details.

Each enumeration value MUST have a specified value.

The width of the bitfield MUST also be specified by its integer type.

It is important to note that the size of the flag enumeration itself is unrelated to the size of the corresponding FlagSet instance.

It is also worth noting that this macro automatically implements a variety of standard traits including:

  • Copy
  • Clone
  • Debug
  • PartialEq
  • Eq
  • From<$enum> for $integer
  • Not
  • BitAnd
  • BitOr
  • BitXor
  • Sub
  • Rem
use std::mem::{align_of, size_of};
use flagset::{FlagSet, flags};

flags! {
    enum Flag8: u8 {
        Foo = 0b001,
        Bar = 0b010,
        Baz = 0b100
    }

    pub enum Flag16: u16 {
        Foo,
        Bar,
        #[deprecated]
        Baz,
    }

    #[derive(PartialOrd, Ord)]
    enum Flag32: u32 {
        Foo = 0b001,
        #[deprecated]
        Bar = 0b010,
        Baz = 0b100
    }

    #[repr(u64)]
    enum Flag64: u64 {
        Foo = 0b001,
        Bar = 0b010,
        Baz = 0b100
    }

    #[repr(u32)]
    enum Flag128: u128 {
        Foo = 0b001,
        Bar = 0b010,
        Baz = 0b100
    }
}

assert_eq!(size_of::<Flag8>(), 1);
assert_eq!(size_of::<Flag16>(), 1);
assert_eq!(size_of::<Flag32>(), 1);
assert_eq!(size_of::<Flag64>(), 8);
assert_eq!(size_of::<Flag128>(), 4);

assert_eq!(align_of::<Flag8>(), 1);
assert_eq!(align_of::<Flag16>(), 1);
assert_eq!(align_of::<Flag32>(), 1);
assert_eq!(align_of::<Flag64>(), align_of::<u64>());
assert_eq!(align_of::<Flag128>(), align_of::<u32>());

assert_eq!(size_of::<FlagSet<Flag8>>(), size_of::<u8>());
assert_eq!(size_of::<FlagSet<Flag16>>(), size_of::<u16>());
assert_eq!(size_of::<FlagSet<Flag32>>(), size_of::<u32>());
assert_eq!(size_of::<FlagSet<Flag64>>(), size_of::<u64>());
assert_eq!(size_of::<FlagSet<Flag128>>(), size_of::<u128>());

assert_eq!(align_of::<FlagSet<Flag8>>(), align_of::<u8>());
assert_eq!(align_of::<FlagSet<Flag16>>(), align_of::<u16>());
assert_eq!(align_of::<FlagSet<Flag32>>(), align_of::<u32>());
assert_eq!(align_of::<FlagSet<Flag64>>(), align_of::<u64>());
assert_eq!(align_of::<FlagSet<Flag128>>(), align_of::<u128>());