cuda-model: EnvSetting<T> and LazySetting<T> beside EnvSwitch (a typed operator setting read once)
EnvSwitch becomes the alias of EnvSetting<bool>; EnvSetting carries any value a rule parses from one variable, LazySetting a value one function parses from several variables or from the host. STRA-338
This commit is contained in:
@@ -6,7 +6,8 @@
|
||||
//! read), and can be judged without touching the process environment through [`EnvSwitch::decide`].
|
||||
//!
|
||||
//! This file is the one place of the model crate where a variable is read and a value is cached for the process life;
|
||||
//! both are listed in `architecture.json` `exceptions`.
|
||||
//! both are listed in `architecture.json` `exceptions`. [`EnvSetting`] carries any typed value (a number, a choice, a
|
||||
//! tuple) the same way, and [`LazySetting`] a value parsed from several variables by one function.
|
||||
|
||||
use std::ffi::OsStr;
|
||||
use std::sync::OnceLock;
|
||||
@@ -49,16 +50,20 @@ pub mod rules {
|
||||
}
|
||||
}
|
||||
|
||||
/// One switch: the variable, its rule, and the answer cached on the first read.
|
||||
pub struct EnvSwitch {
|
||||
/// One operator setting: the variable, the rule that turns its text into a value, and the value cached on the first
|
||||
/// read. [`EnvSwitch`] is the setting whose value is a flag.
|
||||
pub struct EnvSetting<T> {
|
||||
name: &'static str,
|
||||
rule: Rule,
|
||||
answer: OnceLock<bool>,
|
||||
rule: fn(Option<&OsStr>) -> T,
|
||||
answer: OnceLock<T>,
|
||||
}
|
||||
|
||||
impl EnvSwitch {
|
||||
/// A switch over the variable `name`; nothing is read until the first [`Self::get`].
|
||||
pub const fn new(name: &'static str, rule: Rule) -> Self {
|
||||
/// One on/off switch: the variable, its [`Rule`], and the answer cached on the first read.
|
||||
pub type EnvSwitch = EnvSetting<bool>;
|
||||
|
||||
impl<T> EnvSetting<T> {
|
||||
/// A setting over the variable `name`; nothing is read until the first [`Self::get_ref`].
|
||||
pub const fn new(name: &'static str, rule: fn(Option<&OsStr>) -> T) -> Self {
|
||||
Self { name, rule, answer: OnceLock::new() }
|
||||
}
|
||||
|
||||
@@ -67,21 +72,57 @@ impl EnvSwitch {
|
||||
self.name
|
||||
}
|
||||
|
||||
/// The position of the switch: the environment is read on the first call only.
|
||||
/// The value of the setting: the environment is read on the first call only.
|
||||
#[inline]
|
||||
pub fn get(&self) -> bool {
|
||||
*self.answer.get_or_init(|| (self.rule)(std::env::var_os(self.name).as_deref()))
|
||||
pub fn get_ref(&self) -> &T {
|
||||
self.answer.get_or_init(|| (self.rule)(std::env::var_os(self.name).as_deref()))
|
||||
}
|
||||
|
||||
/// What the rule says for a given value, without reading the environment or the cache.
|
||||
pub fn decide(&self, value: Option<&OsStr>) -> bool {
|
||||
pub fn decide(&self, value: Option<&OsStr>) -> T {
|
||||
(self.rule)(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy> EnvSetting<T> {
|
||||
/// The value of the setting (a hot path pays one atomic load after the first call).
|
||||
#[inline]
|
||||
pub fn get(&self) -> T {
|
||||
*self.get_ref()
|
||||
}
|
||||
}
|
||||
|
||||
/// A setting that is parsed from several variables (or from the platform) by one function, run on the first read.
|
||||
/// The function is the only reader; a hot path pays one atomic load afterwards.
|
||||
pub struct LazySetting<T> {
|
||||
parse: fn() -> T,
|
||||
answer: OnceLock<T>,
|
||||
}
|
||||
|
||||
impl<T> LazySetting<T> {
|
||||
/// A setting that `parse` computes on the first [`Self::get_ref`].
|
||||
pub const fn new(parse: fn() -> T) -> Self {
|
||||
Self { parse, answer: OnceLock::new() }
|
||||
}
|
||||
|
||||
/// The value of the setting: `parse` runs on the first call only.
|
||||
#[inline]
|
||||
pub fn get_ref(&self) -> &T {
|
||||
self.answer.get_or_init(self.parse)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy> LazySetting<T> {
|
||||
/// The value of the setting (a hot path pays one atomic load after the first call).
|
||||
#[inline]
|
||||
pub fn get(&self) -> T {
|
||||
*self.get_ref()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{EnvSwitch, rules};
|
||||
use super::{EnvSetting, EnvSwitch, LazySetting, rules};
|
||||
use std::ffi::OsStr;
|
||||
|
||||
fn os(text: &str) -> Option<&OsStr> {
|
||||
@@ -119,4 +160,28 @@ mod tests {
|
||||
assert_eq!(ABSENT_ON.name(), "LELEKA_TEST_SWITCH_THAT_IS_NEVER_SET");
|
||||
assert!(!ABSENT_ON.decide(os("0")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_typed_setting_parses_its_text_once() {
|
||||
fn parse(value: Option<&OsStr>) -> usize {
|
||||
value.and_then(OsStr::to_str).and_then(|v| v.trim().parse().ok()).unwrap_or(512)
|
||||
}
|
||||
static ABSENT: EnvSetting<usize> = EnvSetting::new("LELEKA_TEST_SETTING_THAT_IS_NEVER_SET", parse);
|
||||
assert_eq!(ABSENT.get(), 512);
|
||||
assert_eq!(*ABSENT.get_ref(), 512);
|
||||
assert_eq!(ABSENT.decide(os(" 7 ")), 7);
|
||||
assert_eq!(ABSENT.decide(os("seven")), 512);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_lazy_setting_runs_its_parser_once() {
|
||||
static CALLS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||
fn parse() -> u32 {
|
||||
CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
||||
41 + 1
|
||||
}
|
||||
static SETTING: LazySetting<u32> = LazySetting::new(parse);
|
||||
assert_eq!((SETTING.get(), SETTING.get(), *SETTING.get_ref()), (42, 42, 42));
|
||||
assert_eq!(CALLS.load(std::sync::atomic::Ordering::SeqCst), 1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user