1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//! Mutex guard for immutably accessing global application state

use super::Application;
use std::{ops::Deref, sync::RwLockReadGuard};

/// Generic `RwLockWriteGuard` for a `'static` lifetime.
pub(crate) type Guard<T> = RwLockReadGuard<'static, Option<T>>;

/// Wrapper around a `RwLockReadGuard` for reading global application state.
pub struct Reader<A>(Guard<A>)
where
    A: 'static + Application;

impl<A> Reader<A>
where
    A: 'static + Application,
{
    /// Create wrapper around a read-only application mutex guard
    pub(super) fn new(guard: Guard<A>) -> Self {
        Reader(guard)
    }
}

impl<A> Deref for Reader<A>
where
    A: 'static + Application,
{
    type Target = A;

    fn deref(&self) -> &A {
        self.0.deref().as_ref().unwrap_or_else(|| not_loaded())
    }
}

/// Error handler called if `get()` is invoked before the global
/// application state has been initialized.
///
/// This indicates a bug in the program accessing this type.
fn not_loaded() -> ! {
    panic!("Abscissa application state accessed before it has been initialized!")
}