-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP actually implement
[TaggedUnion]
.
- Loading branch information
Showing
17 changed files
with
539 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 60 additions & 7 deletions
67
uniffi_bindgen/src/bindings/kotlin/templates/EnumTemplate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,72 @@ | ||
{# | ||
// Kotlin's `enum class` constuct doesn't support variants with associated data, | ||
// but is a little nicer for consumers than its `sealed class` enum pattern. | ||
// So, we switch here, using `enum class` for enums with no associated data | ||
// and `sealed class` for the general case. | ||
#} | ||
|
||
{% if e.has_associated_data() %} | ||
|
||
sealed class {{ e.name()|class_name_kt }} { | ||
{% for variant in e.variants() %} | ||
class {{ variant.name()|class_name_kt }}() : {{ e.name()|class_name_kt }} { | ||
override fun write(buf: RustBufferBuilder) { | ||
buf.putInt({{ loop.index }}) | ||
// TODO: serialize fields here, if any | ||
} | ||
} | ||
{% endfor %} | ||
|
||
companion object { | ||
internal fun lift(rbuf: RustBuffer.ByValue): {{ e.name()|class_name_kt }} { | ||
return liftFromRustBuffer(rbuf) { buf -> {{ e.name()|class_name_kt }}.read(buf) } | ||
} | ||
|
||
internal fun read(buf: ByteBuffer): {{ e.name()|class_name_kt }} { | ||
return when(buf.getInt()) { | ||
{%- for variant in e.variants() %} | ||
{{ loop.index }} -> {{ e.name()|class_name_kt }}.{{ variant.name()|class_name_kt }}(/* TODO: read fields here, if any */) | ||
{%- endfor %} | ||
else -> throw RuntimeException("invalid enum value, something is very wrong!!") | ||
} | ||
} | ||
} | ||
|
||
internal fun lower(): RustBuffer.ByValue { | ||
return lowerIntoRustBuffer(this, {v, buf -> v.write(buf)}) | ||
} | ||
|
||
internal fun write(buf: RustBufferBuilder) { | ||
throw RuntimeException("enum variant should have overridden `write` method, something is very wrong!!") | ||
} | ||
} | ||
|
||
{% else %} | ||
|
||
enum class {{ e.name()|class_name_kt }} { | ||
{% for variant in e.variants() %} | ||
{{ variant|enum_variant_kt }}{% if loop.last %};{% else %},{% endif %} | ||
{{ variant.name()|enum_variant_kt }}{% if loop.last %};{% else %},{% endif %} | ||
{% endfor %} | ||
|
||
companion object { | ||
internal fun lift(n: Int) = | ||
try { values()[n - 1] } | ||
internal fun lift(rbuf: RustBuffer.ByValue): {{ e.name()|class_name_kt }} { | ||
return liftFromRustBuffer(rbuf) { buf -> {{ e.name()|class_name_kt }}.read(buf) } | ||
} | ||
|
||
internal fun read(buf: ByteBuffer) = | ||
try { values()[buf.getInt() - 1] } | ||
catch (e: IndexOutOfBoundsException) { | ||
throw RuntimeException("invalid enum value, something is very wrong!!", e) | ||
} | ||
|
||
internal fun read(buf: ByteBuffer) = lift(buf.getInt()) | ||
} | ||
|
||
internal fun lower() = this.ordinal + 1 | ||
internal fun lower(): RustBuffer.ByValue { | ||
return lowerIntoRustBuffer(this, {v, buf -> v.write(buf)}) | ||
} | ||
|
||
internal fun write(buf: RustBufferBuilder) = buf.putInt(this.lower()) | ||
internal fun write(buf: RustBufferBuilder) { | ||
buf.putInt(this.ordinal + 1) | ||
} | ||
} | ||
|
||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
{% if e.has_associated_data() %} | ||
assert False, "Sorry, the python backend doesn't yet support enums with associated data" | ||
{% else %} | ||
class {{ e.name()|class_name_py }}(enum.Enum): | ||
{% for variant in e.variants() -%} | ||
{{ variant|enum_name_py }} = {{ loop.index }} | ||
{{ variant.name()|enum_name_py }} = {{ loop.index }} | ||
{% endfor %} | ||
{% endif %} |
22 changes: 9 additions & 13 deletions
22
uniffi_bindgen/src/bindings/swift/templates/EnumTemplate.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.