ftts_kernels/startup_env.rs
1//! Audited startup-time environment defaulting.
2//!
3//! `std::env::set_var` is unsafe in edition 2024 because mutating the environment while other
4//! threads read it is undefined behavior on some platforms. This island exists so the CLI (a
5//! `forbid(unsafe_code)` crate) can install product-default switches at the top of `main`,
6//! before anything spawns a thread. It is the same class of tiny OS-interface island as
7//! `mmap.rs`.
8
9/// Sets `key` to `value` unless the user already set it.
10///
11/// # Contract
12///
13/// Call only during single-threaded process startup — in practice, as the first statements of
14/// `cli_main` before any engine, team, or runtime construction. Calling this after threads
15/// exist would be the exact hazard `set_var`'s unsafety describes.
16pub fn set_default_if_unset(key: &str, value: &str) {
17 if std::env::var_os(key).is_some() {
18 return;
19 }
20 // SAFETY: per this function's contract the process is still single-threaded, so no
21 // concurrent reader of the environment can exist.
22 unsafe { std::env::set_var(key, value) };
23}