Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cuprated: add constants & statics modules #301

Merged
merged 5 commits into from
Sep 28, 2024
Merged

Conversation

hinto-janai
Copy link
Contributor

What

Adds a constants and statics module to cuprated.

  • constants contains general constants used by cuprated, currently it contains version strings roughly equivalent to version.h
  • statics contains global static data used throughout cuprated

Some data (e.g. const DEBUG: bool) will be available in cuprate_constants so it isn't defined in constants.

Why

@github-actions github-actions bot added A-dependency Related to dependencies, or changes to a Cargo.{toml,lock} file. A-workspace Changes to a root workspace file or general repo file. A-binaries Related to binaries. labels Sep 27, 2024
Comment on lines 8 to 41
/// 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();
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have little faith that people will manually update things so newly added statics that should be initialized before anything else go in this macro, which will generate a function that initializes everything.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(people including us in the far future btw)

Comment on lines 22 to +24
fn main() {
// Initialize global static `LazyLock` data.
statics::init_lazylock_statics();
Copy link
Contributor Author

@hinto-janai hinto-janai Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The statics get initialized here.

We could add this to a broader init_everything() later that setups up logging as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for now this will be ok

@hinto-janai hinto-janai marked this pull request as ready for review September 27, 2024 01:25
Comment on lines 22 to +24
fn main() {
// Initialize global static `LazyLock` data.
statics::init_lazylock_statics();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for now this will be ok

Comment on lines 20 to 24
$(
LazyLock::force(&$name);
)*
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tabs here should be changed to spaces

@Boog900 Boog900 merged commit 12bbadd into Cuprate:main Sep 28, 2024
6 checks passed
@hinto-janai hinto-janai deleted the statics branch September 28, 2024 00:55
@hinto-janai hinto-janai mentioned this pull request Oct 10, 2024
20 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-binaries Related to binaries. A-dependency Related to dependencies, or changes to a Cargo.{toml,lock} file. A-workspace Changes to a root workspace file or general repo file.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants