-
Notifications
You must be signed in to change notification settings - Fork 0
add padded_enum macro #9
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
base: main
Are you sure you want to change the base?
Conversation
The intention behind this macro is to facilitate generating zerocopy-able enums, by generating a padded variant of the enum while having to write the minimum amount of code manually.
raffael0
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like this macro, however I'm missing the larger context.
Would you use this for the Message enum?
Or instead for the genericCommand enum?
Another question I have is how this would then be serialized, because right now the only tag we are sending over the wire is the command_id:
pub struct CanMessageData {
pub data_info: CanMessageDataInfo,
pub command_id: u8,
pub data: [u8; 62],
}Would the tag then be the command id, and the data the rest?
Thanks!
|
Yeah, so ideally I wanted to use it on both enums, but on the Message enum it is more tricky because you would have to extract the channel id from the bitfield and then write it to its own byte. After that you could deserialize the I now added it for the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really nice work. Looks great!
Feel free to merge when ready
| // --------------------------------------------------------- | ||
| // Unit Tests | ||
| // --------------------------------------------------------- | ||
| #[cfg(test)] | ||
| mod tests { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since your macro supports explicitly setting the tags. Can you add a test for that?
|
I added a github automation for automatic compilation/testing. Please rebase before merging |
The intention behind this macro is to facilitate generating zerocopy-able enums, by generating a padded variant of the enum while having to write the minimum amount of code manually.
Example:
This creates a new enum
MyProtoPadded, which adds an[u8; N]array to each variant corresponding to the value provided in#pad[N]. Sadly, automatically determining the appropriate padding to add is not possible in a macro. The(size = ...)annotation at the beginning adds an assertion enforcing that the generated enum has the expected size, making it more obvious if something doesn't add up.My hope is that this makes the transformation of messages from rust enums to bytes and vice versa a bit easier, because the standard
IntoBytes/TryFromBytesis implemented for thePaddedversion of the enum.