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: infallible "from" for custom fields with bits that are a multiple of 8 #75

Open
whitevegagabriel opened this issue Oct 10, 2023 · 2 comments

Comments

@whitevegagabriel
Copy link

If a custom field takes up, say, 48 bits, and each bit can be arbitrarily anything, then it should be able to infallibly be converted from a [u8; 6].

But the current generated code performs a fallible try_from(u64), which would require wrangling a series of bytes into a [u8; 8], and then converted to a u64.

@hchataing
Copy link
Collaborator

Can you provide more context for this request ?
Currently the rust generator only produces TryFrom implementations for the nearest scalar type; not TryFrom<[u8;N]>.

@whitevegagabriel
Copy link
Author

whitevegagabriel commented Nov 8, 2023

For example, lets consider this field declaration:

custom_field Field : 48

Right now, I can initialize it like this:

Field::try_from(0_u64).unwrap();

However, something that is 48 bits long is exactly 6 bytes long.
That means that if I know which 6 bytes I want to initialize Field with, I first have to wrangle it into a u64, and then initialize Field in a way that may fail if I wrangle my bytes into a u64 incorrectly`.

If Field were to implement From<[u8; 6]>, then there are fewer things that could go wrong in my conversion. (ignoring endianness, which complicates matters)

Let me know if I need to clarify further!

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