Debian Codegraph @

third_party/rust/uniffi_bindgen/src/bindings/kotlin/templates/EnumTemplate.kt

raw
1 {#
2 // Kotlin's `enum class` construct doesn't support variants with associated data,
3 // but is a little nicer for consumers than its `sealed class` enum pattern.
4 // So, we switch here, using `enum class` for enums with no associated data
5 // and `sealed class` for the general case.
6 #}
7
8 {%- if e.is_flat() %}
9
10 {%- call kt::docstring(e, 0) %}
11 {% match e.variant_discr_type() %}
12 {% when None %}
13 enum class {{ type_name }} {
14 {% for variant in e.variants() -%}
15 {%- call kt::docstring(variant, 4) %}
16 {{ variant|variant_name }}{% if loop.last %};{% else %},{% endif %}
17 {%- endfor %}
18 companion object
19 }
20 {% when Some(variant_discr_type) %}
21 enum class {{ type_name }}(val value: {{ variant_discr_type|type_name(ci) }}) {
22 {% for variant in e.variants() -%}
23 {%- call kt::docstring(variant, 4) %}
24 {{ variant|variant_name }}({{ e|variant_discr_literal(loop.index0) }}){% if loop.last %};{% else %},{% endif %}
25 {%- endfor %}
26 companion object
27 }
28 {% endmatch %}
29
30 /**
31 * @suppress
32 */
33 public object {{ e|ffi_converter_name }}: FfiConverterRustBuffer<{{ type_name }}> {
34 override fun read(buf: ByteBuffer) = try {
35 {% if config.use_enum_entries() %}
36 {{ type_name }}.entries[buf.getInt() - 1]
37 {% else -%}
38 {{ type_name }}.values()[buf.getInt() - 1]
39 {%- endif %}
40 } catch (e: IndexOutOfBoundsException) {
41 throw RuntimeException("invalid enum value, something is very wrong!!", e)
42 }
43
44 override fun allocationSize(value: {{ type_name }}) = 4UL
45
46 override fun write(value: {{ type_name }}, buf: ByteBuffer) {
47 buf.putInt(value.ordinal + 1)
48 }
49 }
50
51 {% else %}
52
53 {%- call kt::docstring(e, 0) %}
54 sealed class {{ type_name }}{% if contains_object_references %}: Disposable {% endif %} {
55 {% for variant in e.variants() -%}
56 {%- call kt::docstring(variant, 4) %}
57 {% if !variant.has_fields() -%}
58 object {{ variant|type_name(ci) }} : {{ type_name }}()
59 {% else -%}
60 data class {{ variant|type_name(ci) }}(
61 {%- for field in variant.fields() -%}
62 {%- call kt::docstring(field, 8) %}
63 val {% call kt::field_name(field, loop.index) %}: {{ field|type_name(ci) }}{% if loop.last %}{% else %}, {% endif %}
64 {%- endfor -%}
65 ) : {{ type_name }}() {
66 companion object
67 }
68 {%- endif %}
69 {% endfor %}
70
71 {% if contains_object_references %}
72 @Suppress("UNNECESSARY_SAFE_CALL") // codegen is much simpler if we unconditionally emit safe calls here
73 override fun destroy() {
74 when(this) {
75 {%- for variant in e.variants() %}
76 is {{ type_name }}.{{ variant|type_name(ci) }} -> {
77 {%- if variant.has_fields() %}
78 {% call kt::destroy_fields(variant) %}
79 {% else -%}
80 // Nothing to destroy
81 {%- endif %}
82 }
83 {%- endfor %}
84 }.let { /* this makes the `when` an expression, which ensures it is exhaustive */ }
85 }
86 {% endif %}
87 companion object
88 }
89
90 /**
91 * @suppress
92 */
93 public object {{ e|ffi_converter_name }} : FfiConverterRustBuffer<{{ type_name }}>{
94 override fun read(buf: ByteBuffer): {{ type_name }} {
95 return when(buf.getInt()) {
96 {%- for variant in e.variants() %}
97 {{ loop.index }} -> {{ type_name }}.{{ variant|type_name(ci) }}{% if variant.has_fields() %}(
98 {% for field in variant.fields() -%}
99 {{ field|read_fn }}(buf),
100 {% endfor -%}
101 ){%- endif -%}
102 {%- endfor %}
103 else -> throw RuntimeException("invalid enum value, something is very wrong!!")
104 }
105 }
106
107 override fun allocationSize(value: {{ type_name }}) = when(value) {
108 {%- for variant in e.variants() %}
109 is {{ type_name }}.{{ variant|type_name(ci) }} -> {
110 // Add the size for the Int that specifies the variant plus the size needed for all fields
111 (
112 4UL
113 {%- for field in variant.fields() %}
114 + {{ field|allocation_size_fn }}(value.{%- call kt::field_name(field, loop.index) -%})
115 {%- endfor %}
116 )
117 }
118 {%- endfor %}
119 }
120
121 override fun write(value: {{ type_name }}, buf: ByteBuffer) {
122 when(value) {
123 {%- for variant in e.variants() %}
124 is {{ type_name }}.{{ variant|type_name(ci) }} -> {
125 buf.putInt({{ loop.index }})
126 {%- for field in variant.fields() %}
127 {{ field|write_fn }}(value.{%- call kt::field_name(field, loop.index) -%}, buf)
128 {%- endfor %}
129 Unit
130 }
131 {%- endfor %}
132 }.let { /* this makes the `when` an expression, which ensures it is exhaustive */ }
133 }
134 }
135
136 {% endif %}