absolute_unit

Crates.ioabsolute_unit
lib.rsabsolute_unit
version0.9.0
created_at2025-02-10 15:57:02.874052+00
updated_at2025-02-10 15:57:02.874052+00
descriptionA system of scalars with units.
homepagehttps://github.com/terrence2/absolute_unit
repositoryhttps://github.com/terrence2/absolute_unit
max_upload_size
id1550246
size163,891
Terrence Cole (terrence2)

documentation

https://github.com/terrence2/absolute_unit

README

absolute_unit

A unit system for Rust's type system to catch unit errors at build time.

Examples

Use the type system to ensure that you get the expected units out of a calculation.

let time = seconds!(10);
let velocity = meters_per_second!(100);
let position: Length<Meters> = velocity * time;
assert_eq!(position, meters!(100));

Use IDE type introspection to discover the type of a result. IDE Type Discovery

Check type correctness even when using approximation formulas that contain intermediate values with no inherent unit-based meaning. Runtime checks only happen in #[cfg(debug_assertions)], so there is no runtime cost in release builds.

let drag: Force<Newtons> = (coef_drag.as_dyn()
            * air_density.as_dyn()
            * (velocity_cg * velocity_cg).as_dyn()
            * meters2!(1_f64).as_dyn())
        .into();

Technical Details

The unit wrappers store the underlying value as f64. Values are wrapped in OrderedFloat and the unit values provide the same derivations as the underlying OrderedFloat(f64), enabling straightforward usage in most situations. All of the type info compiles away, leaving identical performance to bare f64.

Usage

Add absolute_unit to your Cargo.toml:

absolute_unit = "0.9"

Import the prelude to get access to everything, or import ala carte, if you know what you need.

use absolute_unit::prelude::*;
Commit count: 2

cargo fmt