Crates.io | bitworks |
lib.rs | bitworks |
version | 0.2.0 |
source | src |
created_at | 2024-02-13 17:54:23.399303 |
updated_at | 2024-02-25 17:05:48.802236 |
description | Crate meant to provide easy to use bitsets with emphasis on safety. |
homepage | |
repository | https://github.com/TRI99ERED/bitworks |
max_upload_size | |
id | 1138753 |
size | 239,029 |
Crate meant to provide easy to use bitsets, with some out of the box functionality.
Enable feature "serde" to enable serde::Serialize
and serde::Deserialize
for most applicable types.
use bitworks::prelude::*;
fn main() {
// Build the bitset with variety of methods.
let mut bitset = Bitset8::NONE
.clone()
.include(Bitset8::new(0b10000000))
.set(Index8::from_usize(1))
.replace(Index8::from_usize(3), One)
.flip(Index8::from_usize(5))
.build();
assert_eq!(bitset.into_inner(), 0b10101010);
// Iterate over bits as copies, references or mutable references.
for mut bit in bitset.bits_mut() {
*bit = !*bit;
}
assert_eq!(bitset.into_inner(), 0b01010101);
assert_eq!(bitset.bit(Index8::from_usize(7)), Zero);
let other = Bitset8::new(0b11110000);
// Use Set operations.
let bitset = bitset.complement();
assert_eq!(bitset.into_inner(), 0b10101010);
assert_eq!(bitset.union(other).into_inner(), 0b11111010);
assert_eq!(bitset.intersection(other).into_inner(), 0b10100000);
assert_eq!(bitset.difference(other).into_inner(), 0b00001010);
assert_eq!(bitset.sym_difference(other).into_inner(), 0b01011010);
assert!(bitset.intersects(&other));
assert!(!bitset.includes(&other));
assert!(Bitset8::ALL.includes(&bitset));
// Expand, combine and split.
let bigger_bitset: Bitset16 = bitset.expand();
assert_eq!(bigger_bitset.into_inner(), 0b0000000010101010);
let combined: Bitset16 = bitset.combine(other);
assert_eq!(combined.into_inner(), 0b1111000010101010);
let (bitset, other) = combined.split::<Bitset8, Bitset8>();
assert_eq!(bitset.into_inner(), 0b10101010);
assert_eq!(other.into_inner(), 0b11110000);
// Collect in other collections and construct from them.
let v: Vec<Bit> = bitset.bits().rev().collect();
assert_eq!(&v, &[One, Zero, One, Zero, One, Zero, One, Zero]);
let bitset = Bitset8::from_iterable(&v);
assert_eq!(bitset.into_inner(), 0b01010101);
}
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.