constrandom icon indicating copy to clipboard operation
constrandom copied to clipboard

feat: Add unique const random

Open AlseinX opened this issue 4 years ago • 2 comments

The original implementation does not guarantee the generated number to be unique. This may cause problem when using it directly as a compile-time generated identifier. So I added a unique version to make sure when a duplicate number is generated, it would simply re-generate it, until all possible values have been taken.

AlseinX avatar Mar 08 '21 10:03 AlseinX

As for cases that require unique identifier, unless the identified items themselves are sequential, it would be better to use random identifier rather than sequential, otherwise some errors and vulnerabilities on identifier would be more likely to be soundless, because a valid id ± 1 would also be a valid id and sometimes they are relative. There are certain to be many other reasons and examples that random identifier is choosed over sequential, and I believe that this feature is useful for many cases.

AlseinX avatar Mar 09 '21 12:03 AlseinX

This might make sense for types like U64. But with u8 for example, I can easily imagine a case where due to dependencies more than 256 values get used. Then this creates the awkward situation where crates that build on their own don't build together (and in this case cause the build to go into an infinite loop).

I think the unique + random for smaller types like this it might be better to have tuple of (unique, random) which is used instead.

tkaitchuck avatar Jun 19 '21 22:06 tkaitchuck