1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
extern crate self as init_hook;
pub use call_on_init;
pub use linkme;
/// 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
///
/// ```should_panic
/// 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);
/// }
/// ```