ferrunitas 0.4.0

A type-safe unit conversion library with compile-time dimensional analysis
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
//! Dimensional type system primitives (exponent vectors & compile-time ops).
//! Most notably it contains the DimensionVector type, which represents a vector of the SI base dimensions.

use core::fmt::Debug;
use core::marker::PhantomData;
use core::ops::{Add, Div, Mul, Neg, Sub};

use num_traits::Inv;
use typenum::*;

/// Dimension exponent marker.
///
/// Implemented by typenum integers representing a single base dimension's
/// exponent (e.g. `P2`, `N1`, `Z0`). Provides conversion to a signed integer.
/// Required to be used with the dimensional vector.
pub trait Dimension: Debug + Clone + Copy + PartialOrd + PartialEq {
    fn to_int() -> i8;
}
impl<T: typenum::Integer + Debug + PartialOrd> Dimension for T {
    fn to_int() -> i8 {
        Self::to_i8()
    }
}

/// Implemented by types that carry a 7‑component dimensional signature.
///
/// Associated types correspond to exponents of base dimensions in order:
/// Mass (M), Length (L), Time (T), Electric Current (I), Temperature (Th),
/// Amount of Substance (N), Luminous Intensity (J).
pub trait Dimensioned: Debug + Clone + Copy + PartialEq + crate::model::sealed::Sealed {
    type M: Dimension;
    type L: Dimension;
    type T: Dimension;
    type I: Dimension;
    type Th: Dimension;
    type N: Dimension;
    type J: Dimension;

    fn to_array() -> [i8; 7] {
        [
            Self::M::to_int(),
            Self::L::to_int(),
            Self::T::to_int(),
            Self::I::to_int(),
            Self::Th::to_int(),
            Self::N::to_int(),
            Self::J::to_int(),
        ]
    }
}

#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
/// Concrete carrier of seven base dimension exponents.
///
/// Invariant: contains no runtime data; all information lives at the type
/// level. Construction is always zero-cost.
pub struct DimensionVector<M, L, T, I, Th, N, J>
where
    M: Dimension,
    L: Dimension,
    T: Dimension,
    I: Dimension,
    Th: Dimension,
    N: Dimension,
    J: Dimension,
{
    _phantom: PhantomData<(M, L, T, I, Th, N, J)>,
}

/// Convenience alias for a zero (all exponents 0) dimension signature.
pub type DimensionZero = DimensionVector<Z0, Z0, Z0, Z0, Z0, Z0, Z0>;

impl<M, L, T, I, Th, N, J> crate::model::sealed::Sealed for DimensionVector<M, L, T, I, Th, N, J>
where
    M: Dimension,
    L: Dimension,
    T: Dimension,
    I: Dimension,
    Th: Dimension,
    N: Dimension,
    J: Dimension,
{
}

impl<M, L, T, I, Th, N, J> Dimensioned for DimensionVector<M, L, T, I, Th, N, J>
where
    M: Dimension,
    L: Dimension,
    T: Dimension,
    I: Dimension,
    Th: Dimension,
    N: Dimension,
    J: Dimension,
{
    type M = M;
    type L = L;
    type T = T;
    type I = I;
    type Th = Th;
    type N = N;
    type J = J;
}

impl<M, L, T, I, Th, N, J> Default for DimensionVector<M, L, T, I, Th, N, J>
where
    M: Dimension,
    L: Dimension,
    T: Dimension,
    I: Dimension,
    Th: Dimension,
    N: Dimension,
    J: Dimension,
{
    fn default() -> Self {
        Self {
            _phantom: PhantomData,
        }
    }
}

// ======================
// ARITHMETIC
// ======================

/// Addition - only works for same dimensions
impl<M, L, T, I, Th, N, J> Add for DimensionVector<M, L, T, I, Th, N, J>
where
    M: Dimension,
    L: Dimension,
    T: Dimension,
    I: Dimension,
    Th: Dimension,
    N: Dimension,
    J: Dimension,
{
    type Output = Self;

    fn add(self, _: Self) -> Self::Output {
        Self::Output::default()
    }
}

/// Subtraction - only works for same dimensions
impl<M, L, T, I, Th, N, J> Sub for DimensionVector<M, L, T, I, Th, N, J>
where
    M: Dimension,
    L: Dimension,
    T: Dimension,
    I: Dimension,
    Th: Dimension,
    N: Dimension,
    J: Dimension,
{
    type Output = Self;

    fn sub(self, _: Self) -> Self::Output {
        Self::Output::default()
    }
}

