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

Support FromBytes for [u8; N] #210

Open
isomer opened this issue Feb 17, 2023 · 1 comment
Open

Support FromBytes for [u8; N] #210

isomer opened this issue Feb 17, 2023 · 1 comment
Assignees

Comments

@isomer
Copy link

isomer commented Feb 17, 2023

Would adding this feature cause a breaking change?
No

Is your feature request related to a problem? Please describe.

Some attributes return fixed length byte arrays. (eg a mac address, [u8;6], or the 802.11 VHT MCS Set [u8;32] etc). Some types (eg IP Addresses) have conversions from fixed sized arrays, but not from slices.

Describe the solution you'd like

I would like attr.get_payload_as::<[u8;32]>() to work, which I think is approximately:

impl<const N: usize> FromBytes for [u8;N] { ... }
impl<const N: usize> ToBytes for [u8;N] { ... }
impl<const N: usize> Size for [u8;N] { ... }

(I don't understand the difference between FromBytes and FromBytesWithInput, maybe this should be the WithInput variant? I dunno. This also means I don't understand when to use .get_payload_as() vs .get_payload_as_with_len())

It might also be possible (although I imagine a bit tricker to implement in an efficient manner) to have:

impl<const N: usize, T: Size + FromBytes> FromBytes for [T; N] { ... }
impl<const N: usize, T: Size + ToBytes> ToBytes for [T;N] { ... }
impl<const N: usize, T: Size + FromBytes> Size for [T; N] { ... }

which would allow for arbitrary arrays of u8, u16, u32 and even more complex types. (Interface counters are often returned as arrays of u32 or u64 for instance).

Describe alternatives you've considered
Currently I get the reference to the slice, then use TryFrom to convert it to the correct sized array but this includes an extra error type that couldn't occur if get_payload_as() had returned the correct size.

@jbaublitz
Copy link
Owner

Hi @isomer! I can likely include this in the final release of 0.7.0. I'll try to put up a PR with code that you can comment on and test soon.

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

No branches or pull requests

2 participants