diff --git a/uniffi_bindgen/src/bindings/kotlin/templates/MapTemplate.kt b/uniffi_bindgen/src/bindings/kotlin/templates/MapTemplate.kt index c8cbecb68b..5fb635342d 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 }}>(len) { + 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 {