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
use std::cmp::Ordering; use std::fmt::{Debug, Formatter}; use std::hash::{Hash, Hasher}; use std::ops::{Deref, DerefMut}; pub struct Tracked<T> { data: T, changed: bool, } impl<T> Tracked<T> { pub fn changed(&self) -> bool { self.changed } pub fn trigger(&mut self) { self.changed = true; } pub fn reset(&mut self) -> bool { let last_state = self.changed; self.changed = false; last_state } } impl<T> Deref for Tracked<T> { type Target = T; fn deref(&self) -> &Self::Target { &self.data } } impl<T> DerefMut for Tracked<T> { fn deref_mut(&mut self) -> &mut Self::Target { self.changed = true; &mut self.data } } impl<T> Default for Tracked<T> where T: Default, { fn default() -> Self { Self { data: Default::default(), changed: false, } } } impl<T> Copy for Tracked<T> where T: Copy {} impl<T> PartialOrd for Tracked<T> where T: PartialOrd, { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { self.data.partial_cmp(&other.data) } } impl<T> Hash for Tracked<T> where T: Hash, { fn hash<H: Hasher>(&self, state: &mut H) { self.data.hash(state); self.changed.hash(state); } } impl<T> Ord for Tracked<T> where T: PartialOrd + Ord + Eq, { fn cmp(&self, other: &Self) -> Ordering { self.data.cmp(&other.data) } } impl<T> PartialEq for Tracked<T> where T: PartialEq, { fn eq(&self, other: &Self) -> bool { self.data.eq(&other.data) } } impl<T> Eq for Tracked<T> where T: Eq + PartialEq {} impl<T> Clone for Tracked<T> where T: Clone, { fn clone(&self) -> Self { Self { data: self.data.clone(), changed: false, } } } impl<T> Debug for Tracked<T> where T: Debug, { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.debug_struct("Tracked") .field("data", &self.data) .field("changed", &self.changed) .finish() } }