Crates.io | unit_system |
lib.rs | unit_system |
version | 0.1.0 |
source | src |
created_at | 2022-01-28 09:53:03.377824 |
updated_at | 2022-01-28 09:53:03.377824 |
description | Automatically derived typed unit systems. |
homepage | |
repository | https://github.com/carrascomj/unit_system |
max_upload_size | |
id | 523010 |
size | 7,611 |
Automatically derived typed Unit system with conversions.
use unit_system::prelude::*;
#[derive(Unit)]
pub struct Meter(pub f32);
#[derive(Unit)]
pub struct Inch(pub f64);
// we can convert between any `Unit`s whose bases define BaseIntoBase
impl BaseIntoBase<Inch> for Meter {
fn convert_base(self) -> Inch {
Inch(self.0 as f64 * 40.)
}
}
// CMeter, DMeter, HMeter, NMeter... have been automatically built
let centimeters: CMeter = CMeter(3.);
// the same for inches (non-SI units would require manual impls of Unit)
let kilo_inches: KInch = centimeters.convert_outer();
// KInch{ 0.012. }
println!("{:?}", kilo_inches);
assert!(f64::abs(kilo_inches.0 - 0.0012) < 1e-7);
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
README.md is automatically generated on CI using cargo-readme. Please, modify README.tpl or lib.rs instead (check the github worflow for more details).