quark/
bit_size.rs

1/// Provides the bit size of the type as a constant.
2///
3/// This trait defines a constant for the number of bits in the type. This constant can be useful
4/// for implementing other traits on various sized types.
5///
6/// # Examples
7///
8/// ```
9/// use quark::BitSize;
10///
11/// assert_eq!(u8::BIT_SIZE, 8);
12/// assert_eq!(u128::BIT_SIZE, 128);
13/// ```
14pub trait BitSize {
15    /// The size of the type in bits.
16    const BIT_SIZE: usize;
17}
18
19macro_rules! bit_size_impl {
20    ($type:ty, $sz:expr) => {
21        impl BitSize for $type {
22            const BIT_SIZE: usize = $sz;
23        }
24    };
25}
26
27bit_size_impl!(u8, 8);
28bit_size_impl!(u16, 16);
29bit_size_impl!(u32, 32);
30bit_size_impl!(u64, 64);
31bit_size_impl!(u128, 128);
32#[cfg(target_pointer_width = "32")]
33bit_size_impl!(usize, 32);
34#[cfg(target_pointer_width = "64")]
35bit_size_impl!(usize, 64);
36bit_size_impl!(i8, 8);
37bit_size_impl!(i16, 16);
38bit_size_impl!(i32, 32);
39bit_size_impl!(i64, 64);
40bit_size_impl!(i128, 128);
41#[cfg(target_pointer_width = "32")]
42bit_size_impl!(isize, 32);
43#[cfg(target_pointer_width = "64")]
44bit_size_impl!(isize, 64);