const-units 0.1.1

A library that lets you check the dimensions of your quantities at compile time
Documentation

const-units

A library that lets you validate the dimensions of you quantities at compile time

Example

Okay

use const_units::prefixes::a; // `a` is the prefix for `atto` (10^-18)
use const_units::units::m; // `m` is the abbreviation for `meter`
use const_units::Quantity; // Represents a number with a dimension
// Input attometers, return square attometers
fn square_dist(
    x: Quantity<f64, { a(m) }>,
    y: Quantity<f64, { a(m) }>,
) -> Quantity<f64, { a(m).powi(2) }> {
    x.powi::<2>() + y.powi::<2>()
}
// Input attometers, return attometers
fn distance(x: Quantity<f64, { a(m) }>, y: Quantity<f64, { a(m) }>) -> Quantity<f64, { a(m) }> {
    square_dist(x, y).powf::<{ (1, 2) }>() // `(1, 2)` represents 1/2
}

Broken

fn sum(x: Quantity<f64, { m }>, y: Quantity<f64, { s }>) -> Quantity<f64, DIMENSIONLESS> {
    x + y // You can't add meters and seconds
}