ulid-rs icon indicating copy to clipboard operation
ulid-rs copied to clipboard

Infallible `Generator` generation

Open Ekleog opened this issue 1 year ago • 2 comments

Hey!

I see that all the methods in Generator are currently infallible. It certainly makes sense, in order to be sure that the timestamp part of the ULID stays technically correct.

However, I'd argue that most of the time the system clock is not set to millisecond accuracy, and fewer than 2**80 ULIDs are generated per second.

As such, I'd like to introduce an infallible Generator::generate_overflowing() (or similar name) method, that'd just make +1 to the u128 in case the randomly-generated ULID was before the last one output, and still allow overflow in the time bits.

Would you be interested in such a PR?

Ekleog avatar Jan 29 '24 18:01 Ekleog

Yes, this is a very good improvement.

The only real case where an overflow would occur would be getting exceptionally lucky on the random roll.

Ulid::increment_overflowing could also be added as a matching API.

dylanhart avatar Jan 31 '24 20:01 dylanhart

Cool! I have opened https://github.com/dylanhart/ulid-rs/pull/75 with the implementation :)

Ekleog avatar Feb 02 '24 15:02 Ekleog