Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to bytes via associated type #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

burdges
Copy link

@burdges burdges commented Oct 10, 2016

This adds a ::Bytes associated type along with Into and From instances.

In fact, I'm no longer completely sure if the struct Endiannes<T>(T); approach taken by this crate always makes the most sense :

  • Ain't likely you'll want this for floats, which require you transmute them into unsigned integers, convert their endianness, and transmute them back.
  • One cannot implement every From instance, although maybe this does not matter.
  • It's obnoxious to write <LittleEndian<T> as Endian<T>>::Bytes, so you want a macro, but if you use a macro then you can do almost anything.

We might try this alternative formulation instead

struct LittleEndianBytes<T>(<LittleEndian<T> as Endian<T>>::Bytes)
    where LittleEndian<T>: Endian<T>;

so users can access the bytes with .0 and all the conversions happen with into and from calls the converted to the integer representation. There is seemingly no way to shorten the trail of traits one needs to define this however. And one must take care if trying to make this polymorphic on endianness due to rust's lack of higher kinded types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant