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

[Feature Request] ByteOrder::read<T> #143

Closed
kitlith opened this issue Feb 28, 2019 · 3 comments
Closed

[Feature Request] ByteOrder::read<T> #143

kitlith opened this issue Feb 28, 2019 · 3 comments

Comments

@kitlith
Copy link

kitlith commented Feb 28, 2019

Add

fn read<T>(buf: &[u8]) -> T;

to the ByteOrder trait.

In my case, this would be useful so that I can create a wrapper type that specifies the byte order of an integer without having to create an implementation for every integer type. i.e.

struct EndianWrapper<T, Endian>(T, PhantomData<Endian>);

// later
Endian::read<T>(src);

Downsides/Arguments against:

  • This doesn't cover u48/i48, as there is no native type for it.
  • This requires a second set of one-for-each-type impls, though read could be implemented in terms of read_foo or visa versa.
  • This could (should?) be done in a seperate library with a seperate API.

This API may have been considered and discarded previously, but I couldn't tell and wanted to double check to make sure.

@kitlith
Copy link
Author

kitlith commented Feb 28, 2019

... immediately after opening this issue I noticed read_uint, which if you make use of TryFrom/TryInto already lets you implement something similar like this: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=0fe4511284d8010049089665d17aa411

(it's not perfect, but it's better than I anticipated beforehand)

@BurntSushi
Copy link
Owner

BurntSushi commented Feb 28, 2019

It looks like there's some overlap here between this and #95 and #27.

fn read<T>(buf: &[u8]) -> T;

Are you proposing this signature as something that is actually implementable? Because I don't think it is. You need some kind of additional information about T, which implies more traits and thus more API surface area and more machinery.

Generally, I think you should just write out the case analysis for stuff like this. Of course, if you can use read_uint, then that would be better still. If you need static dispatch, then yeah, I think you should just build up your own machinery for it internally.

@kitlith
Copy link
Author

kitlith commented Feb 28, 2019

No, I don't think it's directly implementable, I just wanted to propose roughly what it'd look like to the user, i.e. read(src) -> u32 and such.

I think this overlaps more with #27 than #95, as the latter seems to be more about run-time dispatch than static dispatch. Regardless, I think your stance after reading the other issue threads is that you think this would complicate the API presented by byteorder, and should be implemented elsewhere.

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

No branches or pull requests

2 participants