GithubHelp home page GithubHelp logo

bitflag-attr's Introduction

bitflag-attr

Rust Latest version Documentation License

This is a proc-macro Rust crate that allows to turn a C-like enum into a bitflag structures with an API similar to bitfields crate.

You can use this crate to:

  • provide more user-friendly bindings to C APIs where flags may or may not be fully known in advance.

You can't use this crate to:

  • guarantee only bits corresponding to defined flags will ever be set. bitflag-attr allows access to the underlying bits type so arbitrary bits may be set.
  • define bitfields. bitflag-attr only generates types where set bits denote the presence of some combination of flags.

Implemented traits

The macro will also implement some traits for bitwise operations and formatting.

  • core::ops::Not
  • core::ops::BitAnd
  • core::ops::BitOr
  • core::ops::BitXor
  • core::ops::BitAndAssign
  • core::ops::BitOrAssign
  • core::ops::BitXorAssign
  • core::ops::Sub
  • core::ops::SubAssign
  • core::fmt::Debug
  • core::fmt::Binary
  • core::fmt::UpperHex
  • core::fmt::LowerHex
  • core::fmt::Octal
  • From
  • Clone
  • Copy

Besides the Debug, Clone and Copy traits, all other derivable traits can be used together with the type

Example

Generate a flags structure

use bitflag_attr::bitflag;

#[bitflag(u32)]
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
enum Flags {
    /// The value `A`, at bit position `0`.
    A = 0b00000001,
    /// The value `B`, at bit position `1`.
    B = 0b00000010,
    /// The value `C`, at bit position `2`.
    C = 0b00000100,

    /// The combination of `A`, `B`, and `C`.
    ABC = A | B | C,
}

fn main() {
    let e1 = Flags::A | Flags::C;
    let e2 = Flags::B | Flags::C;
    assert_eq!((e1 | e2), Flags::ABC);   // union
    assert_eq!((e1 & e2), Flags::C);     // intersection
    assert_eq!((e1 - e2), Flags::A);     // set difference
    assert_eq!(!e2, Flags::A);           // set complement
}

Rust Version Support

The minimum supported Rust version is documented in the Cargo.toml file. This may be bumped in minor releases as necessary.

bitflag-attr's People

Contributors

grayjack avatar

Watchers

 avatar

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.