GithubHelp home page GithubHelp logo

bitbag's Introduction

crates-io docs-rs github

bitbag

This crate provides [BitBag], a type intended for tracking bitflags defined in a field-less enum. Get started like this:

use bitbag::{BitBag, BitBaggable};
use strum::EnumIter;

#[derive(BitBaggable, EnumIter, Clone, Copy)]
#[repr(u8)]
enum Flags {
    A = 0b0001,
    B = 0b0010,
    C = 0b0100,
}

Basic functionality is provided by [BitBag]

let mut bag = BitBag::<Flags>::new_unchecked(0b0011);
assert!(bag.is_set(Flags::A));
assert!(bag.is_set(Flags::B));
assert!(!bag.is_set(Flags::C));

bag.set(Flags::C);
assert_eq!(*bag, 0b0111);

Deriving [BitBaggable] will also give you very ergonomic constructors

use Flags::*;
let bag = A | B | C;
assert!(bag.is_set(Flags::A));
assert!(bag.is_set(Flags::B));
assert!(bag.is_set(Flags::C));

Additionally deriving [EnumIter], and [Copy] will allow fallible creation, and iteration over the set flags

//                                  ⬇ this bit is not defined in Flags
let result = BitBag::<Flags>::new(0b1000);
assert!(matches!(
    result,
    Err(e) if e.given() == 0b1000
));

let bag = BitBag::<Flags>::new_unchecked(0b0110);
for flag in &bag {
    match flag {
        Flags::A => println!("Flag A was set"),
        Flags::B => println!("Flag B was set"),
        Flags::C => println!("Flag C was set"),
    }
};

bitbag's People

Contributors

aatifsyed avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

bitbag's Issues

Compilation error on 1.50.0

Interesting crate! When attempting to run the example from the reddit post, I get the error:

jon@r4 ~/src/oss/bitbag_test $ cargo run
   Compiling bitbag v0.1.4
error[E0391]: cycle detected when computing the supertraits of `BitBaggable`
   --> /home/jon/.cargo/registry/src/github.com-1ecc6299db9ec823/bitbag-0.1.4/src/lib.rs:103:1
    |
103 | pub trait BitBaggable: Into<Self::Repr> {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: ...which again requires computing the supertraits of `BitBaggable`, completing the cycle
note: cycle used when computing trait implemented by `<impl at /home/jon/.cargo/registry/src/github.com-1ecc6299db9ec823/bitbag-0.1.4/src/lib.rs:109:45: 109:50>`
   --> /home/jon/.cargo/registry/src/github.com-1ecc6299db9ec823/bitbag-0.1.4/src/lib.rs:109:45
    |
109 | #[derive(Clone, Copy, Debug, Deref, Binary, AsRef)]
    |                                             ^^^^^
    = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0391`.
error: could not compile `bitbag`

But running with nightly (1.55.0), it compiles and runs as expected:

    Finished dev [unoptimized + debuginfo] target(s) in 14.03s
     Running `target/debug/bitbag_test`
A was set
B was set

Perhaps there is a MSRV, or nightly-only limitation that could be called out in the docs?

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.