I've been thinking about and searching for a crate that helps me handles physics units. There are some crates but they don't do what I want to do.

I want to see if I can implement something that performs unit calculation at compile time using Rust type system, since it's Turing complete according to some posts. I think it will be fun to write something like this. Specifically, I can define types based on International System of Units, say meter, second and kg. Then I can define derived units, newton (kg⋅m⋅s^(−2)) for example. An ideal sample of code is like

```
pub struct Meter(f32);
pub struct Kilogram(f32);
pub struct Second(f32);
// somehow to define newton as kg⋅m⋅s^(−2)
pub struct Mul<A, B>;
pub struct Order<T, const N: isize>;
type Newton = Mul<Kilogram, Mul<Meter, Order<Second, -2>>>; // #1
// in main
let meter = Meter(1.);
let kg = Kilogram(1.);
let second = Second(1.0);
let newton : Newton = meter * kg / (second * second); // need to handle Mul and Div traits
```

but this type aliasing at `#1`

cannot handle the transtivity, because we can also define `type Newton = Mul<Meter, Mul<Kilogram, Order<Second, -2>>>;`

. And it seems Rust type system cannot allow some sort of transtivity now.

Another problem is about const generics because if we multiply units like `1m * 1m`

, we will get `1m^2`

and if we define something like `struct Order<T, const N: isize>`

, we will need to somehow raise the number of N, like this:

```
pub fn same_unit_multiply<T, const N: isize, const M: isize>(a: Order<T, N>, b: Order<T, M>)
-> Order<T, { N + M }> { /*...*/ }
```

but this code won't compile and I don't know how I should do it or whether to do it like this. I think in theory, doing this calculation on const generics is perfectly fine, though, because `+`

of `isize`

is a const fn, right?

So in summary, the problems seem to be these two:

- How to allow some transitivity on nested generic type?
- How to do calculations on const generics?

If you have any other comments, please let me know as well. Thanks!