whippyunits-core 0.1.0

Core types and utilities for whippyunits
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
use core::ops::{Add, Mul, Neg, Sub};

use crate::num::N;

/// Axis of dimensions vector space.
///
/// All dimensions live in a 8 axis vector space.
/// For example, `m^2/s` would be vector `[0, 2, -1, 0, 0, 0, 0, 0]`.
/// These axis are defined by the
/// [International System of Quantities](https://en.wikipedia.org/wiki/International_System_of_Quantities). Additionally, we add angles as their own axis.
///
/// Addition of dimensioned quantities is only defined if their dimensions
/// are the same vector.
/// Multiplication of dimensioned quantities is defined by adding their
/// dimension vectors to form a new dimension.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum DimensionBasis {
    Mass,
    Length,
    Time,
    Current,
    Temperature,
    Amount,
    Luminosity,
    Angle,
}

impl DimensionBasis {
    /// List of all dimension basis.
    pub const ALL: [Self; 8] = [
        Self::Mass,
        Self::Length,
        Self::Time,
        Self::Current,
        Self::Temperature,
        Self::Amount,
        Self::Luminosity,
        Self::Angle,
    ];

    /// Convert a basis to it's exponents.
    ///
    /// The resulting exponents will always have one and only one exponent set to `1`.
    pub const fn exponents(&self) -> DynDimensionExponents {
        match self {
            Self::Mass => DynDimensionExponents::MASS,
            Self::Length => DynDimensionExponents::LENGTH,
            Self::Time => DynDimensionExponents::TIME,
            Self::Current => DynDimensionExponents::CURRENT,
            Self::Temperature => DynDimensionExponents::TEMPERATURE,
            Self::Amount => DynDimensionExponents::AMOUNT,
            Self::Luminosity => DynDimensionExponents::LUMINOSITY,
            Self::Angle => DynDimensionExponents::ANGLE,
        }
    }

    /// Get the symbol for the dimension.
    pub const fn symbol(&self) -> &'static str {
        match self {
            Self::Mass => "M",
            Self::Length => "L",
            Self::Time => "T",
            Self::Current => "I",
            Self::Temperature => "Θ",
            Self::Amount => "N",
            Self::Luminosity => "J",
            Self::Angle => "A",
        }
    }
}

/// Trait for type level or dynamic dimension exponents.
///
/// Dimension exponents are tracked at the type level most of the time
/// to provide compile time checks.
pub trait DimensionExponents: exponents_seal::Sealed + 'static {
    /// Get the exponent values.
    fn value(&self) -> DynDimensionExponents;

    /// Check if all the exponents are zero.
    ///
    /// When `true` that maps to a dimensionless quantity.
    fn is_zero(&self) -> bool {
        self.value() == DynDimensionExponents::ZERO
    }

    /// Get the basis this value represents.
    ///
    /// A basis is one of the eight axis in the space defining all dimensions.
    ///
    /// This returns `None` for negative basis.
    fn as_basis(&self) -> Option<DimensionBasis> {
        match self.value().0.iter().position(|&x| x == 1) {
            Some(0) => Some(DimensionBasis::Mass),
            Some(1) => Some(DimensionBasis::Length),
            Some(2) => Some(DimensionBasis::Time),
            Some(3) => Some(DimensionBasis::Current),
            Some(4) => Some(DimensionBasis::Temperature),
            Some(5) => Some(DimensionBasis::Amount),
            Some(6) => Some(DimensionBasis::Luminosity),
            Some(7) => Some(DimensionBasis::Angle),
            Some(_) => unreachable!(),
            None => None,
        }
    }
}

mod exponents_seal {
    use super::__exponents;

    pub trait Sealed {}

    impl Sealed for super::DynDimensionExponents {}

    super::__exponents_impl_trait!({
        impl Sealed for TypeDimensionExponents {}
    });
}

/// Type level dimension of a quantity.
///
/// If all dimension exponents are zero, the quantity is dimensionless.
pub struct TypeDimensionExponents<
    Mass = _M<0>,
    Length = _L<0>,
    Time = _T<0>,
    Current = _I<0>,
    Temperature = <0>,
    Amount = _N<0>,
    Luminosity = _J<0>,
    Angle = _A<0>,
> {
    #[allow(clippy::type_complexity)]
    _phantom: core::marker::PhantomData<(
        Mass,
        Length,
        Time,
        Current,
        Temperature,
        Amount,
        Luminosity,
        Angle,
    )>,
}

