Extend from_enum macro to keep all enums at smallest repr #172
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.
The
from_enum
macro, before this PR, defined each macro to have variants ofisize
type. While this probably didn't really cause any issues in practice, this always made me a bit nervous. As far as I can tell, rust doesn't guarantee that enums withrepr(rust)
are automatically sized down to the smallest representation they could use (see the reference chapters on type layout and enums, maybe I missed something), so we might be passing all enums around asisize
s right now (as opposed to their most optimal representation, which is a u8 in most cases).So this PR, to fix that, allows specifying a
repr
for all enums created withfrom_enum
and ensures that every constant passed in to be the value of a variant in that enum will actually fit within the given repr.This pr also cleans up a few other small things, like using a fully-qualified path for
$crate::error::Error
and providing a way to convert from an enum into its c representation (e.g.c_int
) losslessly (to ensure that we don't accidentally lose information when we add a new enum without checking what its largest value actually is).