maycoon_core/app/
update.rs1use bitflags::bitflags;
2use std::sync::Arc;
3use std::sync::atomic::{AtomicU8, Ordering};
4
5bitflags! {
6 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
14 pub struct Update: u8 {
15 const EVAL = 0b00000001;
17 const DRAW = 0b00000010;
19 const LAYOUT = 0b00000100;
21 const FORCE = 0b00001000;
23 const EXIT = 0b00010000;
25 }
26}
27
28#[derive(Clone, Debug)]
32pub struct UpdateManager {
33 update: Arc<AtomicU8>,
34}
35
36impl UpdateManager {
37 pub fn new() -> Self {
39 Self {
40 update: Arc::new(AtomicU8::new(0)),
41 }
42 }
43
44 pub fn insert(&self, update: Update) {
46 tracing::debug!("inserting update {update:?}");
47 self.update.fetch_or(update.bits(), Ordering::AcqRel);
48 }
49
50 pub fn remove(&self, update: Update) {
52 tracing::debug!("removing update {update:?}");
53 self.update.fetch_and(!update.bits(), Ordering::AcqRel);
54 }
55
56 pub fn get(&self) -> Update {
58 Update::from_bits(self.update.load(Ordering::Acquire))
59 .expect("failed to decode update bits")
60 }
61
62 pub fn set(&self, update: Update) {
64 tracing::debug!("setting update {update:?}");
65 self.update.store(update.bits(), Ordering::Release);
66 }
67
68 pub fn clear(&self) {
70 self.update.store(0, Ordering::Release);
71 }
72}
73
74impl Default for UpdateManager {
75 fn default() -> Self {
76 Self::new()
77 }
78}