| Crates.io | init-hook |
| lib.rs | init-hook |
| version | 2.0.0 |
| created_at | 2025-03-19 05:53:14.769423+00 |
| updated_at | 2025-03-19 07:21:35.127714+00 |
| description | Guaranteed one-time initialization |
| homepage | |
| repository | https://github.com/Bajix/init-hook/ |
| max_upload_size | |
| id | 1597738 |
| size | 23,422 |
This crate allows registering functions for guaranteed initialization during main
use std::sync::atomic::{AtomicUsize, Ordering};
static COUNTER: AtomicUsize = AtomicUsize::new(0);
// Register function to be called exactly once during init
#[init_hook::call_on_init]
fn init_once() {
COUNTER.fetch_add(1, Ordering::Release);
}
// Registered functions can also be unsafe
#[init_hook::call_on_init]
unsafe fn init_once_unchecked() {
COUNTER.fetch_add(1, Ordering::Release);
}
fn main() {
// This is enforced by a pre-main assertion to always be included exactly once
init_hook::init!();
assert_eq!(COUNTER.load(Ordering::Acquire), 2);
}
| Linux | macOS | Windows | FreeBSD | OpenBSD | illumos | Other |
|---|---|---|---|---|---|---|
| 💚 | 💚 | 💚 | 💚 | 💚 | 💚 | ❌ |