I'm looking for a way to move type constraints into some kind of wrapper. As an example, in Ada you might see something like this:
type Element is Integer range 100 .. 1000;
Which is defining a new type Element that --- while still an Integer --- is bound to a specific range. There's also mod which will loop back around (super useful).
In Rust, so far I've been checking this manually within all my functions, ie:
if x < 100 || x >= 1000 {
// throw some kind of error
}
But it would be really nice to instead define a new type that performs this check for me on assignment, similar to how integers can't overflow by default. I know that we don't have inheritance, but maybe there's some kind of trait I can implement?
TL;DR: I'm sure my method isn't best practice, but what's the standard alternative?