Skip to content

Repository files navigation

LicenseLicenseCargoDocumentation

Init Hook

This crate allows registering functions for guaranteed initialization during main

Example

use std::sync::atomic::{AtomicUsize,Ordering};staticCOUNTER:AtomicUsize = AtomicUsize::new(0);// Register function to be called exactly once during init#[init_hook::call_on_init]fninit_once(){COUNTER.fetch_add(1,Ordering::Release);}// Registered functions can also be unsafe#[init_hook::call_on_init]unsafefninit_once_unchecked(){COUNTER.fetch_add(1,Ordering::Release);}fnmain(){// This is enforced by a pre-main assertion to always be included exactly once
init_hook::init!();assert_eq!(COUNTER.load(Ordering::Acquire),2);}

Platform support

LinuxmacOSWindowsFreeBSDOpenBSDillumosOther
💚💚💚💚💚💚

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

Guaranteed initialization during main

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages