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
//! Module for base dimensions underlying the unit system.

use std::{marker::PhantomData, ops::{Add, Div, Mul, Neg, Sub}};
use num_traits::Inv;
use typenum::{
    marker_traits::{Integer, NonZero},
    PartialDiv,
};


dummy!(
    /// Trait bound for [`Dimension`] type parameters.
    pub trait Int: Integer
);


/// Integer type used for dimension exponents.
pub type Exp = i32;

/// Number of fundamental quantities.
pub const LEN: usize = 7;


/// Scalar "dimension", representing no dimension.
pub type One          = dim!(< 0, 0, 0, 0, 0, 0, 0>);
//                             L  M  T  I  Θ  N  J
pub type Length       = dim!(< 1, 0, 0, 0, 0, 0, 0>);
pub type Mass         = dim!(< 0, 1, 0, 0, 0, 0, 0>);
pub type Time         = dim!(< 0, 0, 1, 0, 0, 0, 0>);
pub type Current      = dim!(< 0, 0, 0, 1, 0, 0, 0>);
pub type Temp         = dim!(< 0, 0, 0, 0, 1, 0, 0>);
pub type Amount       = dim!(< 0, 0, 0, 0, 0, 1, 0>);
pub type Intensity    = dim!(< 0, 0, 0, 0, 0, 0, 1>);
//                             L  M  T  I  Θ  N  J
pub type Frequency    = dim!(< 0, 0,-1, 0, 0, 0, 0>);
pub type Velocity     = dim!(< 1, 0,-1, 0, 0, 0, 0>);
pub type Accel        = dim!(< 1, 0,-2, 0, 0, 0, 0>);
pub type Force        = dim!(< 1, 1,-2, 0, 0, 0, 0>);
pub type Pressure     = dim!(<-1, 1,-2, 0, 0, 0, 0>);
pub type Area         = dim!(< 2, 0, 0, 0, 0, 0, 0>);
pub type Volume       = dim!(< 3, 0, 0, 0, 0, 0, 0>);
pub type Density      = dim!(<-3, 1, 0, 0, 0, 0, 0>);
//                             L  M  T  I  Θ  N  J
pub type Charge       = dim!(< 0, 0, 1, 1, 0, 0, 0>);
pub type Torque       = dim!(< 2, 1,-2, 0, 0, 0, 0>);
pub type Energy       = dim!(< 2, 1,-2, 0, 0, 0, 0>);
pub type Power        = dim!(< 2, 1,-3, 0, 0, 0, 0>);
pub type Voltage      = dim!(< 2, 1,-3,-1, 0, 0, 0>);
pub type Resistance   = dim!(< 2, 1,-3,-2, 0, 0, 0>);
pub type Capacitance  = dim!(<-2,-1, 4, 2, 0, 0, 0>);
//                             L  M  T  I  Θ  N  J


/// Zero-size type that serves as a type-level array of exponents.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Dimension<L: Int, M: Int, T: Int, I: Int, K: Int, N: Int, J: Int> {
    _l: PhantomData<L>, _m: PhantomData<M>, _t: PhantomData<T>,
    _i: PhantomData<I>, _k: PhantomData<K>, _n: PhantomData<N>,
    _j: PhantomData<J>,
}

impl<L: Int, M: Int, T: Int, I: Int, K: Int, N: Int, J: Int>
Dimension<L, M, T, I, K, N, J> {
    pub const fn new() -> Self { Self {
        _l: PhantomData, _m: PhantomData, _t: PhantomData,
        _i: PhantomData, _k: PhantomData, _n: PhantomData,
        _j: PhantomData,
    }}
}


use private::Sealed;
mod private {
    pub trait Sealed {}
}


impl<L: Int, M: Int, T: Int, I: Int, Θ: Int, N: Int, J: Int>
Sealed for Dimension<L, M, T, I, Θ, N, J> {}


/// Trait specifying a type to be a [`Dimension`] with arbitrary exponents.
pub trait DimType: Sealed + Copy + std::fmt::Display {
    //region Definitions.
    /// Exponent typenum for Length.
    type ExpLen: Int;
    /// Exponent typenum for Mass.
    type ExpMass: Int;
    /// Exponent typenum for Time.
    type ExpTime: Int;
    /// Exponent typenum for Electrical Current.
    type ExpCurr: Int;
    /// Exponent typenum for Temperature.
    type ExpTemp: Int;
    /// Exponent typenum for Substance Amount.
    type ExpAmt: Int;
    /// Exponent typenum for Luminous Intensity.
    type ExpLum: Int;

    /// Exponent constant for Length.
    const EXP_LEN:  Exp = <Self::ExpLen as Integer>::I32;
    /// Exponent constant for Mass.
    const EXP_MASS: Exp = <Self::ExpMass as Integer>::I32;
    /// Exponent constant for Time.
    const EXP_TIME: Exp = <Self::ExpTime as Integer>::I32;
    /// Exponent constant for Electrical Current.
    const EXP_CURR: Exp = <Self::ExpCurr as Integer>::I32;
    /// Exponent constant for Temperature.
    const EXP_TEMP: Exp = <Self::ExpTemp as Integer>::I32;
    /// Exponent constant for Substance Amount.
    const EXP_AMT:  Exp = <Self::ExpAmt as Integer>::I32;
    /// Exponent constant for Luminous Intensity.
    const EXP_LUM:  Exp = <Self::ExpLum as Integer>::I32;
    //endregion

    //region Arrays.
    /// Exponents of the seven fundamental quantities.
    const ARRAY: [Exp; LEN] = [
        Self::EXP_LEN,
        Self::EXP_MASS,
        Self::EXP_TIME,
        Self::EXP_CURR,
        Self::EXP_TEMP,
        Self::EXP_AMT,
        Self::EXP_LUM,
    ];

    /// Labels of the seven fundamental quantities, paired with their exponents.
    const CHARS: [(char, Exp); LEN] = [
        ('L', Self::EXP_LEN),
        ('M', Self::EXP_MASS),
        ('T', Self::EXP_TIME),
        ('I', Self::EXP_CURR),
        ('Θ', Self::EXP_TEMP),
        ('N', Self::EXP_AMT),
        ('J', Self::EXP_LUM),
    ];
    //endregion

    /// Return a runtime representation of this dimension.
    fn dimension() -> Self;
}

impl<L: Int, M: Int, T: Int, I: Int, K: Int, N: Int, J: Int> DimType
for Dimension<L, M, T, I, K, N, J> {
    type ExpLen = L;
    type ExpMass = M;
    type ExpTime = T;
    type ExpCurr = I;
    type ExpTemp = K;
    type ExpAmt = N;
    type ExpLum = J;

    fn dimension() -> Self { Self::new() }
}


impl<L: Int, M: Int, T: Int, I: Int, K: Int, N: Int, J: Int> std::fmt::Display
for Dimension<L, M, T, I, K, N, J> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        use std::fmt::Write;

        let mut any = false;

        for (char, exp) in Self::CHARS {
            if exp != 0 {
                if any {
                    f.write_char('*')?;
                } else {
                    any = true;
                }

                f.write_char(char)?;

                if exp != 1 {
                    write!(f, "^{}", exp)?;
                }
            }
        }

        Ok(())
    }
}


/// Division.
impl<
    L1: Int, M1: Int, T1: Int, I1: Int, K1: Int, N1: Int, J1: Int,
    L2: Int, M2: Int, T2: Int, I2: Int, K2: Int, N2: Int, J2: Int,
> Div<Dimension<L2, M2, T2, I2, K2, N2, J2>>
for Dimension<L1, M1, T1, I1, K1, N1, J1> where
    L1: Sub<L2>, L1::Output: Int,
    M1: Sub<M2>, M1::Output: Int,
    T1: Sub<T2>, T1::Output: Int,
    I1: Sub<I2>, I1::Output: Int,
    K1: Sub<K2>, K1::Output: Int,
    N1: Sub<N2>, N1::Output: Int,
    J1: Sub<J2>, J1::Output: Int,
{
    type Output = Dimension<
        L1::Output, M1::Output, T1::Output,
        I1::Output, K1::Output, N1::Output,
        J1::Output,
    >;

    fn div(self, _: Dimension<L2, M2, T2, I2, K2, N2, J2>) -> Self::Output {
        Default::default()
    }
}


/// Multiplication.
impl<
    L1: Int, M1: Int, T1: Int, I1: Int, K1: Int, N1: Int, J1: Int,
    L2: Int, M2: Int, T2: Int, I2: Int, K2: Int, N2: Int, J2: Int,
> Mul<Dimension<L2, M2, T2, I2, K2, N2, J2>>
for Dimension<L1, M1, T1, I1, K1, N1, J1> where
    L1: Add<L2>, L1::Output: Int,
    M1: Add<M2>, M1::Output: Int,
    T1: Add<T2>, T1::Output: Int,
    I1: Add<I2>, I1::Output: Int,
    K1: Add<K2>, K1::Output: Int,
    N1: Add<N2>, N1::Output: Int,
    J1: Add<J2>, J1::Output: Int,
{
    type Output = Dimension<
        L1::Output, M1::Output, T1::Output,
        I1::Output, K1::Output, N1::Output,
        J1::Output,
    >;

    fn mul(self, _: Dimension<L2, M2, T2, I2, K2, N2, J2>) -> Self::Output {
        Default::default()
    }
}


/// Inversion.
impl<
    L: Int + Neg, M: Int + Neg, T: Int + Neg,
    I: Int + Neg, K: Int + Neg, N: Int + Neg,
    J: Int + Neg,
> Inv for Dimension<L, M, T, I, K, N, J> where
    L::Output: Int, M::Output: Int, T::Output: Int,
    I::Output: Int, K::Output: Int, N::Output: Int,
    J::Output: Int,
{
    type Output = Dimension<
        L::Output, M::Output, T::Output,
        I::Output, K::Output, N::Output,
        J::Output,
    >;

    fn inv(self) -> Self::Output { Default::default() }
}


/// Indicates that a [`Dimension`] may be raised to an [`Integer`] power.
pub trait DimPowType<E: Int>: DimType {
    /// The output of the operation.
    type Output: DimType;
}

impl<
    L: Int + Mul<E>, M: Int + Mul<E>, T: Int + Mul<E>,
    I: Int + Mul<E>, K: Int + Mul<E>, N: Int + Mul<E>,
    J: Int + Mul<E>,
    E: Int,
> DimPowType<E> for Dimension<L, M, T, I, K, N, J> where
    L::Output: Int, M::Output: Int, T::Output: Int,
    I::Output: Int, K::Output: Int, N::Output: Int,
    J::Output: Int,
{
    type Output = Dimension<
        L::Output, M::Output, T::Output,
        I::Output, K::Output, N::Output,
        J::Output,
    >;
}


/// Indicates that a [`Dimension`] may be taken to a [`NonZero`] [`Integer`] root.
pub trait DimRootType<D: Int + NonZero>: DimType {
    /// The output of the operation.
    type Output: DimType;
}

impl<
    L: Int + PartialDiv<D>, M: Int + PartialDiv<D>, T: Int + PartialDiv<D>,
    I: Int + PartialDiv<D>, K: Int + PartialDiv<D>, N: Int + PartialDiv<D>,
    J: Int + PartialDiv<D>,
    D: Int + NonZero,
> DimRootType<D> for Dimension<L, M, T, I, K, N, J> where
    L::Output: Int, M::Output: Int, T::Output: Int,
    I::Output: Int, K::Output: Int, N::Output: Int,
    J::Output: Int,
{
    type Output = Dimension<
        L::Output, M::Output, T::Output,
        I::Output, K::Output, N::Output,
        J::Output,
    >;
}


/// Zero-size generic representing a specific `i32` at the type level.
pub struct ExpHack<const E: Exp>;

/// Trait for associating a type with a specific [`typenum`] [`Integer`].
pub trait HasTypenum {
    /// The [`Integer`] equivalent of this type.
    type Typenum: Integer;
}

dana_macros::impl_typenums!();


/// Indicates that a [`Dimension`] may be raised to an arbitrary `i32` power.
pub trait DimPow<const E: Exp>: DimType {
    /// The output of the operation.
    type Output: DimType;
}

impl<Dim: DimType, const E: Exp> DimPow<E> for Dim where
    ExpHack<E>: HasTypenum,
    Dim: DimPowType<<ExpHack<E> as HasTypenum>::Typenum>,
{
    type Output = Dim::Output;
}


/// Indicates that a [`Dimension`] may be taken to an arbitrary `i32` root.
pub trait DimRoot<const D: Exp>: DimType {
    /// The output of the operation.
    type Output: DimType;
}

impl<Dim: DimType, const D: Exp> DimRoot<D> for Dim where
    ExpHack<D>: HasTypenum,
    <ExpHack<D> as HasTypenum>::Typenum: NonZero,
    Dim: DimRootType<<ExpHack<D> as HasTypenum>::Typenum>,
{
    type Output = Dim::Output;
}


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

    #[test]
    fn test_dimensions() {
        assert_eq!(format!("{}", Length::dimension()), "L");
        assert_eq!(format!("{}", Velocity::dimension()), "L*T^-1");
        assert_eq!(format!("{}", Accel::dimension()), "L*T^-2");

        let _a: Accel = Velocity::dimension() / Time::dimension();
        let _a: Accel = Velocity::dimension() * Time::dimension().inv();
        let _a: Length = Velocity::dimension() * Time::dimension();
        let _a: Torque = Length::dimension() * Force::dimension();
    }
}