An open API service indexing awesome lists of open source software.

https://github.com/lucalewin/measurement

unit safe computation in rust
https://github.com/lucalewin/measurement

measurement quantity rust unit-safety zero-cost-abstraction

Last synced: 2 months ago
JSON representation

unit safe computation in rust

Awesome Lists containing this project

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.

```rust
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);
}
```

```rust
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

```rust
use measurement::{Quantity, Dimension};

pub type Length = Quantity>;
pub type Mass = Quantity>;
pub type Time = Quantity>;
pub type ElectricalCurrent = Quantity>;
pub type ThermodynamicTemperatur = Quantity>;
pub type AmountOfSubstance = Quantity>;
pub type LuminousIntensity = Quantity>;

pub type Velocity = Quantity>;
pub type Acceleration = Quantity>;
pub type Force = Quantity>;

```

## Limitations

- currently only seven dimensions are supported
- all quantities use the base unit
- you can't define units
- unstable feature has to be used