Add from_uuid_bytes #145
No reviewers
Labels
No labels
breaking
bug
dependencies
good first issue
help wanted
unstable
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-uuid-rs/uuid!145
Loading…
Reference in a new issue
No description provided.
Delete branch "add-from-uuid-bytes"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This adds a constructor that uses the explicit type
UuidBytes.The main difference between this constructor and
from_bytesis that the length ofUuidBytesis fixed at the correct length, so any use offrom_uuid_bytescannot fail. As such, instead of returning aResultit simply returns auuid.A secondary reason for this is that, if
constfunctions ever move into nightly, this function would be an easy way to allow external consumers of this library to createconstUUIDs.If there are any other tests, or contribution guidelines that I've failed to include, please let me know.
@ncallaway other than the review can you run
cargo +nightly fmtaswell?@ -603,6 +603,40 @@ impl Uuid {Ok(uuid)Since we just take a copy of these bytes, and
UuidBytesis sized, maybe this could just take an ownedUuidBytesinstead of a&UuidByteshere? The clone will be really cheap, but it could be clearer that we take ownership of the raw bytes.What do you think?
@ -603,6 +603,40 @@ impl Uuid {Ok(uuid)from_bytes()also accepts a reference. So this keeps it consistent with that. Also I guess it is better to change the return type to aResultright?@ -603,6 +603,40 @@ impl Uuid {Ok(uuid)Hmm,
from_bytestakes a reference because it needs to, otherwise it's unsized. ButUuidBytesisn't unsized, so we can take it by value if we want, since we need it by value.I don't think we'd need to return a
Result, because this always succeeds.@ -603,6 +603,40 @@ impl Uuid {Ok(uuid)Yes, I deliberately didn't return a
Result, because this always succeeds, as KodrAus noted. One motivation for this function (for me) is that I want to be able to create const Uuid's (if/when const functions come out of Rust nightly). I believe (thought I'm not positive) that if this returns a Result, this function wouldn't be able to become a const function that could generate a const Uuid.I did have a reference in the signature to mirror
from_bytes(), as noted by Dylan-DPC. However, I didn't have a strong motivational reason for this, other than matching the interface. I think KodrAus makes a good point that it might be better than the consumer of this function should be aware that a copy of the bytes are going to happen. As such, I'd be happy to change the interface to take an owned UuidBytes, and require the caller to perform the.cloneif they want the copy.I can make that change tomorrow morning (PST) and send the results. I can include the nightly rustfmt change at that time, too.
@ -603,6 +603,40 @@ impl Uuid {Ok(uuid)Yeah i agree with it. Cool
As discussed, this PR now has
::from_uuid_bytestake an owned UuidBytes, instead of a reference.cargo +nightly fmthas been run as well.bors: r+
Build succeeded