mod reader;
mod writer;
pub use self::{reader::Reader, writer::Writer};
use super::Application;
use std::sync::{PoisonError, RwLock};
pub struct Lock<A: Application>(RwLock<Option<A>>);
impl<A> Default for Lock<A>
where
A: Application,
{
fn default() -> Self {
Lock(RwLock::new(None))
}
}
impl<A: Application> Lock<A> {
pub fn read(&'static self) -> Reader<A> {
Reader::new(self.0.read().unwrap_or_else(|e| poisoned(e)))
}
pub fn write(&'static self) -> Writer<A> {
Writer::new(self.0.write().unwrap_or_else(|e| poisoned(e)))
}
pub fn set(&self, new_app: A) {
let mut state = self.0.write().unwrap_or_else(|e| poisoned(e));
*state = Some(new_app);
}
}
fn poisoned<Guard>(e: PoisonError<Guard>) -> ! {
panic!(
"Abscissa application state corrupted by unhandled crash: {}",
e
)
}