Macro zng_var::context_var

source ·
macro_rules! context_var {
    ($(
        $(#[$attr:meta])*
        $vis:vis static $NAME:ident: $Type:ty = $default:expr;
    )+) => { ... };
}
Expand description

Declares new ContextVar static items.

§Examples

context_var! {
    /// A public documented context var.
    pub static FOO_VAR: u8 = 10;

    // A private context var.
    static BAR_VAR: NotConst = init_val();

    // A var that *inherits* from another.
    pub static DERIVED_VAR: u8 = FOO_VAR;
}

§Default Value

All context variable have a default fallback value that is used when the variable is not set in the context.

The default value is instantiated once per app and is the value of the variable when it is not set in the context, any value IntoVar<T> is allowed, including other variables.

The default value can also be a Var::map to another context var, but note that mapping vars are contextualized, meaning that they evaluate the mapping in each different context read, so a context var with mapping value read in a thousand widgets will generate a thousand different mapping vars, but if the same var mapping is set in the root widget, the thousand widgets will all use the same mapping var.

§Naming Convention

It is recommended that the type name ends with the _VAR suffix.

§Context Local

If you are only interested in sharing a contextual value you can use the context_local! macro instead.