Expand description
Core type system for strongly typed physical quantities.
qtty-core provides a minimal, zero-cost units model:
- A unit is a zero-sized marker type implementing
Unit. - A value tagged with a unit is a
Quantity<U, S>, whereSis the scalar type (defaults tof64). Supported scalars includef64,f32, signed integers (i8–i128), and optionallyRational64. - Conversion is an explicit, type-checked scaling via
Quantity::to(forRealscalars) orQuantity::to_lossy(forExactscalars). - Derived units like velocity are expressed as
Per<N, D>(e.g.Meter/Second).
Most users should depend on qtty (the facade crate) unless they need direct access to these primitives.
§What this crate solves
- Compile-time separation of dimensions (length vs time vs angle, …).
- Zero runtime overhead for unit tags (phantom types only).
- Full dimensional arithmetic:
m * m → Prod<Meter, Meter>,m / s → Per<Meter, Second>,m / m → Quantity<Unitless>. Named derived units (e.g.SquareMeter) are obtained via.to(). - Automatic compile-time verification that multiplied/divided quantities produce the correct dimension.
§What this crate does not try to solve
- General-purpose symbolic simplification of arbitrary unit expressions.
Products and quotients are structural (
Prod<A, B>,Per<A, B>); use.to()to convert to named units.
§Quick start
Convert between predefined units:
use qtty_core::length::{Kilometers, Meter};
let km = Kilometers::new(1.25);
let m = km.to::<Meter>();
assert!((m.value() - 1250.0).abs() < 1e-12);Compose derived units using /:
use qtty_core::length::{Meter, Meters};
use qtty_core::time::{Second, Seconds};
use qtty_core::velocity::Velocity;
let d = Meters::new(100.0);
let t = Seconds::new(20.0);
let v: Velocity<Meter, Second> = d / t;
assert!((v.value() - 5.0).abs() < 1e-12);§no_std
Disable default features to build qtty-core without std:
[dependencies]
qtty-core = { version = "0.7.0", default-features = false }When std is disabled, floating-point math that isn’t available in core is provided via libm.
§Feature flags
std(default): enablesstdsupport.cross-unit-ops(default): enables direct cross-unit comparison operators (==,<, etc.) for built-in unit catalogs.serde: enablesserdesupport forQuantity<U, S>; serialization is the raw scalar value.pyo3: enables PyO3 bindings for Python interop via#[pyclass]and#[pymethods].
§Panics and errors
This crate does not define an error type and does not return Result from its core operations. For floating-point
scalars (f64, f32), arithmetic follows IEEE-754 behavior (NaN and infinities propagate). For integer
scalars, abs() uses saturating semantics at the minimum value (e.g. i32::MIN.abs() returns i32::MAX
instead of panicking). Standard integer overflow rules still apply to addition, subtraction, and multiplication.
§SemVer and stability
This crate is currently 0.x. Expect breaking changes between minor versions until 1.0.
Re-exports§
pub use scalar::Exact;pub use scalar::IntegerScalar;pub use scalar::Real;pub use scalar::Scalar;pub use scalar::Transcendental;pub use unit_arithmetic::QuantityDivOutput;pub use unit_arithmetic::SameDivOutput;pub use unit_arithmetic::UnitDiv;pub use unit_arithmetic::UnitMul;pub use unit_arithmetic::UnitSqrt;pub use units::acceleration;pub use units::angular;pub use units::angular_rate;pub use units::area;pub use units::dimensionless;pub use units::energy;pub use units::force;pub use units::length;pub use units::mass;pub use units::power;pub use units::pressure;pub use units::solid_angle;pub use units::temperature;pub use units::time;pub use units::velocity;pub use units::volume;
Modules§
- scalar
- Scalar traits for quantity values.
- unit_
arithmetic - Stable unit arithmetic layer:
UnitDivandUnitMultraits. Stable, macro-generated unit arithmetic layer. - units
- Predefined unit modules (grouped by dimension).
Macros§
- impl_
unit_ arithmetic_ pairs - Convenience macro that generates both division and multiplication pair tables for a set of units.
- impl_
unit_ arithmetic_ pairs_ between - Convenience macro that generates both division and multiplication impls between an existing base group and a new extra group.
- impl_
unit_ conversions - Generates all pairwise conversions and cross-unit comparisons.
- impl_
unit_ cross_ unit_ ops - Generates cross-unit
PartialEqandPartialOrdimplementations for all pairs of units. - impl_
unit_ cross_ unit_ ops_ between - Generates cross-unit
PartialEqandPartialOrdimplementations between every unit in the extra group and every unit in the base group, plus all intra-extra pairs. - impl_
unit_ division_ pairs - Generates
UnitDivimpls for all ordered pairs of distinct units. - impl_
unit_ division_ pairs_ between - Generates
UnitDivimpls between every unit in the extra group and every unit in the base group, plus all intra-extra pairs. - impl_
unit_ from_ conversions - Generates bidirectional
Fromtrait implementations for all pairs of units within a dimension. - impl_
unit_ from_ conversions_ between - Generates
Fromimplementations between every unit in the extra group and every unit in the base group, plus all intra-extra pairs. - impl_
unit_ multiplication_ pairs - Generates
UnitMulimpls for all ordered pairs of units, including self-pairs (A * A). - impl_
unit_ multiplication_ pairs_ between - Generates
UnitMulimpls between every unit in the extra group and every unit in the base group, plus all intra-extra pairs.
Structs§
- Per
- Unit representing the division of two other units.
- Prod
- Unit representing the product of two other units.
- Quantity
- A quantity with a specific unit and scalar type.
Traits§
Type Aliases§
- Acceleration
- Acceleration (L¹ · T⁻²).
- Amount
OfSubstance - Amount of substance (N¹).
- Angular
- Plane angle (A¹) — treated as an independent dimension for type safety.
- Angular
Rate - Angular rate (A¹ · T⁻¹) — angular displacement per unit time.
- Area
- Area (L²).
- Capacitance
- Electrical capacitance (M⁻¹ · L⁻² · T⁴ · I²) — SI unit: farad (F).
- Charge
- Electric charge (I¹ · T¹) — SI unit: coulomb (C = A·s).
- Current
- Electric current (I¹).
- Density
- Mass density (M¹ · L⁻³) — SI unit: kg·m⁻³.
- Dimensionless
- Dimensionless (all exponents zero).
- Energy
- Energy (M¹ · L² · T⁻²).
- Force
- Force (M¹ · L¹ · T⁻²).
- Frequency
- Frequency (T⁻¹) — reciprocal time, SI unit: hertz (Hz = s⁻¹).
- Illuminance
- Illuminance (J¹ · A² · L⁻²) — luminous flux per unit area; SI unit: lux (lx = lm·m⁻²).
- Inductance
- Electrical inductance (M¹ · L² · T⁻² · I⁻²) — SI unit: henry (H).
- Inverse
Solid Angle - Inverse solid angle (A⁻²) — used by photometric brightness scales such as
the
S10“10th-magnitude stars per square degree” unit. - Length
- Length (L¹).
- Luminous
Flux - Luminous flux (J¹ · A²) — radiant power weighted by the luminosity function; SI unit: lumen (lm = cd·sr).
- Luminous
Intensity - Luminous intensity (J¹).
- Magnetic
Flux - Magnetic flux (M¹ · L² · T⁻² · I⁻¹) — SI unit: weber (Wb).
- Magnetic
Flux Density - Magnetic flux density / magnetic field (M¹ · T⁻² · I⁻¹) — SI unit: tesla (T).
- Mass
- Mass (M¹).
- Photon
Radiance - Photon radiance (L⁻² · T⁻¹ · A⁻²) — photon count per area per time per solid angle (e.g. ph·cm⁻²·s⁻¹·sr⁻¹). Photons are treated as dimensionless counts.
- Power
- Power (M¹ · L² · T⁻³).
- Pressure
- Pressure (M¹ · L⁻¹ · T⁻²) — equivalently, force per unit area (N/m²).
- Quantity32
- A quantity backed by
f32. - Quantity64
- A quantity backed by
f64(the default). - Quantity
I8 - A quantity backed by
i8. - Quantity
I16 - A quantity backed by
i16. - Quantity
I32 - A quantity backed by
i32. - Quantity
I64 - A quantity backed by
i64. - Quantity
I128 - A quantity backed by
i128. - Radiance
- Radiance (M¹ · T⁻³ · A⁻²) — radiant power per unit area per unit solid angle (e.g. W·m⁻²·sr⁻¹).
- Resistance
- Electrical resistance (M¹ · L² · T⁻³ · I⁻²) — SI unit: ohm (Ω).
- Solid
Angle - Solid angle (A²) — plane angle squared (e.g. steradian, square degree).
- Spectral
Photon Radiance - Spectral photon radiance per unit wavelength (L⁻³ · T⁻¹ · A⁻²) — photon radiance per unit wavelength (e.g. ph·cm⁻²·s⁻¹·sr⁻¹·Å⁻¹).
- Spectral
Radiance - Spectral radiance per unit wavelength (L⁻¹ · M¹ · T⁻³ · A⁻²) — radiance per unit wavelength (e.g. W·m⁻²·sr⁻¹·nm⁻¹).
- Temperature
- Thermodynamic temperature (Θ¹).
- Time
- Time (T¹).
- Velocity
- Velocity (L¹ · T⁻¹).
- Voltage
- Voltage / electromotive force (M¹ · L² · T⁻³ · I⁻¹) — SI unit: volt (V).
- Volume
- Volume (L³).