-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add modules * docs * test * rename * tabs -> spaces
- Loading branch information
1 parent
a072d44
commit 12bbadd
Showing
6 changed files
with
125 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//! General constants used throughout `cuprated`. | ||
|
||
use const_format::formatcp; | ||
|
||
/// `cuprated`'s semantic version (`MAJOR.MINOR.PATCH`) as string. | ||
pub const VERSION: &str = clap::crate_version!(); | ||
|
||
/// [`VERSION`] + the build type. | ||
/// | ||
/// If a debug build, the suffix is `-debug`, else it is `-release`. | ||
pub const VERSION_BUILD: &str = if cfg!(debug_assertions) { | ||
formatcp!("{VERSION}-debug") | ||
} else { | ||
formatcp!("{VERSION}-release") | ||
}; | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn version() { | ||
assert_eq!(VERSION, "0.0.1"); | ||
} | ||
|
||
#[test] | ||
fn version_build() { | ||
if cfg!(debug_assertions) { | ||
assert_eq!(VERSION_BUILD, "0.0.1-debug"); | ||
} else { | ||
assert_eq!(VERSION_BUILD, "0.0.1-release"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//! Global `static`s used throughout `cuprated`. | ||
|
||
use std::{ | ||
sync::{atomic::AtomicU64, LazyLock}, | ||
time::{SystemTime, UNIX_EPOCH}, | ||
}; | ||
|
||
/// Define all the `static`s that should be always be initialized early on. | ||
/// | ||
/// This wraps all `static`s inside a `LazyLock` and generates | ||
/// a [`init_lazylock_statics`] function that must/should be | ||
/// used by `main()` early on. | ||
macro_rules! define_init_lazylock_statics { | ||
($( | ||
$( #[$attr:meta] )* | ||
$name:ident: $t:ty = $init_fn:expr; | ||
)*) => { | ||
/// Initialize global static `LazyLock` data. | ||
pub fn init_lazylock_statics() { | ||
$( | ||
LazyLock::force(&$name); | ||
)* | ||
} | ||
|
||
$( | ||
$(#[$attr])* | ||
pub static $name: LazyLock<$t> = LazyLock::new(|| $init_fn); | ||
)* | ||
}; | ||
} | ||
|
||
define_init_lazylock_statics! { | ||
/// The start time of `cuprated`. | ||
START_INSTANT: SystemTime = SystemTime::now(); | ||
|
||
/// Start time of `cuprated` as a UNIX timestamp. | ||
START_INSTANT_UNIX: u64 = START_INSTANT | ||
.duration_since(UNIX_EPOCH) | ||
.expect("Failed to set `cuprated` startup time.") | ||
.as_secs(); | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
/// Sanity check for startup UNIX time. | ||
#[test] | ||
fn start_instant_unix() { | ||
// Fri Sep 27 01:07:13 AM UTC 2024 | ||
assert!(*START_INSTANT_UNIX > 1727399233); | ||
} | ||
} |