1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2pub enum Op {
3 None = 0,
4 Write,
5 Take,
6 Replace,
7 Update,
8 Remove,
9}
10impl From<u8> for Op {
11 fn from(s: u8) -> Self {
12 match s {
13 s if s == Op::None as u8 => Op::None,
14 s if s == Op::Write as u8 => Op::Write,
15 s if s == Op::Take as u8 => Op::Take,
16 s if s == Op::Replace as u8 => Op::Replace,
17 s if s == Op::Remove as u8 => Op::Remove,
18 _ => Op::None,
19 }
20 }
21}
22impl From<Op> for u8 {
23 fn from(s: Op) -> Self {
24 s as u8
25 }
26}
27
28pub type AtomicMemoryOp = atomic::Atomic<MemoryOp>;
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
31pub struct MemoryOp(u32);
32impl MemoryOp {
33 pub const fn new() -> MemoryOp {
34 Self(0)
35 }
36 pub fn op(&self) -> Op {
37 Op::from((self.0 & 0xFF) as u8)
38 }
39 pub fn cur_version(&self) -> u16 {
40 ((self.0 & 0x000FFF00) >> 8) as u16
41 }
42 pub fn next_version(&mut self) -> u16 {
43 ((self.0 & 0xFFF00000) >> 20) as u16
44 }
45 pub fn finish(&mut self) {
46 self.0 = (self.0 & 0xFFF00000) | ((self.next_version() as u32) << 8)
47 }
48 pub fn is_finished(&mut self) -> bool {
49 self.cur_version() == self.next_version()
50 }
51 pub fn set_op(&mut self, op: Op) -> u16 {
52 self.0 = (self.0.wrapping_add(0x00100000) & 0xFFFFFF00) | (op as u32);
53 self.next_version()
54 }
55}
56impl From<u32> for MemoryOp {
57 fn from(s: u32) -> Self {
58 Self(s)
59 }
60}
61impl From<MemoryOp> for u32 {
62 fn from(s: MemoryOp) -> Self {
63 s.0
64 }
65}