danwi 0.4.0

Zero-cost dimensional analysis library with SI units, compile-time checking, and no_std support
Documentation

danwi

build Crates.io Version Crates.io License docs.rs

A zero-cost dimensional analysis library for Rust with SI units, compile-time type checking, and no_std support.

Usage

use danwi::prelude::*;

// dimensions combine at the type level; mismatches are compile errors
let v = 5.0.mA() * 2.0.kOhm();
assert_eq!(v, 10.0.V());

// convert and format in any unit of the same dimension
assert_eq!(v.to(mV), 10000.0);
println!("{}", v.display_as(mV)); // 10000 mV

// values are stored in SI base units, so mixed prefixes just work
let len = 100.0.m() + 50.0 * cm + 0.001 * km;
assert_eq!(len.value(), 101.5);

// non-SI units: minute, hour, Celsius, litre, inch, ...
let temp = 25.0.celsius();
assert_eq!(temp.to(K), 298.15);
println!("{}", temp.display_as(degC)); // 25 °C

Custom units are one line each; the built-in set is itself a single units! invocation:

mod imperial {
    danwi::units! {
        mile: danwi::dimension::Length { symbol: mi, scale: 1_609_344 / 1_000 },
    }
}

use danwi::prelude::*;
use imperial::ext::F64QuantityExt as _;

assert_eq!(1.0.mile(), 1609.344.m());

Construction and conversion compile to at most one float op each, and everything in between is plain scalar arithmetic; verify with cargo rustc --release --example asm_check -- --emit=asm.