This crate provides type-safe basic scientific units and constants for no_std
programs.
```rust use yaum::time::; use yaum::length::; use yaum::velocity::*;
// Simple arithmetic asserteq!(1.0 * kph, 1.0 * km / h); asserteq!(1.0 * km / min, 60.0 * km / h);
// Read value in a given unit asserteq!(60.0, (1.0 * min).s()); asserteq!(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
Define custom units and conversions using the impl_unit!
, convert_div!
and convert_unit!
macros.
```rust
use yaum::; use yaum::time::;
yaum::implunit!(ByteSize, { B: 1.0, kB: 1024.0, MB: 1024.0 * 1024.0 }); yaum::implunit!(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() { asserteq!(8.0 * b, (1.0 * B).into()); asserteq!(1.0 * kbps, 1.0 * kb/s); } ```
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"]