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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
extern crate self as booter;
#[doc(hidden)]
pub extern crate atomic_take;
#[doc(hidden)]
pub extern crate inventory;
use atomic_take::AtomicTake;
use std::sync::atomic::{AtomicBool, Ordering};
#[doc(hidden)]
pub struct BootBox {
pub boot_fn: AtomicTake<Box<dyn FnOnce()>>,
}
inventory::collect!(BootBox);
#[cfg(debug_assertions)]
static BOOT_CALLED: AtomicBool = AtomicBool::new(false);
pub fn boot() {
#[cfg(debug_assertions)]
BOOT_CALLED.store(true, Ordering::Release);
for boot_box in inventory::iter::<BootBox> {
if let Some(boot_fn) = boot_box.boot_fn.take() {
boot_fn();
}
}
}
pub fn assert_booted() {
#[cfg(debug_assertions)]
assert_eq!(
BOOT_CALLED.load(Ordering::Acquire),
true,
"booter::boot should be called after env setup"
);
}
#[macro_export]
macro_rules! call_on_boot {
($boot_fn:block) => {
::booter::inventory::submit! {
booter::BootBox {
boot_fn: booter::atomic_take::AtomicTake::new(Box::new(|| $boot_fn))
}
}
};
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::{AtomicBool, Ordering};
static CALLBACK_CALLED: AtomicBool = AtomicBool::new(false);
call_on_boot!({
CALLBACK_CALLED.store(true, Ordering::Release);
});
call_on_boot!({
println!("Hello world");
});
#[test]
#[should_panic(expected = "booter::boot should be called after env setup")]
fn it_asserts_booter_booted() {
BOOT_CALLED.store(false, Ordering::Release);
assert_booted();
}
#[test]
fn it_boots() {
boot();
assert!(CALLBACK_CALLED.load(Ordering::Acquire));
}
}