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

feat: Add NonZeroUuid type for optimized Option<Uuid> representation #779

Merged
merged 4 commits into from
Jan 14, 2025

Conversation

ab22593k
Copy link
Contributor

@ab22593k ab22593k commented Jan 4, 2025

This commit introduces a new NonZeroUuid type that wraps a Uuid and guarantees that it is not the nil UUID. This allows for a more memory-efficient representation of Option<Uuid>, as Option<NonZeroUuid> takes up the same space as Uuid.

This change improves the memory efficiency of representing optional UUIDs and provides a more convenient way to work with non-nil UUIDs.

fixes: #770

This commit introduces a new `NonZeroUuid` type that wraps a `Uuid` and guarantees that it is
not the nil UUID. This allows for a more memory-efficient representation of `Option<Uuid>`,
as `Option<NonZeroUuid>` takes up the same space as `Uuid`.

This change improves the memory efficiency of representing optional UUIDs and provides a more convenient way to work with non-nil UUIDs.

fixes: uuid-rs#770
Copy link
Member

@KodrAus KodrAus left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @ab22593k! I've left a few comments to get started.

In addition to this, I think we should consider other trait implementations on this type:

  • Debug and Display that are based on the underlying Uuid
  • serde::Serialize and other external libraries that uuid optionally depends on

I'd also like to set the expectation that we won't be adding any methods to NonNilUuid besides construction from Uuids and conversions to Uuids, otherwise it'll end up duplicating Uuids entire API on it.

It's also worth pointing out that the conversion will always succeed for any Uuid created via the Uuid::new_v* methods, since they set version/variant bits they're never nil.

src/external/nonzero_support.rs Outdated Show resolved Hide resolved
src/external/nonzero_support.rs Outdated Show resolved Hide resolved
src/external/nonzero_support.rs Outdated Show resolved Hide resolved
src/external.rs Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
@ab22593k
Copy link
Contributor Author

PTL @KodrAus

@KodrAus
Copy link
Member

KodrAus commented Jan 13, 2025

It looks like we've got some build failures with different feature combinations:

Compiling proc-macro2 v1.0.93
     Compiling toml_edit v0.22.22
     Compiling quote v1.0.38
     Compiling syn v2.0.96
     Compiling toml v0.8.19
     Compiling serde_derive v1.0.217
     Compiling trybuild v1.0.101
     Compiling uuid v1.11.1 (/home/runner/work/uuid/uuid)
  error[E0599]: no function or associated item named `new_v4` found for struct `Uuid` in the current scope
     --> src/non_nil.rs:73:26
      |
  73  |         let uuid = Uuid::new_v4();
      |                          ^^^^^^ function or associated item not found in `Uuid`
      |
     ::: src/lib.rs:453:1
      |
  453 | pub struct Uuid(Bytes);
      | --------------- function or associated item `new_v4` not found for this struct
      |
  note: if you're trying to build a new `Uuid` consider using one of the following associated functions:
        builder::<impl Uuid>::nil
        builder::<impl Uuid>::max
        builder::<impl Uuid>::from_fields
        builder::<impl Uuid>::from_fields_le
        and 10 others
     --> src/builder.rs:72:5
      |
  72  |     pub const fn nil() -> Self {
      |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  ...
  98  |     pub const fn max() -> Self {
      |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  ...
  122 |     pub const fn from_fields(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Uuid {
      |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  ...
  168 |     pub const fn from_fields_le(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Uuid {
      |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@ab22593k
Copy link
Contributor Author

We should use Uuid::builder() since the new_v4 method is not available.

src/non_nil.rs Outdated Show resolved Hide resolved
@KodrAus
Copy link
Member

KodrAus commented Jan 13, 2025

Thanks for working through this @ab22593k! I just had a few comments, but I think this is getting close now.

Copy link
Member

@KodrAus KodrAus left a comment

Choose a reason for hiding this comment

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

Thanks @ab22593k! This looks good to me

@KodrAus KodrAus merged commit 618e817 into uuid-rs:main Jan 14, 2025
21 checks passed
kodiakhq bot pushed a commit to pdylanross/fatigue that referenced this pull request Jan 15, 2025
Bumps uuid from 1.11.1 to 1.12.0.

Release notes
Sourced from uuid's releases.

1.12.0
What's Changed

feat: Add NonZeroUuid type for optimized Option<Uuid> representation by @​ab22593k in uuid-rs/uuid#779
Finalize NonNilUuid by @​KodrAus in uuid-rs/uuid#783
Prepare for 1.12.0 release by @​KodrAus in uuid-rs/uuid#784

New Contributors

@​ab22593k made their first contribution in uuid-rs/uuid#779

Full Changelog: uuid-rs/[email protected]



Commits

c5f1d02 Merge pull request #784 from uuid-rs/cargo/1.12.0
4cfbd83 fix deprecation versions
8f76175 prepare for 1.12.0 release
358eb34 Merge pull request #783 from uuid-rs/feat/non-nil
6c5099e also remove borsh from NonNilUuid for now
b12c690 fix up non nil docs
38df005 remove zerocopy from NonNilUuid for now
4021daa fix up zerocopy derives
f570b57 support equality between NonNilUuid and Uuid
4ffd872 add a few missing derives
Additional commits viewable in compare view




Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


Dependabot commands and options

You can trigger Dependabot actions by commenting on this PR:

@dependabot rebase will rebase this PR
@dependabot recreate will recreate this PR, overwriting any edits that have been made to it
@dependabot merge will merge this PR after your CI passes on it
@dependabot squash and merge will squash and merge this PR after your CI passes on it
@dependabot cancel merge will cancel a previously requested merge and block automerging
@dependabot reopen will reopen this PR if it is closed
@dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
@dependabot show <dependency name> ignore conditions will show all of the ignore conditions of the specified dependency
@dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
@dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
@dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
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.

NonZeroUuid
2 participants