pub struct Units { /* private fields */ }Expand description
A set of unit symbols: the SI, and whatever the caller defines.
Every operation reads expressions against this set; nothing is global.
Implementations§
Source§impl Units
impl Units
Sourcepub fn define(
&mut self,
symbol: &str,
factor: f64,
expression: &str,
offset: f64,
) -> Result<(), UnitError>
pub fn define( &mut self, symbol: &str, factor: f64, expression: &str, offset: f64, ) -> Result<(), UnitError>
Defines symbol as factor × expression (+ offset SI units at
its zero, for a scale): define("cal_IT", 4.1868, "J", 0.0),
define("toe", 1e7, "kcal_IT", 0.0),
define("degF", 5.0 / 9.0, "K", 459.67 * 5.0 / 9.0).
expression may use the built-in units and every symbol defined
before. A defined unit takes decimal prefixes unless it has an offset.
A symbol that reads as a prefix on an atom (ft, the femtotonne) is
free to define: atoms are read before prefixes, so the definition wins.
§Errors
UnitError::InvalidSymbol for a symbol that is empty, starts with a
digit or a sign, or holds an operator character (. / ( ) {
} or a space); UnitError::DuplicateSymbol for one already
defined (built-in or not); UnitError::NotFinite /
UnitError::NonPositiveFactor for factor and offset; and the
errors of Units::parse for expression.
Sourcepub fn parse(&self, expression: &str) -> Result<Unit, UnitError>
pub fn parse(&self, expression: &str) -> Result<Unit, UnitError>
The unit expression stands for.
§Errors
UnitError::Malformed, UnitError::UnknownSymbol,
UnitError::OffsetCombined.
Sourcepub fn convert(
&self,
value: f64,
from: &str,
to: &str,
) -> Result<f64, UnitError>
pub fn convert( &self, value: f64, from: &str, to: &str, ) -> Result<f64, UnitError>
value read in from, in to: a reading, so a scale’s offset
applies (20 Cel is 293.15 K).
§Errors
UnitError::NotFinite for value; the errors of Units::parse;
UnitError::DimensionMismatch when the units measure different
things.
Sourcepub fn convert_difference(
&self,
value: f64,
from: &str,
to: &str,
) -> Result<f64, UnitError>
pub fn convert_difference( &self, value: f64, from: &str, to: &str, ) -> Result<f64, UnitError>
An amount of change value in from, in to: the offset cancels
(a rise of 20 Cel is a rise of 20 K).
§Errors
As Units::convert.