Closed camerondavison closed 5 years ago
Hi! Thank you for your contribution. You're right that this isn't currently implemented; It wasn't in the spec when this project was created. This will likely be implemented with adding a Generator/Factory struct to the library that can store the state of the last ulid created.
Is this something you wanted to implement, or that you have an immediate need for?
I don't have any real immediate need. I may try and put together a pull request for it. Unsure if I will have time. Do you have any other architecture suggestions besides having a generator that keeps track of the last ulid?
The generator will likely use an algorithm similar to:
DateTime
using the chrono libraryUlid::datetime()
)Ulid::increment()
to get a monotonic ulid. (needs implementation, signature: fn increment(&self) -> Option<Ulid>
)
None
will be returned Ulid::from_datetime()
to create a new Ulid
struct Generator {
previous: Option<Ulid>;
}
impl Generator {
fn new() -> Generator { ... }
fn generate(&mut self) -> Result<Ulid, MontonicError> { ... }
}
enum MonotonicError {
Overflow,
}
I'll assign this to you for the next week. If you don't get around to it, it's no big deal.
I found some time this afternoon, and it was easier to knock out than expected. of course if this is not the direction you were expecting then I can change it up.
If you do like it then I can work on putting documentation in the readme as part of the pull request as well.
The spec says that there will be monotonicity within the same millisecond https://github.com/ulid/spec#monotonicity
It does not looks like this is enforced currently.