/// Multiplication - adds dimensions at type level
impl<M1, L1, T1, I1, Th1, N1, J1, M2, L2, T2, I2, Th2, N2, J2>
    Mul<DimensionVector<M2, L2, T2, I2, Th2, N2, J2>>
    for DimensionVector<M1, L1, T1, I1, Th1, N1, J1>
where
    M1: Dimension + Add<M2>,
    L1: Dimension + Add<L2>,
    T1: Dimension + Add<T2>,
    I1: Dimension + Add<I2>,
    Th1: Dimension + Add<Th2>,
    N1: Dimension + Add<N2>,
    J1: Dimension + Add<J2>,
    M2: Dimension,
    L2: Dimension,
    T2: Dimension,
    I2: Dimension,
    Th2: Dimension,
    N2: Dimension,
    J2: Dimension,
    Sum<M1, M2>: Dimension,
    Sum<L1, L2>: Dimension,
    Sum<T1, T2>: Dimension,
    Sum<I1, I2>: Dimension,
    Sum<Th1, Th2>: Dimension,
    Sum<N1, N2>: Dimension,
    Sum<J1, J2>: Dimension,
{
    type Output = DimensionVector<
        Sum<M1, M2>,
        Sum<L1, L2>,
        Sum<T1, T2>,
        Sum<I1, I2>,
        Sum<Th1, Th2>,
        Sum<N1, N2>,
        Sum<J1, J2>,
    >;

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

/// Division - subtracts dimensions at type level
impl<M1, L1, T1, I1, Th1, N1, J1, M2, L2, T2, I2, Th2, N2, J2>
    Div<DimensionVector<M2, L2, T2, I2, Th2, N2, J2>>
    for DimensionVector<M1, L1, T1, I1, Th1, N1, J1>
where
    M1: Dimension + Sub<M2>,
    L1: Dimension + Sub<L2>,
    T1: Dimension + Sub<T2>,
    I1: Dimension + Sub<I2>,
    Th1: Dimension + Sub<Th2>,
    N1: Dimension + Sub<N2>,
    J1: Dimension + Sub<J2>,
    M2: Dimension,
    L2: Dimension,
    T2: Dimension,
    I2: Dimension,
    Th2: Dimension,
    N2: Dimension,
    J2: Dimension,
    Diff<M1, M2>: Dimension,
    Diff<L1, L2>: Dimension,
    Diff<T1, T2>: Dimension,
    Diff<I1, I2>: Dimension,
    Diff<Th1, Th2>: Dimension,
    Diff<N1, N2>: Dimension,
    Diff<J1, J2>: Dimension,
{
    type Output = DimensionVector<
        Diff<M1, M2>,
        Diff<L1, L2>,
        Diff<T1, T2>,
        Diff<I1, I2>,
        Diff<Th1, Th2>,
        Diff<N1, N2>,
        Diff<J1, J2>,
    >;

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

/// Inverse quantity - negates dimensions at type level
impl<M, L, T, I, Th, N, J> Inv for DimensionVector<M, L, T, I, Th, N, J>
where
    M: Neg + Dimension,
    L: Neg + Dimension,
    T: Neg + Dimension,
    I: Neg + Dimension,
    Th: Neg + Dimension,
    N: Neg + Dimension,
    J: Neg + Dimension,
    Negate<M>: Dimension,
    Negate<L>: Dimension,
    Negate<T>: Dimension,
    Negate<I>: Dimension,
    Negate<Th>: Dimension,
    Negate<N>: Dimension,
    Negate<J>: Dimension,
{
    type Output = DimensionVector<
        Negate<M>,
        Negate<L>,
        Negate<T>,
        Negate<I>,
        Negate<Th>,
        Negate<N>,
        Negate<J>,
    >;

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

/// Exponentiation of quantity - multiplies dimensions by exponent
/// (New trait cause exponent is completely given by type, no need for runtime argument)
pub trait TypePow<Exp>
where
    Exp: Integer + NonZero + ToInt<i32>,
{
    /// Resulting dimension vector.
    type Output;
}

impl<M, L, T, I, Th, N, J, Exp> TypePow<Exp> for DimensionVector<M, L, T, I, Th, N, J>
where
    M: Dimension + Mul<Exp>,
    L: Dimension + Mul<Exp>,
    T: Dimension + Mul<Exp>,
    I: Dimension + Mul<Exp>,
    Th: Dimension + Mul<Exp>,
    N: Dimension + Mul<Exp>,
    J: Dimension + Mul<Exp>,
    Exp: Integer + NonZero + ToInt<i32>,
    Prod<M, Exp>: Dimension,
    Prod<L, Exp>: Dimension,
    Prod<T, Exp>: Dimension,
    Prod<I, Exp>: Dimension,
    Prod<Th, Exp>: Dimension,
    Prod<N, Exp>: Dimension,
    Prod<J, Exp>: Dimension,
{
    type Output = DimensionVector<
        Prod<M, Exp>,
        Prod<L, Exp>,
        Prod<T, Exp>,
        Prod<I, Exp>,
        Prod<Th, Exp>,
        Prod<N, Exp>,
        Prod<J, Exp>,
    >;
}

#[cfg(test)]
mod tests {
    use super::*;
    use typenum::{N1, N2, N3, P1, P2, P3, Z0};

    // Helper function to verify dimension vector exponents
    fn assert_dimensions<D: Dimensioned>(_: D, expected: [i8; 7]) {
        assert_eq!(D::to_array(), expected);
    }

    #[test]
    fn test_dimension_trait() {
        // Test dimension exponent conversion to integers
        assert_eq!(<Z0 as Dimension>::to_int(), 0);
        assert_eq!(<P1 as Dimension>::to_int(), 1);
        assert_eq!(<P2 as Dimension>::to_int(), 2);
        assert_eq!(<N1 as Dimension>::to_int(), -1);
        assert_eq!(<N2 as Dimension>::to_int(), -2);
        assert_eq!(<P3 as Dimension>::to_int(), 3);
        assert_eq!(<N3 as Dimension>::to_int(), -3);
    }

    #[test]
    fn test_addition_same_dimensions() {
        // [M L T^-1 I^2 Th^-1 N^3 J^-2] + [M L T^-1 I^2 Th^-1 N^3 J^-2] = [M L T^-1 I^2 Th^-1 N^3 J^-2]
        let dim1 = DimensionVector::<P1, P1, N1, P2, N1, P3, N2>::default();
        let dim2 = DimensionVector::<P1, P1, N1, P2, N1, P3, N2>::default();
        let result = dim1 + dim2;
        assert_dimensions(result, [1, 1, -1, 2, -1, 3, -2]);
    }

    #[test]
    fn test_subtraction_same_dimensions() {
        // [M^2 L^-3 T^1 I^-1 Th^2 N^-2 J^3] - [M^2 L^-3 T^1 I^-1 Th^2 N^-2 J^3] = [M^2 L^-3 T^1 I^-1 Th^2 N^-2 J^3]
        let dim1 = DimensionVector::<P2, N3, P1, N1, P2, N2, P3>::default();
        let dim2 = DimensionVector::<P2, N3, P1, N1, P2, N2, P3>::default();
        let result = dim1 - dim2;
        assert_dimensions(result, [2, -3, 1, -1, 2, -2, 3]);
    }

    #[test]
    fn test_multiplication_adds_dimensions() {
        // [M^1 L^0 T^0 I^0 Th^0 N^0 J^0] × [L^1 T^-2 I^0 Th^0 N^0 J^0] = [M^1 L^1 T^-2 I^0 Th^0 N^0 J^0]
        let dim1 = DimensionVector::<P1, Z0, Z0, Z0, Z0, Z0, Z0>::default();
        let dim2 = DimensionVector::<Z0, P1, N2, Z0, Z0, Z0, Z0>::default();
        let result = dim1 * dim2;
        assert_dimensions(result, [1, 1, -2, 0, 0, 0, 0]);

        // [M^2 L^-1 T^3 I^-2 Th^1 N^-3 J^2] × [M^-1 L^2 T^-1 I^1 Th^0 N^1 J^-1]
        // = [M^1 L^1 T^2 I^-1 Th^1 N^-2 J^1]
        let dim3 = DimensionVector::<P2, N1, P3, N2, P1, N3, P2>::default();
        let dim4 = DimensionVector::<N1, P2, N1, P1, Z0, P1, N1>::default();
        let result2 = dim3 * dim4;
        assert_dimensions(result2, [1, 1, 2, -1, 1, -2, 1]);
    }

    #[test]
    fn test_division_subtracts_dimensions() {
        // [M^0 L^1 T^0 I^0 Th^0 N^0 J^0] ÷ [M^0 L^0 T^1 I^0 Th^0 N^0 J^0] = [M^0 L^1 T^-1 I^0 Th^0 N^0 J^0]
        let dim1 = DimensionVector::<Z0, P1, Z0, Z0, Z0, Z0, Z0>::default();
        let dim2 = DimensionVector::<Z0, Z0, P1, Z0, Z0, Z0, Z0>::default();
        let result = dim1 / dim2;
        assert_dimensions(result, [0, 1, -1, 0, 0, 0, 0]);

        // [M^3 L^-2 T^1 I^2 Th^-1 N^0 J^3] ÷ [M^1 L^1 T^-1 I^-1 Th^2 N^-1 J^1]
        // = [M^2 L^-3 T^2 I^3 Th^-3 N^1 J^2]
        let dim3 = DimensionVector::<P3, N2, P1, P2, N1, Z0, P3>::default();
        let dim4 = DimensionVector::<P1, P1, N1, N1, P2, N1, P1>::default();
        let result2 = dim3 / dim4;
        assert_dimensions(result2, [2, -3, 2, 3, -3, 1, 2]);
    }

    #[test]
    fn test_inverse_negates_dimensions() {
        // 1/[M^0 L^0 T^1 I^0 Th^0 N^0 J^0] = [M^0 L^0 T^-1 I^0 Th^0 N^0 J^0]
        let dim1 = DimensionVector::<Z0, Z0, P1, Z0, Z0, Z0, Z0>::default();
        let result = dim1.inv();
        assert_dimensions(result, [0, 0, -1, 0, 0, 0, 0]);

        // 1/[M^2 L^-1 T^3 I^-2 Th^1 N^-3 J^2] = [M^-2 L^1 T^-3 I^2 Th^-1 N^3 J^-2]
        let dim2 = DimensionVector::<P2, N1, P3, N2, P1, N3, P2>::default();
        let result2 = dim2.inv();
        assert_dimensions(result2, [-2, 1, -3, 2, -1, 3, -2]);
    }

    #[test]
    fn test_exponentiation() {
        // [M^0 L^1 T^0 I^0 Th^0 N^0 J^0]^2 = [M^0 L^2 T^0 I^0 Th^0 N^0 J^0]
        let res1 = <DimensionVector<Z0, P1, Z0, Z0, P2, N2, Z0> as TypePow<P2>>::Output::default();
        assert_dimensions(res1, [0, 2, 0, 0, 4, -4, 0]);

        // [M^0 L^1 T^0 I^0 Th^0 N^0 J^0]^3 = [M^0 L^3 T^0 I^0 Th^0 N^0 J^0]
        let res2 = <DimensionVector<Z0, P1, N1, Z0, P2, P3, Z0> as TypePow<P3>>::Output::default();
        assert_dimensions(res2, [0, 3, -3, 0, 6, 9, 0]);

        // [M^0 L^0 T^1 I^0 Th^0 N^0 J^0]^(-1) = [M^0 L^0 T^-1 I^0 Th^0 N^0 J^0]
        let res3 = <DimensionVector<Z0, Z0, P1, Z0, N2, Z0, N3> as TypePow<N1>>::Output::default();
        assert_dimensions(res3, [0, 0, -1, 0, 2, 0, 3]);

        // [M^1 L^-1 T^2 I^-1 Th^1 N^-1 J^1]^2 = [M^2 L^-2 T^4 I^-2 Th^2 N^-2 J^2]
        let res4 = <DimensionVector<P1, N1, P2, N1, P1, N1, P1> as TypePow<P2>>::Output::default();
        assert_dimensions(res4, [2, -2, 4, -2, 2, -2, 2]);
    }

    #[test]
    fn test_dimensionless_operations() {
        // [1] + [1] = [1]
        let result_add = DimensionZero::default() + DimensionZero::default();
        assert_dimensions(result_add, [0, 0, 0, 0, 0, 0, 0]);

        // [1] × [M L T^-1] = [M L T^-1]
        let dim1 = DimensionVector::<P1, P1, N1, N2, P2, P2, Z0>::default();
        let result_mul = DimensionZero::default() * dim1;
        assert_dimensions(result_mul, [1, 1, -1, -2, 2, 2, 0]);

        // [M] ÷ [M] = [1] (ratio of same dimension)
        let dim2 = DimensionVector::<P1, N2, N4, Z0, Z0, P2, P4>::default();
        let dim3 = DimensionVector::<P1, N2, N4, Z0, Z0, P2, P4>::default();
        let ratio = dim2 / dim3;
        assert_dimensions(ratio, [0, 0, 0, 0, 0, 0, 0]);
    }
}