[][src]Function spirit::extension::immutable_cfg_init

pub fn immutable_cfg_init<Ext, R, E, F, N>(
    extractor: E,
    init: F,
    name: N
) -> impl Extension<Ext> where
    Ext: Extensible,
    E: for<'a> Fn(&'a Ext::Config) -> &R + Send + 'static,
    F: FnOnce(&R) + Send + 'static,
    R: Clone + PartialEq + Send + 'static,
    N: Display + Send + 'static, 

An extension for one-time initial configuration.

Sometimes, some configuration values can't be reasonably updated at runtime (libraries don't support reconfiguration, there's no time to implement that, ...). This callback tries to improve the situation around these configurations.

The extractor extracts a fragment of configuration every time a configuration is loaded. The first time this happens, init is called with this extracted configuration. Upon any future configuration reloads, a warning is issued (with the given name) if the configuration contains a different value than the one it was originally initialized (and is silent if it is the same).

See also

Examples

use serde::Deserialize;
use spirit::{Empty, Spirit};
use spirit::prelude::*;
use spirit::extension;

#[derive(Clone, Debug, Default, Deserialize)]
struct Cfg {
    #[serde(default)]
    msg: String,
}

impl Cfg {
    fn msg(&self) -> &String {
        &self.msg
    }
}

fn print_msg(msg: &String) {
    println!("{}", msg);
}

fn main() {
    Spirit::<Empty, Cfg>::new()
        // The first version of `msg` is printed at the initial configuration load. If however
        // the configuration changes into some other message, a warning is printed (because
        // there's no way to modify the already printed message
        .with(extension::immutable_cfg_init(Cfg::msg, print_msg, "message"))
        .run(|_| Ok(()));
}