init-hook 1.0.0

Guaranteed one-time initialization
Documentation

License License Cargo Documentation

This crate allows registering functions for guaranteed initialization during main

Example

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);
}

fn main() {
    // This is enforced by a pre-main assertion to always be included exactly once
    init_hook::init!();

    assert!(INIT_CALLED.load(Ordering::Acquire));
}