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

TryFromPrimitive - accept more input types #82

Open
Crzyrndm opened this issue Dec 9, 2022 · 0 comments
Open

TryFromPrimitive - accept more input types #82

Crzyrndm opened this issue Dec 9, 2022 · 0 comments

Comments

@Crzyrndm
Copy link

Crzyrndm commented Dec 9, 2022

I'm using this library to help generate a binary protocol so there's lots of repr(u8)/repr(u16) going around.

More often than not, the try_from input type is not a match for the repr type and particularly in the case of larger -> smaller types it is inconvenient to consistently need two try_from calls in a row that don't add anything

  • TryFromPrimitive is going to match [0..3] whether the input is a u8 or a usize or anything in between. The conversion to u8 is just additional book-keeping (and one with an incompatible error type -.-)
  • narrow->wide is better (generally) because of From but you still need to know the exact repr type so it's not perfect
  • signed results from calculations and unsigned encodings are also common so it's not just widening/narrowing that creates papercuts

It makes sense to only handle the repr type in Into/From but limiting TryFrom just opens the door for as (which is the whole point of this crate to begin with)

Not sure what this would best look like (list of alternate try_from reprs? I have a couple of cases where types need different handling (e.g. u8 -> u16 shifts to the high byte) so opt-in would be better).

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

1 participant