use std::sync::RwLock;
pub trait RwLockExt<T> {
fn if_then<P, A, R>(&self, predicate: P, action: A) -> Option<R>
where P: Fn(&T) -> bool, A: FnOnce(&mut T) -> R;
}
impl<T> RwLockExt<T> for RwLock<T> {
fn if_then<P, A, R>(&self, predicate: P, action: A) -> Option<R>
where P: Fn(&T) -> bool, A: FnOnce(&mut T) -> R {
if predicate(&self.read().unwrap()) {
let mut writer = self.write().unwrap();
if predicate(&writer) {
return Some(action(&mut writer))
}
};
None
}
}
pub mod test {
use {ContentId};
pub fn gen_random_block(block_size: usize) -> (ContentId, Vec<u8>) {
let data = (0..block_size).map(|_| ::rand::random::<u8>()).collect::<Vec<_>>();
let id = ContentId::hash(&data);
(id, data)
}
}