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
66
67
68
69
70
71
72
73
74
/// Utility mixins for bit sets. Designed to be imported with 'using' to supplement regular Ints with
/// bitset functions.
pub trait BitSets {
/// Adds all the bits included in the mask, and returns the new bitset.
fn add(&self, mask: u32) -> u32;
/// Removes all the bits included in the mask, and returns the new bitset.
// static
fn remove(&self, mask: u32) -> u32;
/// Toggles all the bits included in the mask, and returns the new bitset.
// static
fn toggle(&self, mask: u32) -> u32;
/// Returns true if the bitset contains ANY of the bits in the given mask.
// static
fn contains(&self, mask: u32) -> bool;
/// Returns true if the bitset contains ALL of the bits in the given mask.
// static
fn contains_all(&self, mask: u32) -> bool;
/// Either adds or removes all the bits included in the mask, and returns the new bitset.
// static
fn set(&self, mask: u32, enabled: bool) -> u32;
}
impl BitSets for u32 {
/// Adds all the bits included in the mask, and returns the new bitset.
// static
#[inline]
fn add(&self, mask: u32) -> u32 {
self | mask
}
/// Removes all the bits included in the mask, and returns the new bitset.
// static
#[inline]
fn remove(&self, mask: u32) -> u32 {
// return self & ~mask;
unimplemented!()
}
/// Toggles all the bits included in the mask, and returns the new bitset.
// static
#[inline]
fn toggle(&self, mask: u32) -> u32 {
self ^ mask
}
/// Returns true if the bitset contains ANY of the bits in the given mask.
// static
#[inline]
fn contains(&self, mask: u32) -> bool {
self & mask != 0
}
/// Returns true if the bitset contains ALL of the bits in the given mask.
// static
#[inline]
fn contains_all(&self, mask: u32) -> bool {
self & mask == mask
}
/// Either adds or removes all the bits included in the mask, and returns the new bitset.
// static
fn set(&self, mask: u32, enabled: bool) -> u32 {
if enabled {
self.add(mask)
} else {
self.remove(mask)
}
}
}