Search code examples
genericsrusttypesconstantsconst-generics

Do const generics and the `typenum` crate have an overlapping purpose?


After learning about const generics and stumbling upon the typenum crate, I was confused by the fact that they seem to have an overlapping purpose - using numeric values as a type.

The only difference is that typenum provides more flexibility, such as arithmetic operations on the number types, but with less intuitive and ergonomic usage. typenum existed before const generics, and many of the features from typenum seem to be undergoing discussion to be added in the const generic syntax.

I couldn't find anything explaining the relation between these two, but all the things above is leading me to the conclusion:
"typenum is a bandage fix to the current limitations of const generics".

Is this correct? Is there more to typenum that is beyond the scope of const generics, or will it be eventually completely replaced by const generics once its features and syntaxes are more mature?


Solution

  • Yes, this is correct, and was even more correct before the introduction of min_const_generics, where not only arithmetic operations but also all of const generics was not available. Back then typenum (combined with generic-array was really useful. Today it is less useful, but due to the limitations in min_const_generics some uses still require it. Hopefully, in the future it won't be needed at all.