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

Support counters in v7 UUIDs #755

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Support counters in v7 UUIDs #755

wants to merge 3 commits into from

Conversation

KodrAus
Copy link
Member

@KodrAus KodrAus commented May 13, 2024

For #717

This PR sketches out a solution to make the v7 support respect the input counter, while retaining existing semantics around filling with random data. It requires a bit of fudgery to keep compatibility with existing APIs, but I think the overall solution works out ok. I need to actually fill it all in fully and then explore ways to better integrate the existing Timestamp API, which may involve deprecating bits.

The gist of it is to add a usable_bits method to ClockSequence, so it can tell how many bits a given counter occupies. The UUID constructor can then do appropriate shifts and fill the rest with random bytes. That way a caller creating a v7 UUID can decide how many bits to dedicate to sequencing and how many to randomness.

As part of this PR I'm expecting to find some solution to re-seeding sequences, which will probably remain an implementation detail of the given ClockSequence.

@@ -434,6 +478,10 @@ pub mod context {
// where the clock sequence doesn't change regardless of the timestamp
self.count.fetch_add(1, Ordering::AcqRel) & (u16::MAX >> 2)
}

fn usable_bits(&self) -> usize {
14
Copy link
Member Author

Choose a reason for hiding this comment

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

We need to add another implementation here with a 42bit counter that's intended to be used by v7 UUIDs.

/// ```rust
/// # use uuid::{Uuid, Timestamp, Context};
/// let context = Context::new(42);
/// let uuid1 = Uuid::new_v7(Timestamp::from_unix(&context, 1497624119, 1234));
Copy link
Member Author

Choose a reason for hiding this comment

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

These examples should use Timestamp::from_unix_wide with a 42bit counter to steer users wanting counters in the right direction.

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.

None yet

1 participant