aboutsummaryrefslogtreecommitdiffstats
path: root/src/run/var/watch.rs
diff options
context:
space:
mode:
authorJonas Maier <>2026-05-23 22:45:12 +0200
committerJonas Maier <>2026-05-23 22:45:12 +0200
commita9ea5669915f4ac6b0d9e7d826d1ee341a2e3c80 (patch)
treee5756d045b25e45c46de1609e735074eb21e23e6 /src/run/var/watch.rs
parent1ea4d307556ff522ab3a5bcca011966f3a0f440e (diff)
downloadpish-a9ea5669915f4ac6b0d9e7d826d1ee341a2e3c80.tar.gz
allow registration of variable watchers
Diffstat (limited to 'src/run/var/watch.rs')
-rw-r--r--src/run/var/watch.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/run/var/watch.rs b/src/run/var/watch.rs
new file mode 100644
index 0000000..22ac638
--- /dev/null
+++ b/src/run/var/watch.rs
@@ -0,0 +1,61 @@
+use std::{
+ hash::Hash,
+ sync::{Arc, Weak, atomic::AtomicU32},
+};
+
+#[derive(Clone)]
+pub struct WatchId {
+ id: u32,
+ count: Arc<()>,
+}
+
+#[derive(Clone)]
+pub struct WeakWatchId {
+ id: u32,
+ count: Weak<()>,
+}
+
+impl PartialEq for WeakWatchId {
+ fn eq(&self, other: &Self) -> bool {
+ self.id == other.id
+ }
+}
+
+impl Eq for WeakWatchId {}
+
+impl Hash for WeakWatchId {
+ fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
+ self.id.hash(state);
+ }
+}
+
+impl WatchId {
+ pub(super) fn new() -> Self {
+ static GEN: AtomicU32 = AtomicU32::new(0);
+ Self {
+ id: GEN.fetch_add(1, std::sync::atomic::Ordering::SeqCst),
+ count: Arc::new(()),
+ }
+ }
+
+ pub(super) fn weak(&self) -> WeakWatchId {
+ WeakWatchId {
+ id: self.id,
+ count: Arc::downgrade(&self.count),
+ }
+ }
+}
+
+impl WeakWatchId {
+ pub fn is_dead(&self) -> bool {
+ self.count.upgrade().is_none()
+ }
+}
+
+pub struct WatchState {
+ /// if set, essentially always dirty
+ pub special: bool,
+
+ /// has the set of watched variables changed?
+ pub dirty: bool,
+}