use super::{Config, Reader};
use arc_swap::ArcSwapOption;
use std::sync::Arc;
#[derive(Debug, Default)]
pub struct CfgCell<C: Config> {
inner: ArcSwapOption<C>,
}
impl<C> CfgCell<C>
where
C: Config,
{
pub fn set_once(&self, config: C) {
let old_config = self.inner.swap(Some(Arc::new(config)));
if old_config.is_some() {
panic!("can't reload Abscissa application config (yet)!");
}
}
#[allow(clippy::redundant_closure)]
pub fn read(&self) -> Reader<C> {
self.inner.load_full().unwrap_or_else(|| not_loaded())
}
}
fn not_loaded() -> ! {
panic!("Abscissa application state accessed before it has been initialized!")
}