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
- Host: GitHub
- URL: https://github.com/lucalewin/measurement
- Owner: lucalewin
- Created: 2022-12-31T16:02:52.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-04-29T19:18:47.000Z (about 2 years ago)
- Last Synced: 2025-02-28T13:33:48.896Z (3 months ago)
- Topics: measurement, quantity, rust, unit-safety, zero-cost-abstraction
- Language: Rust
- Homepage: https://lucalewin.dev/projects/measurement
- Size: 23.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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