dims_core/system/
mod.rs

1mod measure;
2mod unit_format;
3mod unit_simple;
4use core::fmt::Debug;
5use core::fmt::Display;
6
7use crate::unit_creation::MeasureSystem as MS;
8
9pub use measure::Measure;
10use num_traits::{NumOps, One};
11pub use unit_format::UnitFormat;
12pub use unit_simple::UnitSimple;
13/// The trait used to define a Measurement System
14///
15/// This defines two items for a given measure:
16/// 1. What underlying Datatype is used (EX: f32, f64, etc)
17/// 2. What unit should be used for debugging (only if the feature `str` is used)
18pub trait MeasureSystem
19where
20    Self: Sized + PartialEq + PartialOrd,
21{
22    type N: NumTrait;
23    #[cfg(feature = "str")]
24    const DEBUG_UNIT: UnitFormat<'static, Self>;
25}
26
27/// A generic super trait of required traits
28pub trait NumTrait: PartialEq + One + NumOps + Clone + Debug + Display {}
29impl<U> NumTrait for U where U: PartialEq + One + NumOps + Clone + Debug + Display {}
30
31/// Allows this MeasureSystem to transform into another via multiplication
32///
33/// - Output specifies what system will be the result (EX: Area as output for Length * Length)
34/// - Other specifies what will be multiplied by this value to get the Output( EX: Length for Length)
35pub trait MultiplyBy<OTH: MS> {
36    type Output: MS<N = OTH::N>;
37}
38/// Allows this MeasureSystem to transform into another via division
39///
40/// - Output specifies what system will be the result (EX: Length as output for Area / Length)
41/// - Other specifies what will be multiplied by this value to get the Output( EX: Length for Area)
42pub trait DivideBy<OTH: MS> {
43    type Output: MS<N = OTH::N>;
44}
45
46/// UnitTrait is used to create a unit for a MeasureSystem
47///
48/// The struct is then used to generate a unit like METRE or KELVIN
49pub trait UnitTrait {
50    type System: MS;
51    /// Generate a new Measure from this unit and value
52    fn from(&self, val: <<Self as UnitTrait>::System as MS>::N) -> Measure<Self::System>;
53    /// Convert the given value as this unit into the base unit
54    ///
55    /// EX: KILOGRAM.to_base(12.0) = 12,000.0
56    fn to_base(
57        &self,
58        val: <<Self as UnitTrait>::System as MS>::N,
59    ) -> <<Self as UnitTrait>::System as MS>::N;
60    /// Convert the value as the base unit into this unit
61    ///
62    /// EX: KILOGRAM.to_self(12,000.0) = 12.0
63    fn to_self(
64        &self,
65        val: <<Self as UnitTrait>::System as MS>::N,
66    ) -> <<Self as UnitTrait>::System as MS>::N;
67}
68
69#[cfg(feature = "std")]
70#[cfg(feature = "str")]
71pub trait UnitFormatTrait {
72    type System: MS;
73    /// Format the given unit as a string with the specified unit suffix abbreviation after it
74    fn as_string_abbr(&self, val: Measure<Self::System>) -> String;
75    /// Format the given unit as a string with the full unit suffix
76    fn as_string_full(&self, val: Measure<Self::System>) -> String;
77}