zserio
zserio copied to clipboard
Alignment of non-present optional member is ignored
Currently, alignment of non-present optional member is ignored. Meaning if a member with alignment is optional, alignment is optional as well:
struct Example
{
bool hasOptional;
align(32):
int32 myOptionalField if hasOptional == true;
int32 myField;
};
In the above code example, if the member hasOptional
is not present then no align(32)
will applied and the size will be 33 bits.
However, this behavior is unexpected for schema writers. It will be more intuitive if alignment becomes a property of the structure and it will be always applied without any exception.
If user wants to ignore alignment if optional member is not present, it will be still possible but only explicitly introducing new structure:
struct AlignedField
{
align(32):
int32 alignedField;
};
struct Example
{
bool hasOptional;
AlignedField myOptionalField if hasOptional == true;
int32 myField;
};
This will be clear from the first view what was intended and there will be no exceptions for alignment.
Please note, that this change can potentially break backward binary compatibility.
Consider to just create a warning if align is used in combination with an optional field and introduce a new keyword with the intended behavior, this avoids breaking binary compatibility. Some ideas for the keyword: alignment
, bitalign
, alignwith
What about introduction of two new keywords, one for applying alignment only to the next (optional) field and one to align within the struct/compound? How to apply align to auto array/optional, packed arrays?
Because we cannot break backward binary compatibility at the moment and because of effort, we postpone this issue.