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
pub extern crate atomic_take;
pub extern crate inventory;
use atomic_take::AtomicTake;
pub struct BootBox {
pub boot_fn: AtomicTake<Box<dyn FnOnce()>>,
}
inventory::collect!(BootBox);
pub fn boot() {
for boot_box in inventory::iter::<BootBox> {
if let Some(boot_fn) = boot_box.boot_fn.take() {
boot_fn();
}
}
}
#[macro_export]
macro_rules! call_on_boot {
($boot_fn:block) => {
use $crate::{atomic_take::AtomicTake, inventory, BootBox};
inventory::submit! {
BootBox {
boot_fn: AtomicTake::new(Box::new(|| $boot_fn))
}
}
};
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::{AtomicBool, Ordering};
static BOOT_CALLED: AtomicBool = AtomicBool::new(false);
call_on_boot!({
BOOT_CALLED.store(true, Ordering::SeqCst);
});
#[test]
fn it_boots() {
boot();
assert!(BOOT_CALLED.load(Ordering::SeqCst));
}
}