pub type Zero = dimension_exponents!([0, 0, 0, 0, 0, 0, 0, 0]);
pub type Mass = dimension_exponents!([1, 0, 0, 0, 0, 0, 0, 0]);
pub type Length = dimension_exponents!([0, 1, 0, 0, 0, 0, 0, 0]);
pub type Time = dimension_exponents!([0, 0, 1, 0, 0, 0, 0, 0]);
pub type Current = dimension_exponents!([0, 0, 0, 1, 0, 0, 0, 0]);
pub type Temperature = dimension_exponents!([0, 0, 0, 0, 1, 0, 0, 0]);
pub type Amount = dimension_exponents!([0, 0, 0, 0, 0, 1, 0, 0]);
pub type Luminosity = dimension_exponents!([0, 0, 0, 0, 0, 0, 1, 0]);
pub type Angle = dimension_exponents!([0, 0, 0, 0, 0, 0, 0, 1]);

/// Construct [`TypeDimensionExponents`] type from an array of exponents.
#[macro_export]
macro_rules! dimension_exponents {
    ([
        $m:expr,
        $l:expr,
        $t:expr,
        $i:expr,
        $h:expr,
        $n:expr,
        $j:expr,
        $a:expr $(,)?
    ]) => {
        $crate::dimension_exponents::TypeDimensionExponents::<
            $crate::dimension_exponents::_M<$m>,
            $crate::dimension_exponents::_L<$l>,
            $crate::dimension_exponents::_T<$t>,
            $crate::dimension_exponents::_I<$i>,
            $crate::dimension_exponents::<$h>,
            $crate::dimension_exponents::_N<$n>,
            $crate::dimension_exponents::_J<$j>,
            $crate::dimension_exponents::_A<$a>,
        >
    };
}
use dimension_exponents;

__exponents_impl!({
    impl TypeDimensionExponents {
        pub const fn new() -> Self {
            Self {
                _phantom: core::marker::PhantomData,
            }
        }

        pub const fn value_const(&self) -> DynDimensionExponents {
            DynDimensionExponents([
                MASS_EXP,
                LENGTH_EXP,
                TIME_EXP,
                CURRENT_EXP,
                TEMPERATURE_EXP,
                AMOUNT_EXP,
                LUMINOSITY_EXP,
                ANGLE_EXP,
            ])
        }
    }
});

__exponents_impl_trait!({
    impl Default for TypeDimensionExponents {
        fn default() -> __exponents!() {
            Self::new()
        }
    }
});

__exponents_impl_trait!({
    impl DimensionExponents for TypeDimensionExponents {
        fn value(&self) -> DynDimensionExponents {
            self.value_const()
        }
    }
});

__exponents_impl_op!({
    impl Add for TypeDimensionExponents {
        type Output = __exponents!();

        fn add(self, _: __exponents!(@rhs)) -> Self::Output {
            TypeDimensionExponents {
                _phantom: core::marker::PhantomData,
            }
        }
    }
});

__exponents_impl_op!({
    impl Sub for TypeDimensionExponents {
        type Output = __exponents!();

        fn sub(self, _: __exponents!(@rhs)) -> Self::Output {
            TypeDimensionExponents {
                _phantom: core::marker::PhantomData,
            }
        }
    }
});

/// The mass dimension exponent of a quantity.
pub struct _M<const EXP: i16 = 0>;

/// The length dimension exponent of a quantity.
pub struct _L<const EXP: i16 = 0>;

/// The time dimension exponent of a quantity.
pub struct _T<const EXP: i16 = 0>;

/// The current dimension exponent of a quantity.
pub struct _I<const EXP: i16 = 0>;

/// The temperature dimension exponent of a quantity.
pub struct <const EXP: i16 = 0>;

/// The amount dimension exponent of a quantity.
pub struct _N<const EXP: i16 = 0>;

/// The luminosity dimension exponent of a quantity.
pub struct _J<const EXP: i16 = 0>;

/// The angle dimension exponent of a quantity.
pub struct _A<const EXP: i16 = 0>;

/// Dimension vector.
///
/// We choose this labelling of the space's axes.
/// ```text
/// [mass, length, time, current, temperature, amount, luminosity, angle]
/// ```
/// We consider angles to be a seperate axis to distingish them from dimensionless.
///
/// Each dimensional quantity exists at some unique integer lattice point in this space.
/// For example `m/s` would be vector `[0, 1, -1, 0, 0, 0, 0, 0]`. The numbers are the
/// exponents found in the written units.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DynDimensionExponents(pub [i16; 8]);

