When using Generator::generate(), if the random bits of the ULID overflow (meaning the maximum value for the current millisecond has been reached), the method returns an Overflow error.
To recover and continue generating monotonic ULIDs, you must "commit" the overflow. You have two primary strategies:
commit_overflow_increment(): Increments the ULID into the next millisecond, starting the random bits at zero.commit_overflow_random(): Increments the ULID into the next millisecond, but starts the random bits with a new random value.
This ensures the generator state is updated and the next call will succeed.
use ulid::Generator;
let mut generator = Generator::new();
let ulid = match generator.generate() {
Ok(ulid) => ulid,
// If random bits overflow, commit the overflow to move to the next millisecond
Err(overflow) => overflow.commit_overflow_increment(),
};