https://github.com/bugadani/yaum
Yet Another Units of Measurement library
https://github.com/bugadani/yaum
Last synced: over 1 year ago
JSON representation
Yet Another Units of Measurement library
- Host: GitHub
- URL: https://github.com/bugadani/yaum
- Owner: bugadani
- License: mit
- Created: 2020-05-08T14:51:47.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-06-23T07:05:33.000Z (almost 6 years ago)
- Last Synced: 2025-01-13T02:43:09.328Z (over 1 year ago)
- Language: Rust
- Size: 30.3 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Yet Another Units of Measurement library [](https://crates.io/crates/yaum)
========================================
This crate provides type-safe basic scientific units and constants for `no_std` programs.
[Documentation](https://docs.rs/yaum/)
Example
-------
```rust
use yaum::time::*;
use yaum::length::*;
use yaum::velocity::*;
// Simple arithmetic
assert_eq!(1.0 * kph, 1.0 * km / h);
assert_eq!(1.0 * km / min, 60.0 * km / h);
// Read value in a given unit
assert_eq!(60.0, (1.0 * min).s());
assert_eq!(1_000.0, (1.0 * km).m());
```
Currently supported units:
* `time`: Time
* `angle`: Angle, angular speed
* `frequency`: Frequency, sampling frequency
* `length`: Length
* `velocity`: Velocity, acceleration
* `electric`: Current, Voltage, Resistance
Define custom units and conversions using the `impl_unit!`, `convert_div!` and `convert_unit!` macros.
```rust
# #[macro_use] extern crate yaum;
use yaum::*;
use yaum::time::*;
yaum::impl_unit!(ByteSize, {
B: 1.0,
kB: 1024.0,
MB: 1024.0 * 1024.0
});
yaum::impl_unit!(BitSize, {
b: 1.0,
kb: 1024.0,
Mb: 1024.0 * 1024.0
});
// define conversion between the two units (1 byte = 8 bits):
yaum::convert_unit!(ByteSize, BitSize, 8.0);
yaum::impl_unit!(BitSpeed, {
bps: 1.0,
kbps: 1024.0,
Mbps: 1024.0 * 1024.0
});
// define relationship between units (BitSpeed = BitSize/Time)
yaum::convert_div!(BitSize, Time, BitSpeed);
fn main() {
assert_eq!(8.0 * b, (1.0 * B).into());
assert_eq!(1.0 * kbps, 1.0 * kb/s);
}
```
Precision
---------
By default, units are implemented on top of `f32`. Enable the `double_precision` feature for `f64`.
```TOML
[dependencies.yaum]
version = "0.1.0"
default-features = false
features = ["double_precision"]
```