blob: 060acae5561c37e4217f3d2f4911de5ce9623b3a (
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
|
use std::ops::Deref;
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;
}
/// Sets the inner value to `value` and marks the tracker as clean.
pub fn set_clean(&mut self, value: T) {
self.dirty = false;
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> Deref for Tracker<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.get()
}
}
impl<T: Default> Default for Tracker<T> {
fn default() -> Self {
Self::new(T::default())
}
}
impl<T: Eq> Tracker<T> {
pub fn set_if_ne(&mut self, value: T) {
if self.inner != value {
self.set(value);
}
}
}
|