Crate const_units

source ·
Expand description

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

WARNING: Uses the experimental features generic_const_exprs and adt_const_params which are known to cause bugs

Example

Okay

use const_units::prefixes::atto; // prefix for 10^-18
use const_units::units::meter;
use const_units::Quantity; // Represents a number with a dimension

// Input attometers, return square attometers
fn square_dist(
    x: Quantity<f64, { atto(meter) }>,
    y: Quantity<f64, { atto(meter) }>,
) -> Quantity<f64, { atto(meter).powi(2) }> {
    x.powi::<2>() + y.powi::<2>()
}

// Input attometers, return attometers
fn distance(x: Quantity<f64, { atto(meter) }>, y: Quantity<f64, { atto(meter) }>) -> Quantity<f64, { atto(meter) }> {
    square_dist(x, y).powf::<{ (1, 2) }>() // `(1, 2)` represents 1/2
}

Broken

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

Modules

Structs

  • A value with dimensionality
  • Represents SI units

Functions