measurement

Crates.iomeasurement
lib.rsmeasurement
version0.3.0
sourcesrc
created_at2022-12-31 16:11:44.32917
updated_at2023-04-26 14:44:17.598927
descriptionunit safe computation in rust
homepage
repositoryhttps://github.com/lucalewin/measurement
max_upload_size
id748464
size8,468
Luca Lewin (lucalewin)

documentation

README

Measurement

Adds quantities and units to rust to allow unit safe computation with zero cost abstraction.

Note: this crate uses an unstable feature which requires you to add #![feature(generic_const_exprs)] and use the nightly compiler.

How to use

Add the generic_const_exprs feature by adding #![feature(generic_const_exprs)] to your main.rs or lib.rs file.

use measurement::si::{Length, Time, Velocity};

fn main() {
    let length = Length::new(20.0); // 20m
    let time = Time::new(5.0); // 5s
    
    // you can specify the type of the quantity but you don't have to
    // (see the example below)
    let velocity: Velocity = length / time;

    println!("{}", velocity.value);
}
use measurement::si::{Mass, Acceleration};

fn main() {
    let mass = Mass::new(50.0);
    let acceleration = Acceleration::new(10.0);

    let force = mass * acceleration;

    println!("{}", force.value);
}

Define new quantities

currently only three dimensions are supported: Time, Length, Mass

use measurement::{Quantity, Dimension};

pub type Length                  = Quantity<Dimension<1, 0, 0, 0, 0, 0, 0>>;
pub type Mass                    = Quantity<Dimension<0, 1, 0, 0, 0, 0, 0>>;
pub type Time                    = Quantity<Dimension<0, 0, 1, 0, 0, 0, 0>>;
pub type ElectricalCurrent       = Quantity<Dimension<0, 0, 0, 1, 0, 0, 0>>;
pub type ThermodynamicTemperatur = Quantity<Dimension<0, 0, 0, 0, 1, 0, 0>>;
pub type AmountOfSubstance       = Quantity<Dimension<0, 0, 0, 0, 0, 1, 0>>;
pub type LuminousIntensity       = Quantity<Dimension<0, 0, 0, 0, 0, 0, 1>>;

pub type Velocity     = Quantity<Dimension<1, 0, -1, 0, 0, 0, 0>>;
pub type Acceleration = Quantity<Dimension<1, 0, -2, 0, 0, 0, 0>>;
pub type Force        = Quantity<Dimension<1, 1, -2, 0, 0, 0, 0>>;

Commit count: 32

cargo fmt