Add a version of Uuid::from_fields that can't fail #150
Labels
No labels
breaking
bug
dependencies
good first issue
help wanted
unstable
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-uuid-rs/uuid#150
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
Uuid::from_fieldstakes an&[u8]ford4, so it will return an error ifd4is not 8 bytes in length. Every time I've used this method I've had an 8 byte array to hand, so it seems silly to have to callunwrapon the result. It would be nice if there was a version of this likefn from_fields(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Uuid.I realize that working with arrays is sort of a pain in Rust for several reasons. Once the const generics RFC gets implemented it should make some things simpler, like making it possible to have a method on slices that returns an array reference of a certain size.
Hi Ted,
Thanks. We will look into this. Instead of having seperate functions, I would prefer if we could use generics for this, if possible.
The earliest we can get this implemented & released is either 0.6.1 or 0.7.0.
Also you are free to send us a PR implementing this.
I'm not sure what precisely you'd want to do with generics here, can you clarify?
I was thinking if we could have just one function that returns the UUID irrespective of data. But I think that won't be possible. Will add the function as you proposed.
It's not quite the same as the proposed
from_fieldsmethod (since it takes the complete 16 byte array) but #145 offers an infallible way to get aUuidfrom its raw representation.Oh, that is indeed very similar. In the uses I've found I can't just pass
[u8; 16]because I'm reading the data from a file and it might need to be byte-swapped, unfortunately.I think that's a niche case, that should technically be handled by the library user before passing it to the library.
I am doing so, I just then don't have a method on
UuidI can call without needing to callunwrapon the result.@luser Can I see an example of how you are using this? Perhaps it might help get a better understanding on this
Sure. This code hasn't actually landed anywhere yet, but it's a proposed patch to
goblin:github.com/luser/goblin@b541e95843/src/pe/debug.rs (L106)Looking again at this, i feel that having a similar method with the only change being a different return type doesn't make much sense to me. It will add more cost ot the maintainence later and there isn't much gain including it. We could provide a function to unwrap but then it will be the same as calling unwrap from outside and there is no visible gain in it.
You are probably right. I think in the future when we have const generics and it's possible to easily get a reference to an array of a specific size from a slice designing APIs that use exactly-sized array references will be more ergonomic.
I’ve ended up circling back around to this while making a lot more things
const. Fallible methods are still tricky forconst.The approach I was thinking of was to make
from_fieldstake a&[u8; 8]and add a falliblefrom_fields_slicethat takes a&[u8]for convenience. You can also still usetry_intoto convert a&[u8]into a&[u8; 8]now.That would make
Uuid::from_fieldsmuch more useful inconstcontexts.