#![deny(missing_docs)]
use std::sync::{Arc, RwLock, RwLockReadGuard};
pub struct AppStateWrapper<S: AppState>(Arc<RwLock<S>>);
pub trait AppState {
type Message;
fn msg(&mut self, message: Self::Message);
}
impl<S: AppState> AppStateWrapper<S> {
pub fn new(state: S) -> Self {
Self(Arc::new(RwLock::new(state)))
}
pub fn msg(&mut self, msg: S::Message) {
self.0.write().unwrap().msg(msg);
}
pub fn read(&self) -> RwLockReadGuard<'_, S> {
self.0.read().unwrap()
}
}
impl<S: AppState> Clone for AppStateWrapper<S> {
fn clone(&self) -> Self {
AppStateWrapper(Arc::clone(&self.0))
}
}