Macro init

Source
macro_rules! init {
    {
        $( #[doc = $($token_doc:tt)*] )*
        $token_vis:vis token $token_name:ident;
        $( #[doc = $($doc:tt)*] )*
        $vis:vis static $name:ident : $type:ty = $init:expr;
    } => { ... };
}
Expand description

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

This macro should be the default choice. Use init_big! only if the static value is too big to pass on stack.

See the crate-level documentation for details.

init_token::init! {
    /// The magic token to get `MY_STATIC` working.
    pub token MyToken;
    /// My cool static.
    pub static MY_STATIC: i32 = "-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);