Adds header and footer attributes and traits #106
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello. I do quite like this library but had a need for some more features, so I wrote a PR for them.
This PR adds attributes and traits to add static or dynamic headers and footers to a packed struct.
For static values a header and/or footer can be specified with the
header
andfooter
attributes. They will be added on pack and will be ignored on unpack. No validation is happening on unpack for either field.This fixes #91
If a more dynamic value is needed for either header or footer I've added two traits,
PackedStructHeader
andPackedStructFooter
. They each provide theget_(header/footer)
andvalidate_(header/footer)
methods, which receive the already packed structure and can be used to generate values and to validate them. Currently the size of the header or footer must be specified with an attribute ((header/footer)_size = X
) because I could not find a way to get the size of the return value in the derive macro. The validation method gets called before the rest of the struct gets unpacked. I added aPackingError::UserError
to allow users to easily return possible validation errorsI've also added tests/examples for the above. I'm currently also looking into allowing enums with structs inside to work with packed_struct as well by using
PackedStructHeader
but if I do this it'll be part of a separate PR