pub struct UnitRegistry { /* private fields */ }Implementations§
Source§impl UnitRegistry
impl UnitRegistry
pub fn new() -> Self
Sourcepub fn register(&mut self, unit: Unit) -> Result<(), UnitError>
pub fn register(&mut self, unit: Unit) -> Result<(), UnitError>
U3: previously this only checked for a duplicate symbol. It now also
rejects a zero base_factor (division by zero in
convert_from_base; the JSON-registration path already caught this,
direct register() calls didn’t) and a base_unit that references
an already-registered unit of a different dimension (a copy-paste
mistake that would silently cross-convert two unrelated dimensions).
It deliberately does not require base_unit to already be
registered — callers legitimately build a registry incrementally
(e.g. register_from_json into a fresh UnitRegistry::new(), where
the base symbol is asserted by convention, not by an existing
entry).
U2: Temperature is rejected outright. Unit::convert is a pure
linear scale (value * base_factor), which cannot express the
affine Celsius/Fahrenheit conversions real temperature units need;
registering one here would silently produce wrong numbers rather
than erroring, which is worse than not supporting it.
Sourcepub fn register_dimension(&mut self, _dimension: Dimension)
pub fn register_dimension(&mut self, _dimension: Dimension)
Records that a dimension is known, without asserting a base unit for
it. Previously this inserted an empty-string placeholder into
base_units when the dimension had none yet (via or_default),
which poisoned any later base_units() consumer with a bogus “”
base symbol (U3). register_base is the only source of truth for a
dimension’s actual base unit, so this is now a no-op until that’s
called.
pub fn register_base( &mut self, dimension: Dimension, base_unit: impl Into<String>, )
pub fn get_unit(&self, symbol: &str) -> Result<&Unit, UnitError>
pub fn units(&self) -> &HashMap<String, Unit>
pub fn base_units(&self) -> &HashMap<Dimension, String>
pub fn convert( &self, value: Decimal, from: &Unit, to: &Unit, ) -> Result<Decimal, UnitError>
Sourcepub fn global() -> &'static RwLock<UnitRegistry>
pub fn global() -> &'static RwLock<UnitRegistry>
U4: this is a process-wide mutable singleton. Any model’s custom
unit registrations (via the DSL’s Dimension/Unit declarations,
see parser/ast.rs) persist for the lifetime of the process and are
visible to every other model parsed afterward in the same process —
harmless for the one-shot CLI, but a cross-request leak risk for any
long-lived host (an LSP server, a WASM/Python binding kept alive
across calls). If that ever becomes a problem, thread an explicit
&UnitRegistry through instead of reaching for this singleton.
Trait Implementations§
Source§impl Clone for UnitRegistry
impl Clone for UnitRegistry
Source§fn clone(&self) -> UnitRegistry
fn clone(&self) -> UnitRegistry
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more