Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce generate_overflowing and increment_overflowing methods #75

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Ekleog
Copy link

@Ekleog Ekleog commented Feb 2, 2024

Fixes #71

I did not add all the _with_datetime_and_source variants to avoid combinatorial explosion: people who want the _overflowing variant are most likely the ones who do not care about the details of how the ulid is generated anyway.

/// ```
pub fn generate_overflowing(&mut self) -> Ulid {
let next = Ulid::new();
if next > self.previous {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behavior differs from Generator::generate(). In the same-millisecond case generate will always increment while this will first try a random value. I'd like to preserve the existing behavior because it matches the reference implementation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to handle this one once #75 (comment) is decided, as it'll impact the things to do :)

///
/// assert!(ulid1 < ulid2);
/// ```
pub fn generate_overflowing(&mut self) -> Ulid {
Copy link
Owner

@dylanhart dylanhart Feb 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a single version of generate_overflowing is going to be added I'd prefer generate_from_datetime_with_source_overflowing. This would give the user the most choice.

However, looking at this code again, it may make sense to make the overflow value part of MonotonicError. If you're up for that change it should provide the best overall API here.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind this doesn't work since it wouldn't store into previous.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering the reason to be of generate_overflowing is for people who just want to say "I don't care", I think only having generate_from_datetime_with_source_overflowing would be counter-productive: after all, it's reasonably easy to replace it:

gen.generate_from_datetime_with_source_overflowing(time, source);
// is equivalent to
gen.generate_from_datetime_with_source(time, source)
    .unwrap_or_else(|| gen.generate_from_datetime_with_source(time + Duration::from_millis(1), source).unwrap());

So IMO this is the place where there's literally the least to gain, compared to generate_overflowing that does gain a lot compared to trying to reimplement it manually. Now, if you'd rather have all the variants, I'm all good with adding them; I was just trying to avoid combinatorial explosion of the API :)

I'll try to handle your other review comments soon! In the meantime please let me know your thoughts about the API you'd prefer so I can adjust this PR accordingly :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dylanhart WDYT about this question?

src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Infallible Generator generation
2 participants