Little-Endian byte order support #277
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#277
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?
Microsoft uses little-endian byte order representation for its GUID structure in memory, like this:
typedef struct _GUID { UINT32 Data1; UINT16 Data2; UINT16 Data3; BYTE Data4[8]; } GUID, UUID, *PGUID, *LPGUID, *LPCGUID;Would it be possible to add the option of reading from byte arrays in little endian order, and writing to byte arrays in little endian order? The current code apparently assumes big-endian ordering 100% of the time, making it harder to interoperate in specific cases.
For instance, from_bytes_le and as_bytes_le could be special functions to read/write to/from bytes in little-endian order.
@awakecoding Thanks for proposing this. You can go ahead and write a PR if you think you can work on it else one of us will do it.
If you do open a PR, please target
breakingbranch. (we will soon be merging it intomasterbut for now we still have active development ofbreaking)@Dylan-DPC @kinggoesgaming I don't know if I should try to do it myself or let you decide how it should be done, as I am unsure how you would like this to be implemented in a proper manner. Do you have a preference for the new API? I don't mind if the internal representation remains in big endian, as long as we can easily import/export to/from little-endian order bytes.
You are always welcome to open to a PR. If we find something that can be improved, we can leave relevant comments.
Edit:
I think I agree with your assumption that internal representation should be left as-is, and only constructors should allow Little Endian.
The other way we can do this is making
byteordera public dependency and using its Endian structs. We can default to usingbyteorder::BigEndianand as needed, the library user can usebyteorder::LittleEndianinstead.But I am doubtful if this will be sound, as this may accidently bump our minimum rust version needed to
1.26. (This is on top of my head, I haven't test this yet)@kinggoesgaming Note that
byteorder's MSRV is 1.12 currently.Speaking from experience, I'd probably prefer solutions that don't incur additional public dependencies.
I agree that introducing a new public dependency is probably not a good idea
@BurntSushi we already depend on
byteorderforu128toUuidconversion and does mean minimum version of 1.26Oh, okay. I guess I got confused. Sounded like MSRV and byteorder as a public dep were related. But if you are unconditionally using u128 internally, then yeah, your MSRV has nothing to do with byteorder really.
Related - it would be handy to support the winapi-rs crate by implementing
std::convert::From<GUID>.opened a new issue #333
I thought this was the case too, but it turns out the current code doesn't make any assumptions about endianness so will read and write the fields of the uuid in the platform's native endianness. For most users that means it's probably already little-endian. Because of this our current versions of the recently added
*_beand*_lemethods aren't quite correct yet.For interoperability I would kind of expect us to pick an endianness when storing our fields and stick with it when reading to and from them (that's what I was expecting already). Then we'd really only need the
from_bytes_*andas_bytes_*for integrating with other systems that have their own representations of uuid that assume some specific endianness. When working withfrom_fieldsandto_fieldsI think we should just always convert to and from the platform's native endianness. So in the Windows case we're always going to create aGuidusing little endian fields because Windows happens to have no interest in supporting any big endian architectures.I think we'd have to be careful about making the decision of picking an explicit endianness going forward though. That could break folks storing uuids as bytes and treating them as fields in some surprising ways...
Sorry, I was totally mistaken here. We are just assuming the input integers to
from_fieldsare big endian, so all that needs to be done is to make the little endian methods properly shift their bytes.closing
I just had to use the code here:
https://github.com/uuid-rs/uuid/pull/330/files#diff-b4aea3e418ccdb71239b96952d9cddb6R1109
I rather have this as a public API if possible.