logo
 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
//! Configuration cell: holder of application configuration.

use super::{Config, Reader};
use arc_swap::ArcSwapOption;
use std::sync::Arc;

/// Configuration cell: holder of application configuration.
#[derive(Debug, Default)]
pub struct CfgCell<C: Config> {
    /// Inner configuration state
    inner: ArcSwapOption<C>,
}

impl<C> CfgCell<C>
where
    C: Config,
{
    /// Set the application configuration to the given value.
    ///
    /// This can only be performed once without causing a crash.
    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)!");
        }
    }

    /// Read the current configuration.
    pub fn read(&self) -> Reader<C> {
        self.inner.load_full().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!")
}