layered_bitset/
primitive.rs

1use super::{BitSet, BitSetMut};
2
3macro_rules! impl_for_primitive {
4    ($ty:ty : $size:literal) => {
5        impl BitSet for $ty {
6            const UPPER_BOUND: u32 = $size;
7
8            #[inline]
9            fn get(&self, index: u32) -> bool {
10                assert!(index < $size);
11                0 != *self & ((1 as $ty) << index)
12            }
13
14            #[inline]
15            fn find_set(&self, lower_bound: u32) -> Option<u32> {
16                assert!(lower_bound < $size);
17                let masked = *self & (!0 as $ty).wrapping_shl(lower_bound);
18                match masked.trailing_zeros() {
19                    $size => None,
20                    index => Some(index),
21                }
22            }
23        }
24
25        impl BitSetMut for $ty {
26            #[inline]
27            fn set(&mut self, index: u32, bit: bool) {
28                assert!(index < $size);
29                match bit {
30                    true => *self |= (1 as $ty << index),
31                    false => *self &= !(1 as $ty << index),
32                }
33            }
34        }
35    };
36
37    // ($ty:ty : $size:literal, $($tail_ty:ty : $tail_size:literal),+ $(,)?) => {
38    //     impl_for_primitive!($($tail_ty : $tail_size),+);
39    // }
40
41    ($ty:ty : $size:literal, $($tail:tt)+) => {
42        impl_for_primitive!($ty : $size);
43        impl_for_primitive!($($tail)+);
44    }
45}
46
47impl_for_primitive!(u8 : 8, u16 : 16, u32 : 32, u64 : 64, u128 : 128);