uuid-rs / uuid

Generate and parse UUIDs.
https://www.crates.io/crates/uuid
Other
1.01k stars 192 forks source link

NonZeroUuid #770

Open rick-de-water opened 1 month ago

rick-de-water commented 1 month ago

Is there a NonZeroUuid type, or has one been considered, similar to the other NonZero types, such that the following is true?

assert_eq!(std::mem::size_of::<Option<NonZeroUuid>>(), std::mem::size_of::<Uuid>());
KodrAus commented 1 month ago

Hi @rick-de-water 👋 A UUID with all zeroes is actually a valid value, it’s called the nil UUID.

Are you looking at Option<Uuid> to avoid serializing them if they’re nil?

rick-de-water commented 1 month ago

Of course, and an integer with a value of zero is also valid. There are however situation where you know that you will never use that value, and you can allow the compiler to do niche optimization if you inform it. This is what happens with the NonZero types, and of course with references, since they also cannot be null.

Its actually quite common to have domain models that do not have NIL UUIDs, since they get generated at the moment of creation and never change. Having NonZeroUuids (or NonNilUuids if you want to be specific) can have a definite impact on the memory usage of a program.