Allow access of inner Uuid for Hyphenated, Simple, Urn #482

Closed
opened 2020-07-28 11:46:19 -05:00 by jkoudys · 1 comment
jkoudys commented 2020-07-28 11:46:19 -05:00 (Migrated from github.com)

Is your feature request related to a problem? Please describe.
This PR was recently merged to sqlx, which allows for Uuid support for the mysql driver. Not only can this encode/decode to the basic Uuid type, but we'd also included Hyphenated in there too: https://github.com/launchbadge/sqlx/pull/536

This is especially helpful, because databases have a number of ways the UUID may be serialized back, e.g. the query may select hex-encoded hyphenated strings -- an increasingly common scenario, as these UUIDs may be pulled out of string fields, or stored json. We can encode + decode to a Hyphenated (or Simple, or Urn) easily enough, but it will force us to decode from a Hyphenated string if we ever want it as raw Bytes, even though those Bytes are already part of the struct.

Describe the solution you'd like
Any way to access the inner is fine. Not sure of the name -- a From<Hyphenated> for Uuid would be okay, as would .to_uuid(), .to_inner(), etc. I'd like it to keep to the style of the crate, hence why I'm opening an issue to ask for help instead of submitting a PR on this fairly simple change.

Is it blocking?
No. It's easy to work-around, just overly complex and poor performance.

Describe alternatives you've considered
I'd considered a a Deref or AsRef trait as a way to get a &Uuid that references the interior Bytes, if you want to avoid changing ownership of those Bytes, and it'd be easy enough to .to_owned or .clone that if you want. It'd also make it easy to write generics that take Deref<Target = Uuid> or AsRef<Uuid>, so you could pass a Uuid, Simple, Hyphenated, or Urn to them and they'd all work the same way. An Into<Uuid> would be nice for similar reasons. My point against this is that a [u8; 16] can Copy just fine, as it's only 2-words long.

**Is your feature request related to a problem? Please describe.** This PR was recently merged to sqlx, which allows for Uuid support for the mysql driver. Not only can this encode/decode to the basic Uuid type, but we'd also included Hyphenated in there too: https://github.com/launchbadge/sqlx/pull/536 This is especially helpful, because databases have a number of ways the UUID may be serialized back, e.g. the query may select hex-encoded hyphenated strings -- an increasingly common scenario, as these UUIDs may be pulled out of string fields, or stored json. We can encode + decode to a Hyphenated (or Simple, or Urn) easily enough, but it will force us to decode from a Hyphenated string if we ever want it as raw Bytes, even though those Bytes are already part of the struct. **Describe the solution you'd like** Any way to access the inner is fine. Not sure of the name -- a `From<Hyphenated> for Uuid` would be okay, as would `.to_uuid()`, `.to_inner()`, etc. I'd like it to keep to the style of the crate, hence why I'm opening an issue to ask for help instead of submitting a PR on this fairly simple change. **Is it blocking?** No. It's easy to work-around, just overly complex and poor performance. **Describe alternatives you've considered** I'd considered a a `Deref` or `AsRef` trait as a way to get a `&Uuid` that references the interior Bytes, if you want to avoid changing ownership of those Bytes, and it'd be easy enough to `.to_owned` or `.clone` that if you want. It'd also make it easy to write generics that take `Deref<Target = Uuid>` or `AsRef<Uuid>`, so you could pass a Uuid, Simple, Hyphenated, or Urn to them and they'd all work the same way. An `Into<Uuid>` would be nice for similar reasons. My point against this is that a `[u8; 16]` can `Copy` just fine, as it's only 2-words long.
KodrAus commented 2020-12-21 01:28:11 -06:00 (Migrated from github.com)

Hi @jkoudys! 👋

This seems like a reasonable addition to me! I'd happily accept a range of trait impls on the adapters:

  • From<Adapter> for Uuid
  • AsRef<Uuid> for Adapter
  • Borrow<Uuid> for Adapter
  • Clone + Copy + PartialEq + Eq + PartialOrd + Ord + Hash
  • not Deref though since Adapters aren't smart pointers
  • An inherent Adapter::into_inner method that takes self and returns a Uuid
Hi @jkoudys! :wave: This seems like a reasonable addition to me! I'd happily accept a range of trait impls on the adapters: - `From<Adapter> for Uuid` - `AsRef<Uuid> for Adapter` - `Borrow<Uuid> for Adapter` - `Clone` + `Copy` + `PartialEq` + `Eq` + `PartialOrd` + `Ord` + `Hash` - not `Deref` though since `Adapter`s aren't smart pointers - An inherent `Adapter::into_inner` method that takes `self` and returns a `Uuid`
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#482
No description provided.