Skip to content

Rust library for typesystem-assisted bitflags.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

meithecatte/enumflags2

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LICENSE LICENSE Documentation Crates.io Version

Enumflags

Usage

In your Cargo.toml:

[dependencies]
enumflags2 = "^0.5"
enumflags2_derive = "^0.5"

If using the 2015 Rust edition, add this to your crate root:

extern crate enumflags2;
#[macro_use]
extern crate enumflags2_derive;

Features

  • Uses enums to represent individual flags—a set of flags is a separate type from a single flag.
  • Detects incorrect BitFlags at compile time.
    • Non-unique bits.
    • Missing values.
    • Flags larger than the chosen repr.
  • Has a similar API compared to the popular bitflags crate.
  • Does not expose the generated types explicity. The user interacts exclusively with struct BitFlags<Enum>;.
  • The debug formatter prints the binary flag value as well as the flag enums: BitFlags(0b1111, [A, B, C, D]).

Example

extern crate enumflags2;
#[macro_use]
extern crate enumflags2_derive;

use enumflags2::BitFlags;

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

fn main() {
    let a_b = Test::A | Test::B; // BitFlags<Test>
    let a_c = Test::A | Test::C;
    let b_c_d = Test::C | Test::B | Test::D;

    // BitFlags<Test>(0b11, [A, B])
    println!("{:?}", a_b);

    // BitFlags<Test>(0b1, [A])
    println!("{:?}", a_b & a_c);

    // Iterate over the flags like a normal set!
    assert_eq!(a_b.iter().collect::<Vec<_>>(), &[Test::A, Test::B]);

    assert!(a_b.contains(Test::A));
    assert!(b_c_d.contains(Test::B | Test::C));
    assert!(!(b_c_d.contains(a_b)));

    assert!(a_b.intersects(a_c));
    assert!(!(a_b.intersects(Test::C | Test::D)));
}

About

Rust library for typesystem-assisted bitflags.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages