macro_rules! init_big {
    {
        $( #[doc = $($token_doc:tt)*] )*
        $token_vis:vis token $token_name:ident;
        $( #[doc = $($doc:tt)*] )*
        $vis:vis static $name:ident : $type:ty = $const_init:expr;

        init($runtime_init_param_name:ident) { $($runtime_init:tt)+ }
    } => { ... };
}
Expand description

A runtime-initialized static with zero access overhead protected by an access token.

This macro should only used if the static value is too big to pass on stack. Use init! otherwise.

See the crate-level documentation for details.

init_token::init_big! {
    /// The magic token to get `MY_STATIC` working.
    pub token MyToken;
    /// My cool static.
    pub static MY_STATIC: i32 = 0;

    init(my_static) {
        *my_static = "-123".parse().unwrap();
    }
}

let token = MY_STATIC.init();
assert_eq!(*token, -123);
let static_ref: &'static i32 = MyToken::static_ref(token);
assert_eq!(*static_ref, -123);
// We can call `init()` twice, but then it has to check whether the `static`
// is initialized. Therefore, prefer using existing tokens if possible.
assert_eq!(*MY_STATIC.init(), -123);