oxedyne/fe2o3/fe2o3_units/src/lib.rs
2.6 KiB, 16 runs
created by r1870400018:1233, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! A library for working with scientific units, scales and dimensions. |
| 2 | //! |
| 3 | //! Provides functionality for handling physical quantities with proper units and scale prefixes |
| 4 | //! (e.g. milli, kilo, etc). Includes support for both decimal (SI) and binary scaling systems, |
| 5 | //! significant figure tracking, and unit dimension validation. |
| 6 | //! |
| 7 | //! Key features: |
| 8 | //! - SI unit system implementation with base units (metre, kilogram, second, etc) |
| 9 | //! - Decimal (SI) and binary prefix support (e.g. kilo/kibi, mega/mebi) |
| 10 | //! - Automatic scale normalisation and humanisation |
| 11 | //! - Dimensional analysis through the System trait |
| 12 | //! - Significant figure preservation in calculations |
| 13 | //! - Proper handling of zero values and edge cases |
| 14 | //! |
| 15 | //! # Examples |
| 16 | //! |
| 17 | //! Working with data sizes using binary prefixes: |
| 18 | //! ```rust |
| 19 | //! use oxedyne_fe2o3_units::{system::Units, si::SI}; |
| 20 | //! use oxedyne_fe2o3_core::prelude::*; |
| 21 | //! |
| 22 | //! fn main() -> Outcome<()> { |
| 23 | //! // Create a measurement representing 1024 bytes with 4 significant figures |
| 24 | //! let data_size = res!(Units::<SI>::bytes(1024.0, 4)); |
| 25 | //! |
| 26 | //! // Convert to a human-readable form (automatically scales to kibibytes) |
| 27 | //! let human_readable = data_size.humanise(); |
| 28 | //! assert_eq!(human_readable.val(), 1.0); |
| 29 | //! assert_eq!(human_readable.prefix(), "Ki"); |
| 30 | //! assert_eq!(human_readable.symbol(), "B"); |
| 31 | //! Ok(()) |
| 32 | //! } |
| 33 | //! ``` |
| 34 | //! |
| 35 | //! Working with SI units and decimal prefixes: |
| 36 | //! ```rust |
| 37 | //! use oxedyne_fe2o3_units::scale::Mag; |
| 38 | //! use oxedyne_fe2o3_core::prelude::*; |
| 39 | //! |
| 40 | //! fn main() -> Outcome<()> { |
| 41 | //! // Create a measurement of 1234000 microseconds with 4 significant figures |
| 42 | //! let time = res!(Mag::micro(1234000.0, 4)); |
| 43 | //! |
| 44 | //! // Convert to a human-readable form (automatically scales to seconds) |
| 45 | //! let human_time = time.humanise(); |
| 46 | //! assert_eq!(human_time.val, 1.234); |
| 47 | //! assert_eq!(human_time.prefix(), ""); // No prefix needed for base unit |
| 48 | //! Ok(()) |
| 49 | //! } |
| 50 | //! ``` |
| 51 | //! |
| 52 | //! Custom unit systems can be created by implementing the System trait: |
| 53 | //! ```rust |
| 54 | //! use oxedyne_fe2o3_units::system::System; |
| 55 | //! |
| 56 | //! #[derive(Clone, Debug, PartialEq)] |
| 57 | //! struct Currency { |
| 58 | //! symbol: &'static str, |
| 59 | //! } |
| 60 | //! |
| 61 | //! impl System for Currency { |
| 62 | //! fn base_symbol(&self) -> &'static str { |
| 63 | //! self.symbol |
| 64 | //! } |
| 65 | //! } |
| 66 | //! |
| 67 | //! impl std::fmt::Display for Currency { |
| 68 | //! fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 69 | //! write!(f, "{}", self.symbol) |
| 70 | //! } |
| 71 | //! } |
| 72 | //! ``` |
| 73 | //! |
| 74 | #![forbid(unsafe_code)] |
| 75 | pub mod dimension; |
| 76 | pub mod quantity; |
| 77 | pub mod scale; |
| 78 | pub mod si; |
| 79 | pub mod system; |