aboutsummaryrefslogtreecommitdiffstats
path: root/src/run/var/watch.rs
blob: 22ac638d4b7f24e4fe256aa39d5db6ec35ce1c09 (plain) (blame)
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
50
51
52
53
54
55
56
57
58
59
60
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,
}