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
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use std::ops::Deref;

/// Dirty wraps a value of type T with functions similiar to that of a Read/Write
/// lock but simply sets a dirty flag on write(), reset on clear().
/// Use read() or deref (*dirty_variable) to access the inner value.
pub struct Dirty<T> {
    value: T,
    dirty: bool,
}

impl<T> Dirty<T> {
    /// Create a new Dirty.
    pub fn new(val: T) -> Dirty<T> {
        Dirty {
            value: val,
            dirty: true,
        }
    }

    /// Returns true if dirty, false otherwise.
    pub fn dirty(&self) -> bool {
        self.dirty
    }

    /// Writable value return, sets the dirty flag.
    pub fn write(&mut self) -> &mut T {
        self.dirty = true;
        &mut self.value
    }

    /// Read the value.
    pub fn read(&self) -> &T {
        &self.value
    }
    
    /// Clears the dirty flag.
    pub fn clear(&mut self) {
        self.dirty = false;
    }

    /// Read the value only if modified since last read.
    pub fn read_dirty(&self) -> Option<&T> {
        match self.dirty {
            true => {
                Some(&self.value)
            },
            false => None,
        }
    }
}

impl<T> Deref for Dirty<T> {
    type Target = T;
    fn deref(&self) -> &T {
        &self.value
    }
}

impl<T> Default for Dirty<T> where T: Default {
    fn default() -> Self {
        Dirty::new(T::default())
    }
}

#[cfg(test)]
mod tests {
    use super::Dirty;

    #[test]
    fn new_dirty() {
        let dirty = Dirty::new(0);
        assert!(dirty.dirty());
    }

    #[test]
    fn read_doesnt_clear_flag() {
        let dirty = Dirty::new(0);
        assert!(dirty.dirty());
        assert!(*dirty.read() == 0);
        assert!(dirty.dirty());
    }

    #[test]
    fn write_sets_flag() {
        let mut dirty = Dirty::new(0);
        assert!(*dirty.read() == 0);
        dirty.clear();
        assert!(!dirty.dirty());
        *dirty.write() += 1;
        assert!(dirty.dirty());
    }

    #[test]
    fn read_dirty() {
        let mut dirty = Dirty::new(0);
        assert!(dirty.read_dirty().is_some());
		dirty.clear();
        assert!(!dirty.dirty());
        assert!(dirty.read_dirty() == None);
        assert!(!dirty.dirty());
        *dirty.write() += 1;
        assert!(dirty.dirty());
        assert!(dirty.read_dirty().is_some());
		dirty.clear();
        assert!(!dirty.dirty());
        assert!(dirty.read_dirty() == None);
    }
    
    #[test]
    fn access_inner_deref() {
        let dirty = Dirty::new(0);
        assert!(*dirty == 0);
    }
    
    #[test]
    fn default_value() {
        let dirty = Dirty::<i32>::default();
        assert!(*dirty == 0);
    }
}