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
use crate::actor::State;
#[derive(Copy, Clone)]
pub(crate) struct CountAndState(usize);
const COUNT_SHIFT: u32 = 2;
const COUNT_INC: usize = 1 << COUNT_SHIFT;
const COUNT_MASK: usize = !(COUNT_INC - 1);
impl CountAndState {
pub fn new() -> Self {
Self(State::Prep as usize)
}
pub fn inc(self) -> Self {
if self.0 >= COUNT_MASK {
self
} else {
Self(self.0 + COUNT_INC)
}
}
pub fn dec(self) -> (Self, bool) {
if self.0 < COUNT_INC || self.0 >= COUNT_MASK {
(self, false)
} else {
let val = self.0 - COUNT_INC;
(Self(val), val < COUNT_INC)
}
}
pub fn set_state(self, state: State) -> Self {
Self((self.0 & COUNT_MASK) | (state as usize))
}
pub fn is_prep(self) -> bool {
(self.0 & !COUNT_MASK) == (State::Prep as usize)
}
pub fn is_zombie(self) -> bool {
(self.0 & !COUNT_MASK) == (State::Zombie as usize)
}
}