const-units
A library that lets you validate the dimensions of your quantities at compile time and at runtime.
WARNING: Uses the experimental features generic_const_exprs and adt_const_params which are known to cause bugs. Disable the const feature if you're only interested in runtime checking.
Compile time example
Okay
use si; // Parses units at compile time
use Quantity; // Represents a number with a dimension
// Input attometers, return square attometers
}>
// Input attometers, return meters
}> > // `(1, 2)` represents 1/2
. si }>
}
assert_eq!;
Broken
# use const_units::{Quantity, units::{meter, second, DIMENSIONLESS}};
fn sum(
x: Quantity<f64, { meter }>,
y: Quantity<f64, { second }>,
) -> Quantity<f64, DIMENSIONLESS> {
x + y // You can't add meters and seconds
}
Run time example
Requires the dyn feature
use si;
use DynQuantity; // A quantity with dynamic units stored at runtime alongside the number
use InconsistentUnits; // The error returned when inconsistent units are used together
assert_eq!;
assert_eq!;
assert!;
no-std
The std feature can be disabled to allow the crate to function in no-std environments. Doing so will remove convenience methods to convert quantities directly to strings as well as prevent errors from implementing std::error::Error.