What's the simplest way to create a custom type that behaves like a number?
I want type-checking that prevents mixing different units in my program, but I still want to be able to easily perform calculations on the type without casting back and forth (similar case to custom Centimeters
and Inches
types).
If I create:
struct Centimeters(f64);
then I have to implement Add
, Mul
, Ord
and lots of other traits manually. That's a lot of boilerplate, and copy&pasting of this code creates a risk of breaking basic arithmetic in the program :)
Unfortunately #[derive(Add, Sub, …)]
doesn't seem to be supported. Is there another standard trait/type/crate that could achieve similar effect?