From e7cbaf4a9c1ec9dff79024db86fe8e7ad42f379c Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Mon, 23 Nov 2026 16:43:06 +0100 Subject: [PATCH] cuda-model: EnvSetting and LazySetting beside EnvSwitch (a typed operator setting read once) EnvSwitch becomes the alias of EnvSetting; 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 --- crates/cuda-model/src/env_switch.rs | 91 ++++++++++++++++++++++++----- 1 file changed, 78 insertions(+), 13 deletions(-) diff --git a/crates/cuda-model/src/env_switch.rs b/crates/cuda-model/src/env_switch.rs index ed6ebc56..e881f237 100644 --- a/crates/cuda-model/src/env_switch.rs +++ b/crates/cuda-model/src/env_switch.rs @@ -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 { name: &'static str, - rule: Rule, - answer: OnceLock, + rule: fn(Option<&OsStr>) -> T, + answer: OnceLock, } -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; + +impl EnvSetting { + /// 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 EnvSetting { + /// 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 { + parse: fn() -> T, + answer: OnceLock, +} + +impl LazySetting { + /// 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 LazySetting { + /// 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 = 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 = 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); + } }