1//! Global `static`s used throughout `cuprated`.
23use std::{
4 sync::LazyLock,
5 time::{SystemTime, UNIX_EPOCH},
6};
78/// Define all the `static`s that should be always be initialized early on.
9///
10/// This wraps all `static`s inside a `LazyLock` and generates
11/// a [`init_lazylock_statics`] function that must/should be
12/// used by `main()` early on.
13macro_rules! define_init_lazylock_statics {
14 ($(
15 $( #[$attr:meta] )*
16$name:ident: $t:ty = $init_fn:expr_2021;
17 )*) => {
18/// Initialize global static `LazyLock` data.
19pub fn init_lazylock_statics() {
20 $(
21 LazyLock::force(&$name);
22 )*
23 }
2425 $(
26 $(#[$attr])*
27pub static $name: LazyLock<$t> = LazyLock::new(|| $init_fn);
28 )*
29 };
30}
3132define_init_lazylock_statics! {
33/// The start time of `cuprated`.
34START_INSTANT: SystemTime = SystemTime::now();
3536/// Start time of `cuprated` as a UNIX timestamp.
37START_INSTANT_UNIX: u64 = START_INSTANT
38 .duration_since(UNIX_EPOCH)
39 .expect("Failed to set `cuprated` startup time.")
40 .as_secs();
41}
4243#[cfg(test)]
44mod test {
45use super::*;
4647/// Sanity check for startup UNIX time.
48#[test]
49fn start_instant_unix() {
50// Fri Sep 27 01:07:13 AM UTC 2024
51assert!(*START_INSTANT_UNIX > 1727399233);
52 }
53}