From a9ea5669915f4ac6b0d9e7d826d1ee341a2e3c80 Mon Sep 17 00:00:00 2001 From: Jonas Maier <> Date: Sat, 23 May 2026 22:45:12 +0200 Subject: allow registration of variable watchers --- src/run/var/watch.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/run/var/watch.rs (limited to 'src/run/var/watch.rs') 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(&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, +} -- cgit v1.2.3