summary refs log tree commit diff
path: root/src/util/tracker.rs
blob: 66c30a9d0dbf50b6f62567eaaecb0f00dfd4d44e (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
pub struct Tracker<T> {
    inner: T,
    dirty: bool,
}

/// Tracks changes to an inner value T. Any change using `set` will cause the
/// tracker to be marked as dirty.
impl<T> Tracker<T> {
    pub fn new(inner: T) -> Self {
        Self { inner, dirty: true }
    }

    pub fn get(&self) -> &T {
        &self.inner
    }

    pub fn get_mut(&mut self) -> &mut T {
        self.dirty = true;
        &mut self.inner
    }

    pub fn set(&mut self, value: T) {
        self.dirty = true;
        self.inner = value;
    }

    pub fn is_dirty(&self) -> bool {
        self.dirty
    }

    /// Marks the tracker as clean.
    pub fn reset(&mut self) {
        self.dirty = false;
    }
}

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