Newnit
A library providing type safety and ergonomics for working with units of measurement, based on the newtype pattern.
Features
Predefined newtypes
A rich library of unit newtypes for common quantities:
use Foot;
use KiloMole;
Traits representing quantities
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 Unit;
use Mass;
use Ounce;
Seamless conversion between compatible units
Units implementing the same quantity are inter-convertible seamlessly:
use Unit;
use SquareMeter;
use SquareFoot;
Compile time protection
Incompatible conversions are compile time errors:
use Unit;
use SquareMeter;
use KiloGram;
Perform common mathematical operations with the units
You can add and subtract compatible units:
use Meter;
use Foot;
You can multiply and divide the units with a constant:
use Meter;
use Foot;
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 Length;
use Meter;
use Second;
use MeterPerSecond;
Extend the library as needed
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
Unittrait - implement the
Unittrait 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 ;
use ;
// opt in to provided PartialEq and Ops implementations
;
Installation
Available from crates.io:
Documentation
Available at docs.rs.
License
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.
Contribution
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.