Adding NonNilUuid::new_v* and NonNilUuid::now_v* #901

Open
opened 2026-08-19 07:54:17 -05:00 by RBerga06 · 0 comments
RBerga06 commented 2026-08-19 07:54:17 -05:00 (Migrated from github.com)

The NonNilUuid docs say that Uuid::new_v* methods (v1, v3, v4, v5, v6, v7, v8) and Uuid::now_v* methods (v1, v6, v7) are guaranteed to create non-nil UUIDs.

However, those methods return Uuid and converting those values to NonNilUuid via NonNilUuid::new() performs an unneeded runtime check (and one has to assert that the resulting Option is actually never None). Therefore, I'm currently generating UUIDs (v4) with the following code:

// SAFETY: `Uuid::new_v4()` is always guaranteed to generate a non-nil UUID.
let id = unsafe { NonNilUuid::new_unchecked(Uuid::new_v4()) }

I was thinking a NonNilUuid::new_v4() convenience method (same for v1, v3, now_* etc.) with essentially this implementation could be useful.

I think it shouldn't be a complex change and I might be able to submit a PR for the implementation of these methods, if desired.

The [`NonNilUuid` docs](https://docs.rs/uuid/1.24.1/uuid/struct.NonNilUuid.html) say that `Uuid::new_v*` methods (v1, v3, v4, v5, v6, v7, v8) and `Uuid::now_v*` methods (v1, v6, v7) are guaranteed to create non-nil UUIDs. However, those methods return `Uuid` and converting those values to `NonNilUuid` via `NonNilUuid::new()` performs an unneeded runtime check (and one has to assert that the resulting `Option` is actually never `None`). Therefore, I'm currently generating UUIDs (v4) with the following code: ```rust // SAFETY: `Uuid::new_v4()` is always guaranteed to generate a non-nil UUID. let id = unsafe { NonNilUuid::new_unchecked(Uuid::new_v4()) } ``` I was thinking a `NonNilUuid::new_v4()` convenience method (same for v1, v3, `now_*` etc.) with essentially this implementation could be useful. I think it shouldn't be a complex change and I might be able to submit a PR for the implementation of these methods, if desired.
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-uuid-rs/uuid#901
No description provided.