Skip to content

Commit

Permalink
Enum converter: write enums based on variant, instead of casting to uint
Browse files Browse the repository at this point in the history
Signed-off-by: Martynas Gurskas <[email protected]>
  • Loading branch information
Lipt0nas committed Feb 12, 2024
1 parent 24751f8 commit 9a1e9f6
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions bindgen/src/bindings/cpp/templates/enum_tmpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ RustBuffer {{ ffi_converter_name }}::lower(const {{ type_name }} &val) {
}

void {{ ffi_converter_name }}::write(RustStream &stream, const {{ type_name }} &val) {
stream << static_cast<int32_t>(val);
switch (val) {
{% for variant in e.variants() %}
case {{ type_name }}::{{ variant|variant_name }}:
stream << static_cast<int32_t>({{ loop.index }});
break;
{% endfor %}
default:
throw std::runtime_error("No matchig {{ type_name }} variant");
}
}

int32_t {{ ffi_converter_name }}::allocation_size(const {{ type_name|class_name }} &) {
Expand Down Expand Up @@ -122,4 +130,4 @@ int32_t {{ ffi_converter_name }}::allocation_size(const {{ type_name|class_name

return size;
}
{%- endif %}
{%- endif %}

0 comments on commit 9a1e9f6

Please sign in to comment.