I'm experimenting as I learn, and I've devised a way to create instances of a struct, each of which gets assigned a (semi-)unique ID number, using a generic integer type. This involves both a
Thing (which gets assigned the ID) and a
Counter (which generates those ID numbers, monotonically increasing), both of which use the same generic type which is meant to be any unsigned integer type, requiring the following set of trait bounds:
num::Integer + num::Unsigned + Copy + std::ops::AddAssign
In the code that I've come up with, I've ended up with this same set of trait bounds written out in six different places. It will compile with fewer, but the set of bounds is logically related, because code that instantiates these types will cause the same concrete type to be used in all cases. Is there any way to write it so that this one set of trait bounds is set in just one place, so that any future change to that set will automatically propagate to every place where it's needed? For example, I might want to expand the set of allowable types by making a small adjustment to the code and using the
Clone trait instead of
Copy. It would be easy to make an error and not update it everywhere it's needed otherwise.
I'm brand new to Rust programming, and I have a suspicion that I'm just approaching the whole problem the wrong way, with a background of writing mostly object-oriented code without multiple threads. (My immediate thought for solving this problem in C++, for example, would be to use a class variable for the counter, which would get incremented in the constructor—obviously not an option in Rust, and for good reason, but that sort of thing leaves me scratching my head and wondering what the "right" way to design the solution is.)