A library providing type safety and ergonomics for working with units of measurement, based on the newtype pattern.
A rich library of unit newtypes for common quantities:
use newnit::length::imperial::Foot;
use newnit::substance_amount::metric::KiloMole;
fn main() {
let length = Foot(32.0);
let oxygen_amount = KiloMole(4.0);
}
Traits representing common quantities are provided. All units in this library implement their respective quantity trait.
You can use the to_base()
method on any unit to get its representation in base
units (see individual quantity's module documentation for definition of its
respective base unit).
use newnit::Unit;
use newnit::mass::Mass;
use newnit::mass::imperial::Ounce;
fn display_weight(weight: &dyn Mass) {
println!("Weight in kilograms is: {}", weight.to_base());
}
fn main() {
let weight_in_ounces = Ounce(4.0);
display_weight(&weight_in_ounces);
}
Units implementing the same quantity are inter-convertible seamlessly:
use newnit::Unit;
use newnit::area::metric::SquareMeter;
use newnit::area::imperial::SquareFoot;
fn main() {
let bathroom_area = SquareMeter(6.0);
let bathroom_area_in_sq_feet = SquareFoot::from(&bathroom_area);
}
Incompatible conversions are compile time errors:
use newnit::Unit;
use newnit::area::metric::SquareMeter;
use newnit::mass::metric::KiloGram;
fn main() {
let bathroom_area = SquareMeter(6.0);
// This is a compile error!
let bad_conversion = KiloGram::from(&bathroom_area);
}
You can add and subtract compatible units:
use newnit::length::metric::Meter;
use newnit::length::imperial::Foot;
fn main() {
let meters = Meter(4.0);
let feet = Foot(2.0);
let sum = meters + &feet;
}
You can multiply and divide the units with a constant:
use newnit::length::metric::Meter;
use newnit::length::imperial::Foot;
fn main() {
let meters = Meter(4.0);
let times_three = 3.0 * meters;
let divided_by_negative_five = meters / -5.0;
}
You can even multiply and divide units between each other, if such operation is recognized by the library (or extended ad hoc by the user).
use newnit::length::Length;
use newnit::length::metric::Meter;
use newnit::time::metric::Second;
use newnit::velocity::metric::MeterPerSecond;
fn main() {
let meters = Meter(4.0);
let seconds = Second(3.1);
let speed: MeterPerSecond = meters.divide_time(&seconds);
}
As usual, you may define your own units that will implement the quantity traits provided by the library. You may even implement your own traits! To keep your units (or quantities) compatible with the rest of the library, you have to:
- bind your quantity traits by the
Unit
trait - implement the
Unit
trait for your units- make sure your conversions to/ from the base unit match the ones in the library
To make defining custom units easier, you can use a derive macro:
use newnit::{Unit, mass::Mass};
use newnit::derive::{Unit, Mass};
#[derive(Unit, Mass)]
#[unit(factor = 6000.0)]
#[quantity(partial_eq, ops)] // opt in to provided PartialEq and Ops implementations
struct Elephants(f64);
Available from crates.io:
cargo add newnit
Available at docs.rs.
You can check out a small example project using the library to build a unit conversion CLI here, or try it out directly in your CLI (download a release).
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
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.