use super::Application;
use once_cell::sync::OnceCell;
use std::ops::Deref;
pub struct AppCell<T>(OnceCell<T>);
impl<T> AppCell<T> {
pub const fn new() -> AppCell<T> {
Self(OnceCell::new())
}
}
impl<A> AppCell<A>
where
A: Application,
{
pub(crate) fn set_once(&self, app: A) {
self.0.set(app).unwrap_or_else(|_| {
panic!("can't reset Abscissa application state (yet)!");
})
}
}
impl<A> Deref for AppCell<A>
where
A: Application,
{
type Target = A;
#[allow(clippy::redundant_closure)]
fn deref(&self) -> &A {
self.0.get().unwrap_or_else(|| not_loaded())
}
}
fn not_loaded() -> ! {
panic!("Abscissa application state accessed before it has been initialized!")
}