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

Add reordering to convert between v1 and v6 UUIDs #648

Closed
tgross35 opened this issue Dec 21, 2022 · 2 comments · Fixed by #748
Closed

Add reordering to convert between v1 and v6 UUIDs #648

tgross35 opened this issue Dec 21, 2022 · 2 comments · Fixed by #748

Comments

@tgross35
Copy link

tgross35 commented Dec 21, 2022

Motivation

According to the uuid v6+ draft, v6 UUIDs are just a rearrangement of v1 UUIDs. Being able to easily convert between them would be useful for updating anything that works with both rearranged and non-rearranged versions, plus providing an easy way to sort v1 UUIDs.

  • v1: time_low|time_mid|time_high_and_version|clk_seq_and_variant|node
  • v6: time_high|time_mid|time_low_and_version|clk_seq_hi_res|clk_seq_low|node

Relates to #523

Solution

An API along the lines of the following would be nice:

// rearrange a v1 UUID to v6
.v1_as_v6(&self) -> Self

// rearrange a v6 UUID to v1
.v6_as_v1(&self) -> Self

Alternatives

This is currently possible by manually rearranging the bytes

@tgross35
Copy link
Author

tgross35 commented Dec 21, 2022

The authors of the v6 spec actually provide some conversion algorithms here: http://gh.peabody.io/uuidv6/ under the "hacks" section

@KodrAus
Copy link
Member

KodrAus commented May 6, 2023

Hmm, I'd rather not add methods to Uuid itself that effectively become quiet no-ops depending on the version. In this case, I think we could add a method like fn node_id(&self) -> Option<[u8; 6]> that returns Some for v1 and v6 UUIDs, so that you could then write:

if let (Some(ts), Some(node_id)) = (uuid.timestamp(), uuid.node_id()) {
    Uuid::new_v6(ts, &node_id)
} else {
    uuid
};

to get yourself a sortable UUID. It's a bit verbose, but keeps the scope of the library to constructing and deconstructing UUIDs, and not straying into grab-bag utility territory. That kind of functionality is useful, but it's difficult to gauge what's worth including.

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 a pull request may close this issue.

2 participants