From 887d014c27b9b8a5860e80e529c25e86c6ecb565 Mon Sep 17 00:00:00 2001 From: Dmitry Borodin Date: Tue, 22 Aug 2023 14:12:06 +0200 Subject: [PATCH 1/3] implemented buildMap todo for kotlin --- .../src/bindings/kotlin/templates/MapTemplate.kt | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/uniffi_bindgen/src/bindings/kotlin/templates/MapTemplate.kt b/uniffi_bindgen/src/bindings/kotlin/templates/MapTemplate.kt index c8cbecb68b..2fe02fd85b 100644 --- a/uniffi_bindgen/src/bindings/kotlin/templates/MapTemplate.kt +++ b/uniffi_bindgen/src/bindings/kotlin/templates/MapTemplate.kt @@ -2,15 +2,14 @@ {%- let value_type_name = value_type|type_name %} public object {{ ffi_converter_name }}: FfiConverterRustBuffer> { override fun read(buf: ByteBuffer): Map<{{ key_type_name }}, {{ value_type_name }}> { - // TODO: Once Kotlin's `buildMap` API is stabilized we should use it here. - val items : MutableMap<{{ key_type_name }}, {{ value_type_name }}> = mutableMapOf() - val len = buf.getInt() - repeat(len) { - val k = {{ key_type|read_fn }}(buf) - val v = {{ value_type|read_fn }}(buf) - items[k] = v + return buildMap<{{ key_type_name }}, {{ value_type_name }}> { + val len = buf.getInt() + repeat(len) { + val k = { { key_type|read_fn } }(buf) + val v = { { value_type|read_fn } }(buf) + this[k] = v + } } - return items } override fun allocationSize(value: Map<{{ key_type_name }}, {{ value_type_name }}>): Int { From c7c3c576e22ad655e8a2a26ebfacd49352ac53b9 Mon Sep 17 00:00:00 2001 From: Dmitry Borodin Date: Tue, 22 Aug 2023 15:10:03 +0200 Subject: [PATCH 2/3] removed added space between } --- uniffi_bindgen/src/bindings/kotlin/templates/MapTemplate.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/uniffi_bindgen/src/bindings/kotlin/templates/MapTemplate.kt b/uniffi_bindgen/src/bindings/kotlin/templates/MapTemplate.kt index 2fe02fd85b..e8b48925de 100644 --- a/uniffi_bindgen/src/bindings/kotlin/templates/MapTemplate.kt +++ b/uniffi_bindgen/src/bindings/kotlin/templates/MapTemplate.kt @@ -5,8 +5,8 @@ public object {{ ffi_converter_name }}: FfiConverterRustBuffer { val len = buf.getInt() repeat(len) { - val k = { { key_type|read_fn } }(buf) - val v = { { value_type|read_fn } }(buf) + val k = {{ key_type|read_fn }}(buf) + val v = {{ value_type|read_fn }}(buf) this[k] = v } } From 331098fa7049a33feee9e90d23d3cd1f50344675 Mon Sep 17 00:00:00 2001 From: Dmitry Borodin Date: Sun, 27 Aug 2023 14:23:25 +0200 Subject: [PATCH 3/3] optimised buildMap - create map of required capaticy to avoid expansion --- uniffi_bindgen/src/bindings/kotlin/templates/MapTemplate.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/uniffi_bindgen/src/bindings/kotlin/templates/MapTemplate.kt b/uniffi_bindgen/src/bindings/kotlin/templates/MapTemplate.kt index e8b48925de..5fb635342d 100644 --- a/uniffi_bindgen/src/bindings/kotlin/templates/MapTemplate.kt +++ b/uniffi_bindgen/src/bindings/kotlin/templates/MapTemplate.kt @@ -2,8 +2,8 @@ {%- let value_type_name = value_type|type_name %} public object {{ ffi_converter_name }}: FfiConverterRustBuffer> { override fun read(buf: ByteBuffer): Map<{{ key_type_name }}, {{ value_type_name }}> { - return buildMap<{{ key_type_name }}, {{ value_type_name }}> { - val len = buf.getInt() + val len = buf.getInt() + return buildMap<{{ key_type_name }}, {{ value_type_name }}>(len) { repeat(len) { val k = {{ key_type|read_fn }}(buf) val v = {{ value_type|read_fn }}(buf)