using_bitmap8/
using_bitmap8.rs

1use fixed_bitmaps::{Bitmap128, Bitmap8, BitmapSize};
2
3fn main() {
4    // Creates an empty bitmap
5    let mut bitmap = Bitmap8::default();
6
7    // Bitmaps implement Display so you can view what the map looks like
8    println!("Default bitmap: {}", bitmap);
9
10    // Bitmaps also convert to their respective unsigned int versions and back again easily
11    println!("Value of bitmap: {}", bitmap.to_u8());
12
13    // Let's do the same as above, but actually setting the values in the bitmap to something
14    bitmap |= Bitmap8::from(101);
15
16    // Will show 01100101
17    println!("Bitmap after OR-ing with 101: {}", bitmap);
18
19    // Set the 4th index (the 5th bit) to true. Can simply unwrap the result to remove the warning, as we know
20    // for certain that 4 < 63
21    bitmap.set(4, true).unwrap();
22
23    // Will show that 117 (101 + 2^4) is the value of the bitmap
24    println!("Bitmap value: {}", bitmap.to_u8());
25
26    // Will print out the error thrown when trying to set an index out of bounds
27    match bitmap.set(8, true) {
28        Ok(_) => println!("That wasn't meant to happen... something's up with my implementation!"),
29        Err(error) => {
30            println!("Yep, threw an error as expected. Error message is as follows:");
31            eprintln!("{}", error);
32        }
33    }
34
35    let a = Bitmap8::from_set(2).unwrap();
36
37    // The above is equivalent to:
38    let b = Bitmap8::from(0b100);
39
40    assert!(a == b);
41
42    let bitmap = Bitmap8::create_bit_mask(3, 6, true);
43    println!("{}", bitmap);
44    println!("{}", *bitmap);
45    println!("{}", 0b111000);
46
47    println!("{:b}", u8::MAX << 3);
48
49    let bitmap = Bitmap8::create_bit_mask(3, 6, false);
50    println!("{}", bitmap);
51    println!("{:b}", u8::MAX.wrapping_shl(8));
52
53    let a = Bitmap128::create_bit_mask(3, 6, false);
54    let b = Bitmap128::create_bit_mask(7, 8, false);
55    let c = Bitmap128::create_bit_mask(0, 1, false);
56    let d = Bitmap128::create_bit_mask(0, 0, false);
57    let e = Bitmap128::create_bit_mask(8, 8, false);
58    let f = Bitmap128::create_bit_mask(0, Bitmap128::MAP_LENGTH, false);
59
60    println!("{}", a);
61    println!("{}", b);
62    println!("{}", c);
63    println!("{}", d);
64    println!("{}", e);
65    println!("{}", f);
66}