impl DimensionExponents for DynDimensionExponents {
    fn value(&self) -> Self {
        *self
    }
}

impl DynDimensionExponents {
    pub const ZERO: Self = Zero::new().value_const();

    pub const MASS: Self = Mass::new().value_const();
    pub const LENGTH: Self = Length::new().value_const();
    pub const TIME: Self = Time::new().value_const();
    pub const CURRENT: Self = Current::new().value_const();
    pub const TEMPERATURE: Self = Temperature::new().value_const();
    pub const AMOUNT: Self = Amount::new().value_const();
    pub const LUMINOSITY: Self = Luminosity::new().value_const();
    pub const ANGLE: Self = Angle::new().value_const();
}

impl Mul<i16> for DynDimensionExponents {
    type Output = DynDimensionExponents;

    fn mul(self, rhs: i16) -> Self {
        Self([
            self.0[0] * rhs,
            self.0[1] * rhs,
            self.0[2] * rhs,
            self.0[3] * rhs,
            self.0[4] * rhs,
            self.0[5] * rhs,
            self.0[6] * rhs,
            self.0[7] * rhs,
        ])
    }
}

impl<T: DimensionExponents> Add<T> for DynDimensionExponents {
    type Output = DynDimensionExponents;

    fn add(self, rhs: T) -> Self {
        let rhs = rhs.value();

        Self([
            self.0[0] + rhs.0[0],
            self.0[1] + rhs.0[1],
            self.0[2] + rhs.0[2],
            self.0[3] + rhs.0[3],
            self.0[4] + rhs.0[4],
            self.0[5] + rhs.0[5],
            self.0[6] + rhs.0[6],
            self.0[7] + rhs.0[7],
        ])
    }
}

impl Neg for DynDimensionExponents {
    type Output = DynDimensionExponents;

    fn neg(self) -> Self {
        Self([
            -self.0[0], -self.0[1], -self.0[2], -self.0[3], -self.0[4], -self.0[5], -self.0[6],
            -self.0[7],
        ])
    }
}

impl core::fmt::Display for DynDimensionExponents {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let mut first = true;
        for (d, x) in DimensionBasis::ALL.iter().zip(self.0) {
            if x == 0 {
                continue;
            }

            if first {
                first = false;
            } else {
                write!(f, " * ")?;
            }

            write!(f, "{}^{x}", d.symbol())?;
        }

        if first {
            write!(f, "1")?;
        }

        Ok(())
    }
}

macro_rules! __exponents_impl {
    ({
        impl TypeDimensionExponents {
            $($t:tt)*
        }
    }) => {
        impl<
            const MASS_EXP: i16,
            const LENGTH_EXP: i16,
            const TIME_EXP: i16,
            const CURRENT_EXP: i16,
            const TEMPERATURE_EXP: i16,
            const AMOUNT_EXP: i16,
            const LUMINOSITY_EXP: i16,
            const ANGLE_EXP: i16,
        > __exponents!() {
            $($t)*
        }
    };
}
use __exponents_impl;

macro_rules! __exponents_impl_trait {
    ({
        impl $trait:ident for TypeDimensionExponents {
            $($t:tt)*
        }
    }) => {
        impl<
            const MASS_EXP: i16,
            const LENGTH_EXP: i16,
            const TIME_EXP: i16,
            const CURRENT_EXP: i16,
            const TEMPERATURE_EXP: i16,
            const AMOUNT_EXP: i16,
            const LUMINOSITY_EXP: i16,
            const ANGLE_EXP: i16,
        > $trait for __exponents!() {
            $($t)*
        }
    };
}
use __exponents_impl_trait;

