summaryrefslogtreecommitdiffstats
path: root/src/dirty.rs
blob: 14c6245bd43b01ff90675cb7f20505a22baca892 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
use std::sync::{Arc, RwLock};
use std::hash::{Hash, Hasher};

#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub struct DirtyBit(bool);

#[derive(Debug)]
pub struct AsyncDirtyBit(pub Arc<RwLock<DirtyBit>>);

impl PartialEq for AsyncDirtyBit {
    fn eq(&self, other: &AsyncDirtyBit) -> bool {
        *self.0.read().unwrap() == *other.0.read().unwrap()
    }
}

impl Eq for AsyncDirtyBit {}

impl Hash for AsyncDirtyBit {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.0.read().unwrap().hash(state)
    }
}

impl Clone for AsyncDirtyBit {
    fn clone(&self) -> Self {
        AsyncDirtyBit(self.0.clone())
    }
}

pub trait Dirtyable {
    fn is_dirty(&self) -> bool;
    fn set_dirty(&mut self);
    fn set_clean(&mut self);
}


impl DirtyBit {
    pub fn new() -> DirtyBit {
        DirtyBit(false)
    }
}

impl AsyncDirtyBit {
    pub fn new() -> AsyncDirtyBit {
        AsyncDirtyBit(Arc::new(RwLock::new(DirtyBit::new())))
    }
}


impl Dirtyable for DirtyBit {
    fn is_dirty(&self) -> bool {
        self.0
    }
    fn set_dirty(&mut self) {
        self.0 = true;
    }
    fn set_clean(&mut self) {
        self.0 = false;
    }
}

impl Dirtyable for AsyncDirtyBit {
    fn is_dirty(&self) -> bool {
        self.0.read().unwrap().is_dirty()
    }
    fn set_dirty(&mut self) {
        self.0.write().unwrap().set_dirty();
    }
    fn set_clean(&mut self) {
        self.0.write().unwrap().set_clean();
    }
}