enum-flags

Crates.ioenum-flags
lib.rsenum-flags
version0.3.0
sourcesrc
created_at2021-07-29 12:22:53.627467
updated_at2022-02-25 16:11:49.552338
descriptionEnumFlags is a csharp like enum flags implementation.
homepagehttps://github.com/mokeyish/enum-flags
repositoryhttps://github.com/mokeyish/enum-flags
max_upload_size
id428747
size26,170
YISH (mokeyish)

documentation

https://docs.rs/enum-flags

README

LICENSE Documentation Crates.io Version

EnumFlags

EnumFlags is a csharp like enum flags implementation.

Example

Since use the feature arbitrary_enum_discriminant, nightly channel is required.

#![feature(arbitrary_enum_discriminant)]
use enum_flags::enum_flags;

#[repr(u8)]  // default: #[repr(usize)]
#[enum_flags]
#[derive(Copy, Clone, PartialEq)] // can be omitted
enum Flags{
    None = 0,
    A = 1,
    B, // 2
    C = 4
}
fn main() {
    let e1: Flags = Flags::A | Flags::C;
    let e2 = Flags::B | Flags::C;

    assert_eq!(e1 | e2, Flags::A | Flags::B | Flags::C); // union
    assert_eq!(e1 & e2, Flags::C); // intersection
    assert_eq!(e1 ^ e2, Flags::A | Flags::B); // toggle
    assert_eq!(e1 & (!Flags::C), Flags::A); // deletion
    assert_eq!(e1 - Flags::C, Flags::A); // deletion

    assert_eq!(format!("{:?}", e1).as_str(), "(Flags::A | Flags::C)");
    assert!(e1.has_a());
    assert!(!e1.has_b());
    assert!(e1.has_flag(Flags::C));
    assert!(e1.contains(Flags::C));
    assert_eq!(match Flags::A | Flags::C {
        Flags::None => "None",
        Flags::A => "A",
        Flags::B => "B",
        Flags::C => "C",
        Flags::__Composed__(v) if v == Flags::A | Flags::B => "A and B",
        Flags::__Composed__(v) if v == Flags::A | Flags::C => "A and C",
        _ => "Others"
    }, "A and C")
}

Breaking Changes

  • before version 0.18
    #[derive(Copy, EnumFlags, Clone, PartialEq)]
    enum Flags {
        None = 0,
        A = 1,
        B = 2,
        C = 4
    }
    
  • version 0.30 later
    #[enum_flags] 
    #[derive(Copy, Clone, PartialEq)]
    enum Flags {
        None = 0,
        A = 1,
        B = 2,
        C = 4
    }
    
Commit count: 15

cargo fmt