macro_rules! __exponents_impl_op {
    ({
        impl $op:ident for TypeDimensionExponents {
            $($t:tt)*
        }
    }) => {
        impl<
            const MASS_EXP: i16,
            const LENGTH_EXP: i16,
            const TIME_EXP: i16,
            const CURRENT_EXP: i16,
            const TEMPERATURE_EXP: i16,
            const AMOUNT_EXP: i16,
            const LUMINOSITY_EXP: i16,
            const ANGLE_EXP: i16,
            const MASS_EXP1: i16,
            const LENGTH_EXP1: i16,
            const TIME_EXP1: i16,
            const CURRENT_EXP1: i16,
            const TEMPERATURE_EXP1: i16,
            const AMOUNT_EXP1: i16,
            const LUMINOSITY_EXP1: i16,
            const ANGLE_EXP1: i16,
            const MASS_EXP2: i16,
            const LENGTH_EXP2: i16,
            const TIME_EXP2: i16,
            const CURRENT_EXP2: i16,
            const TEMPERATURE_EXP2: i16,
            const AMOUNT_EXP2: i16,
            const LUMINOSITY_EXP2: i16,
            const ANGLE_EXP2: i16,
        > $op<__exponents!(@rhs)> for __exponents!(@lhs)
where
    N<MASS_EXP1>: Add<N<MASS_EXP2>, Output = N<MASS_EXP>>,
    N<LENGTH_EXP1>: Add<N<LENGTH_EXP2>, Output = N<LENGTH_EXP>>,
    N<TIME_EXP1>: Add<N<TIME_EXP2>, Output = N<TIME_EXP>>,
    N<CURRENT_EXP1>: Add<N<CURRENT_EXP2>, Output = N<CURRENT_EXP>>,
    N<TEMPERATURE_EXP1>: Add<N<TEMPERATURE_EXP2>, Output = N<TEMPERATURE_EXP>>,
    N<AMOUNT_EXP1>: Add<N<AMOUNT_EXP2>, Output = N<AMOUNT_EXP>>,
    N<LUMINOSITY_EXP1>: Add<N<LUMINOSITY_EXP2>, Output = N<LUMINOSITY_EXP>>,
    N<ANGLE_EXP1>: Add<N<ANGLE_EXP2>, Output = N<ANGLE_EXP>>,
{
            $($t)*
        }
    };
}
use __exponents_impl_op;

macro_rules! __exponents {
    () => {
        $crate::dimension_exponents!([
            MASS_EXP,
            LENGTH_EXP,
            TIME_EXP,
            CURRENT_EXP,
            TEMPERATURE_EXP,
            AMOUNT_EXP,
            LUMINOSITY_EXP,
            ANGLE_EXP,
        ])
    };
    (@lhs) => {
        $crate::dimension_exponents!([
            MASS_EXP1,
            LENGTH_EXP1,
            TIME_EXP1,
            CURRENT_EXP1,
            TEMPERATURE_EXP1,
            AMOUNT_EXP1,
            LUMINOSITY_EXP1,
            ANGLE_EXP1,
        ])
    };
    (@rhs) => {
        $crate::dimension_exponents!([
            MASS_EXP2,
            LENGTH_EXP2,
            TIME_EXP2,
            CURRENT_EXP2,
            TEMPERATURE_EXP2,
            AMOUNT_EXP2,
            LUMINOSITY_EXP2,
            ANGLE_EXP2,
        ])
    };
}
use __exponents;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn exponent_type_to_value() {
        let exp = <dimension_exponents!([1, -2, 3, -4, 5, -6, 7, -8])>::new().value();
        assert_eq!(
            exp.value(),
            DynDimensionExponents([1, -2, 3, -4, 5, -6, 7, -8])
        );
    }

    #[test]
    fn exponents_can_be_types() {
        let exp = <dimension_exponents!([0, 1, 0, 0, 0, 0, 0, 0])>::new();
        assert_eq!(exp.as_basis().unwrap(), DimensionBasis::Length);

        let exp = exp + <dimension_exponents!([0, 0, 2, 0, 0, 0, 0, 0])>::new();
        assert_eq!(format!("{}", exp.value()), "L^1 * T^2");
    }

    #[test]
    fn exponents_can_be_values() {
        let exp = DynDimensionExponents([1, 0, 0, 0, 0, 0, 0, 0]);
        assert_eq!(exp.as_basis().unwrap(), DimensionBasis::Mass);

        let exp = exp + DynDimensionExponents([0, 0, 2, 0, 0, 0, 0, 0]);
        assert_eq!(format!("{exp}"), "M^1 * T^2");
    }

    #[test]
    fn can_add_types_to_values() {
        let a = Mass::new();
        let b = DynDimensionExponents::LENGTH;
        let c = b + a;

        assert_eq!(c, DynDimensionExponents([1, 1, 0, 0, 0, 0, 0, 0]));
    }

    #[test]
    fn dimensionless_formats_as_1() {
        assert_eq!(format!("{}", DynDimensionExponents::ZERO), "1");
    }

    #[test]
    fn basis_to_exponents_to_basis_is_consistent() {
        for basis in DimensionBasis::ALL {
            assert_eq!(basis.exponents().as_basis().unwrap(), basis,);
        }
    }
}