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
98
99
//! This crate allows a simple means to register and call one time initialization functions, the idea being this could be used in conjunction with [static_init](https://crates.io/crates/static_init) or std::mem::MaybeUninit in order to create statics that can be initalized once post-main after Tokio is online and the enviroment configured. 
//! 
//! ```rust
//! booter::call_on_boot!({
//!   println("Hello World!");
//! });
//! 
//! fn main() {
//!   booter::boot();
//!   booter::assert_booted();
//! }
//! ```

#[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);

/// Call all functions captured by booter::call_on_boot.
pub fn boot() {
  if cfg!(debug_assertions) {
    assert_eq!(
      BOOT_CALLED.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed),
      Ok(false),
      "booter::boot should be called exactly once after env setup"
    );
  }

  for boot_box in inventory::iter::<BootBox> {
    if let Some(boot_fn) = boot_box.boot_fn.take() {
      boot_fn();
    }
  }
}

/// Development assertion to ensure booter::boot called exactly once. Release builds skip check
pub fn assert_booted() {
  if cfg!(debug_assertions) {
    assert_eq!(
      BOOT_CALLED.load(Ordering::Acquire),
      true,
      "booter::boot should be called exactly once after env setup"
    );
  }
}

/// Register FnOnce to be called on booter::boot
#[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 CALLBACK_CALLED: AtomicBool = AtomicBool::new(false);

  call_on_boot!({
    CALLBACK_CALLED.store(true, Ordering::Release);
  });

  #[test]
  #[should_panic(expected = "booter::boot should be called exactly once 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));
  }
}