Skip to content

Commit

Permalink
Add use_enum_entries configuration option to enable the `EnumClass.…
Browse files Browse the repository at this point in the history
…entries` usage on `Kotlin >= 1.9.0` and keep backwards compatibility with previous versions
  • Loading branch information
jmartinesp committed Jul 12, 2024
1 parent c26330e commit bd83dbe
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
17 changes: 9 additions & 8 deletions docs/manual/src/kotlin/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ The generated Kotlin modules can be configured using a `uniffi.toml` configurati

## Available options

| Configuration name | Default | Description |
| ------------------ | ------- |------------ |
| `package_name` | `uniffi` | The Kotlin package name - ie, the value used in the `package` statement at the top of generated files. |
| `cdylib_name` | `uniffi_{namespace}`[^1] | The name of the compiled Rust library containing the FFI implementation (not needed when using `generate --library`). |
| Configuration name | Default | Description |
|------------------------------| ------- |------------ |
| `package_name` | `uniffi` | The Kotlin package name - ie, the value used in the `package` statement at the top of generated files. |
| `cdylib_name` | `uniffi_{namespace}`[^1] | The name of the compiled Rust library containing the FFI implementation (not needed when using `generate --library`). |
| `generate_immutable_records` | `false` | Whether to generate records with immutable fields (`val` instead of `var`). |
| `custom_types` | | A map which controls how custom types are exposed to Kotlin. See the [custom types section of the manual](../udl/custom_types.md#custom-types-in-the-bindings-code)|
| `external_packages` | | A map of packages to be used for the specified external crates. The key is the Rust crate name, the value is the Kotlin package which will be used referring to types in that crate. See the [external types section of the manual](../udl/ext_types_external.md#kotlin)
| `android` | `false` | Used to toggle on Android specific optimizations
| `android_cleaner` | `android` | Use the [`android.system.SystemCleaner`](https://developer.android.com/reference/android/system/SystemCleaner) instead of [`java.lang.ref.Cleaner`](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/ref/Cleaner.html). Fallback in both instances is the one shipped with JNA.
| `custom_types` | | A map which controls how custom types are exposed to Kotlin. See the [custom types section of the manual](../udl/custom_types.md#custom-types-in-the-bindings-code)|
| `external_packages` | | A map of packages to be used for the specified external crates. The key is the Rust crate name, the value is the Kotlin package which will be used referring to types in that crate. See the [external types section of the manual](../udl/ext_types_external.md#kotlin)
| `android` | `false` | Used to toggle on Android specific optimizations
| `android_cleaner` | `android` | Use the [`android.system.SystemCleaner`](https://developer.android.com/reference/android/system/SystemCleaner) instead of [`java.lang.ref.Cleaner`](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/ref/Cleaner.html). Fallback in both instances is the one shipped with JNA.
| `use_enum_entries` | `false` | Uses `EnumClass.entries` instead of `EnumClass.values()` in the bindings, which is more efficient as it reuses the same list instance every time. Note this is only available since Kotlin 1.9.0

## Example

Expand Down
6 changes: 6 additions & 0 deletions uniffi_bindgen/src/bindings/kotlin/gen_kotlin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,18 @@ pub struct Config {
android: bool,
#[serde(default)]
android_cleaner: Option<bool>,
#[serde(default)]
use_enum_entries: bool,
}

impl Config {
pub(crate) fn android_cleaner(&self) -> bool {
self.android_cleaner.unwrap_or(self.android)
}

pub(crate) fn use_enum_entries(&self) -> bool {
self.use_enum_entries
}
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
Expand Down
4 changes: 4 additions & 0 deletions uniffi_bindgen/src/bindings/kotlin/templates/EnumTemplate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ enum class {{ type_name }}(val value: {{ variant_discr_type|type_name(ci) }}) {

public object {{ e|ffi_converter_name }}: FfiConverterRustBuffer<{{ type_name }}> {
override fun read(buf: ByteBuffer) = try {
{% if config.use_enum_entries() %}
{{ type_name }}.entries[buf.getInt() - 1]
{% else -%}
{{ type_name }}.values()[buf.getInt() - 1]
{%- endif %}
} catch (e: IndexOutOfBoundsException) {
throw RuntimeException("invalid enum value, something is very wrong!!", e)
}
Expand Down

0 comments on commit bd83dbe

Please sign in to comment.