hunter/src/dirty.rs

73 lines
1.4 KiB
Rust
Raw Normal View History

use std::sync::{Arc, RwLock};
use std::hash::{Hash, Hasher};
2019-03-16 22:49:27 +01:00
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub struct DirtyBit(bool);
#[derive(Debug)]
pub struct AsyncDirtyBit(pub Arc<RwLock<DirtyBit>>);
2019-03-16 22:49:27 +01:00
impl PartialEq for AsyncDirtyBit {
fn eq(&self, other: &AsyncDirtyBit) -> bool {
*self.0.read().unwrap() == *other.0.read().unwrap()
2019-03-16 22:49:27 +01:00
}
}
2019-03-16 22:49:27 +01:00
impl Eq for AsyncDirtyBit {}
impl Hash for AsyncDirtyBit {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.read().unwrap().hash(state)
2019-03-16 22:49:27 +01:00
}
}
impl Clone for AsyncDirtyBit {
fn clone(&self) -> Self {
AsyncDirtyBit(self.0.clone())
2019-03-16 22:49:27 +01:00
}
}
pub trait Dirtyable {
fn is_dirty(&self) -> bool;
fn set_dirty(&mut self);
fn set_clean(&mut self);
}
2019-03-16 22:49:27 +01:00
impl DirtyBit {
pub fn new() -> DirtyBit {
DirtyBit(false)
}
}
impl AsyncDirtyBit {
pub fn new() -> AsyncDirtyBit {
AsyncDirtyBit(Arc::new(RwLock::new(DirtyBit::new())))
}
}
2019-03-16 22:49:27 +01:00
impl Dirtyable for DirtyBit {
fn is_dirty(&self) -> bool {
self.0
}
fn set_dirty(&mut self) {
self.0 = true;
2019-03-16 22:49:27 +01:00
}
fn set_clean(&mut self) {
self.0 = false;
}
}
impl Dirtyable for AsyncDirtyBit {
fn is_dirty(&self) -> bool {
self.0.read().unwrap().is_dirty()
}
fn set_dirty(&mut self) {
self.0.write().unwrap().set_dirty();
}
fn set_clean(&mut self) {
self.0.write().unwrap().set_clean();
2019-03-16 22:49:27 +01:00
}
}