parent
3a45996496
commit
cbb0e341c0
@ -0,0 +1,47 @@
|
||||
use core::ops::{Deref, DerefMut};
|
||||
|
||||
/// 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 read()
|
||||
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: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if dirty, false otherwise
|
||||
#[allow(dead_code)]
|
||||
pub fn dirty(&self) -> bool {
|
||||
self.dirty
|
||||
}
|
||||
|
||||
/// Reset dirty
|
||||
pub fn sync(&mut self) {
|
||||
self.dirty = false;
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Deref for Dirty<T> {
|
||||
type Target = T;
|
||||
|
||||
/// Read the value
|
||||
fn deref(&self) -> &T {
|
||||
&self.value
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> DerefMut for Dirty<T> {
|
||||
/// Writable value return, sets the dirty flag
|
||||
fn deref_mut(&mut self) -> &mut T {
|
||||
self.dirty = true;
|
||||
&mut self.value
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue