Crate neobit

Crate neobit 

Source
Expand description

§neobit

Zero-dependency, lightweight bitflags with readable debug output.

§Quick Start

use neobit::neobit;

neobit! {
    /// File permissions
    pub struct Permissions: u8 {
        const READ    = 0b001;
        const WRITE   = 0b010;
        const EXECUTE = 0b100;
    }
}

let perms = Permissions::READ | Permissions::WRITE;
assert!(perms.contains(Permissions::READ));
println!("{:?}", perms);  // Permissions(READ | WRITE)

// Get all flags
let all = Permissions::all();
assert!(all.contains(Permissions::READ | Permissions::WRITE | Permissions::EXECUTE));

// Validate bits
let valid = Permissions::from_bits(0b011);
assert!(valid.is_some());
let invalid = Permissions::from_bits(0b1000);
assert!(invalid.is_none());

§Design Philosophy

Flexible Bit Validation: neobit provides both validated and unchecked bit operations.

  • from_bits() validates bits and returns Option<Self>
  • from_bits_retain() preserves all bits without validation

This preserves all bit information when needed, which is essential for:

  • C FFI bindings
  • Protocol parsing
  • Hardware register access

§Signed Types Warning

Signed integer types are supported for ABI compatibility, but be careful with the ! (complement) operator - it follows Rust’s two’s complement semantics which may produce unexpected results.

Macros§

neobit
Defines a bitflags struct with the specified flags.