Crate unit_types

Source
Expand description

§Unit Types

DO NOT USE IN PRODUCTION! This is a learning project and is not intended for production use. Some of the conversion numbers were automatically filled out by GitHub Copilot and have not yet been validated.

I have not yet determined how far I will take this project. Forks or contributions are welcome.

The unit_types crate is a dimensionally aware type system for Rust.

Example:

use unit_types::prelude::*; // brings in "unit"
use unit_types::Length;
let x = Length::Kilometers(1.0);
let y = Length::Meters(200.0);
assert_eq!(x - y, Length::Meters(800.0));
assert_eq!(x + y, Length::Meters(1200.0));

let z = 2.0 * (x + y); // should be in Kilometers
assert_eq!(z.value(), 2.4);
assert_eq!(z.to(unit::Meters).value(), 2400.0);

Re-exports§

pub use length::Length;
pub use time::Time;
pub use data::Data;
pub use data_rate::DataRate;

Modules§

data
This module contains the Data enum and its associated unit conversions. The Data enum is used to represent data sizes in bytes, kilobytes, megabytes, etc. The DataUnit enum is used to specify the unit of the data size. The Data enum implements the Unit and UnitConversion traits, which offer the ability to convert between different units of data sizes via the to_base and to methods.
data_rate
length
prelude
The prelude brings in necessary traits for certain operations and all the unit names. The unit names are all namespaced under unit to avoid conflicts with other names.
time
unit
The unit names are all namespaced under unit to avoid conflicts with other names. Unit names come from their respective modules and are re-exported from lib.rs. Within a given dimension module, unit enums are defined in the form DimensionUnit. For example, the length module has LengthUnit, the time module has TimeUnit, etc.