danwi 0.4.2

Zero-cost dimensional analysis library with SI units, compile-time checking, and no_std support
Documentation
// Spot checks that construction and conversion compile to single float ops.
// Inspect with:
//     cargo rustc --release --example asm_check -- --emit=asm
//     cat target/release/examples/asm_check-*.s | less

use danwi::f64::{
    constants::{V, degC, kV, kg, mV},
    types::{Ampere, Ohm, Volt},
};

#[inline(never)]
#[unsafe(no_mangle)]
pub fn make_kv(x: f64) -> Volt {
    x * kV // expect: single fmul
}

#[inline(never)]
#[unsafe(no_mangle)]
pub fn make_mv(x: f64) -> Volt {
    x * mV // expect: single fdiv
}

#[inline(never)]
#[unsafe(no_mangle)]
pub fn to_mv(v: Volt) -> f64 {
    v.to(mV) // expect: single fmul
}

#[inline(never)]
#[unsafe(no_mangle)]
pub fn make_kg(x: f64) -> danwi::f64::types::Gram {
    x * kg // expect: no-op (scale reduces to 1/1)
}

#[inline(never)]
#[unsafe(no_mangle)]
pub fn add_volts(a: Volt, b: Volt) -> Volt {
    a + b // expect: single fadd
}

#[inline(never)]
#[unsafe(no_mangle)]
pub fn ohms_law(v: Volt, i: Ampere) -> Ohm {
    v / i // expect: single fdiv
}

#[inline(never)]
#[unsafe(no_mangle)]
pub fn celsius_to_kelvin(x: f64) -> f64 {
    (x * degC).value() // expect: fmul (or nothing) + fadd, no more
}

fn main() {
    let v = 1.65 * V;
    println!("v = {}", v);
    println!("mv = {}", to_mv(v));
    println!("sum = {}", add_volts(1.0 * V, 2.0 * V));
    println!("kg = {:?}", make_kg(2.5));
    println!("25 degC = {} K", celsius_to_kelvin(25.0));
}