macro_rules! init {
() => { ... };
}Expand description
Call all functions registered by call_on_init
§Example
use std::sync::atomic::{AtomicUsize, Ordering};
static COUNTER: AtomicUsize = AtomicUsize::new(0);
#[init_hook::call_on_init]
unsafe fn init_once_unchecked() {
COUNTER.fetch_add(1, Ordering::Release);
}
#[init_hook::call_on_init]
fn init_once() {
COUNTER.fetch_add(1, Ordering::Release);
}
fn main() {
init_hook::init!();
assert_eq!(COUNTER.load(Ordering::Acquire), 2);
}§Panic
If init isn’t used in main exactly once, init_hook will detect this and panic pre-main
ⓘ
use std::sync::atomic::{AtomicBool, Ordering};
static INIT_CALLED: AtomicBool = AtomicBool::new(false);
#[init_hook::call_on_init]
fn init() {
INIT_CALLED.store(true, Ordering::Release);
}
// This will panic with "`init_hook::init` must be used within the root main function"
fn main() {
let _init_called = INIT_CALLED.load(Ordering::Acquire);
}