1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
pub trait BitTest {
fn bit_len(&self) -> usize;
fn bit(&self, n: usize) -> bool;
fn trailing_zeros(&self) -> Option<usize>;
}
pub trait PowerOfTwo {
fn is_power_of_two(&self) -> bool;
fn next_power_of_two(self) -> Self;
}
macro_rules! impl_bit_ops_prim {
($($T:ty)*) => {$(
impl BitTest for $T {
#[inline]
fn bit_len(&self) -> usize {
(<$T>::BITS - self.leading_zeros()) as usize
}
#[inline]
fn bit(&self, position: usize) -> bool {
self & (1 << position) > 0
}
#[inline]
fn trailing_zeros(&self) -> Option<usize> {
if *self == 0 {
None
} else {
Some(<$T>::trailing_zeros(*self) as usize)
}
}
}
impl PowerOfTwo for $T {
#[inline]
fn is_power_of_two(&self) -> bool {
<$T>::is_power_of_two(*self)
}
#[inline]
fn next_power_of_two(self) -> $T {
<$T>::next_power_of_two(self)
}
}
)*}
}
impl_bit_ops_prim!(u8 u16 u32 u64 u128 usize);