bittle
Zero-cost bitsets over native Rust types.
The name bittle comes from bit and little. Small bitsets!
Usage
Add bittle as a dependency in your Cargo.toml:
[]
= "0.4.1"
Guide
This crate defines the Bits, BitsMut, and BitsOwned traits which
allows for generically interacting bit sets over existing Rust types such as
u32, [u32; 4], or &[u32]:
use Bits;
let array: = ;
assert!;
let n = 0b10001000_00000000_00000000_00000000u32;
assert!;
let array_of_arrays: = ;
assert!;
let mut vec: = vec!;
assert!;
We provide the set! macro, which is a zero-cost convenience method of
constructing primitive forms of bit sets:
use Bits;
let array: = set!;
assert!;
let array_of_arrays: = set!;
assert!;
let n: u32 = set!;
assert!;
Since a vector is not a primitive bit set, it could instead make use of
BitsMut directly:
use ;
let mut vec: = vec!;
vec.set_bit;
vec.set_bit;
vec.set_bit;
vec.set_bit;
assert!;
Due to how broadly these traits are implemented, we also try to avoid using names which are commonly used in other APIs, instead opt for bit-specific terminology such as:
- Something like
is_emptybecomesall_zeros- since with bits you're thinking about "ones and zeros". - Testing if a bit is set is
test_bit, or in general adding the*_bitsuffix to operations over individual bits. - Clearing all bits becomes
clear_bits, or in general adding the*_bitssuffix when operating over all bits.
use ;
let mut set = ;
set.set_bit;
assert!;
set.union_assign;
assert!;
set.clear_bit;
assert!;
set.clear_bits;
assert!;
Some other interesting operations, such as Bits::join_ones are available,
allowing bitsets to act like masks over other iterators:
use ;
let elements = vec!;
let mut m = 0u128;
m.set_bit;
assert!;
m.set_bit;
assert!;