basecoin_store/utils/
sync.rs

1use core::panic;
2use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
3
4pub trait Async: Send + Sync + 'static {}
5
6impl<A> Async for A where A: Send + Sync + 'static {}
7
8pub type SharedRw<T> = Arc<RwLock<T>>;
9
10pub trait SharedRwExt<T> {
11    fn read_access(&self) -> RwLockReadGuard<'_, T>;
12    fn write_access(&self) -> RwLockWriteGuard<'_, T>;
13}
14
15impl<T> SharedRwExt<T> for SharedRw<T> {
16    fn read_access(&self) -> RwLockReadGuard<'_, T> {
17        match self.read() {
18            Ok(guard) => guard,
19            Err(poisoned) => panic!("poisoned lock: {:?}", poisoned),
20        }
21    }
22
23    fn write_access(&self) -> RwLockWriteGuard<'_, T> {
24        match self.write() {
25            Ok(guard) => guard,
26            Err(poisoned) => panic!("poisoned lock: {:?}", poisoned),
27        }
28    }
29}