Skip to content

Instantly share code, notes, and snippets.

@sandwwraith
Created June 27, 2024 14:25
Show Gist options
  • Save sandwwraith/1cc3bf4dc1b1658af34be85dcbb37976 to your computer and use it in GitHub Desktop.
Save sandwwraith/1cc3bf4dc1b1658af34be85dcbb37976 to your computer and use it in GitHub Desktop.
Subject: [PATCH] Improve EncodingUtils.kt
---
Index: formats/cbor/commonMain/src/kotlinx/serialization/cbor/internal/EncodingUtils.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/formats/cbor/commonMain/src/kotlinx/serialization/cbor/internal/EncodingUtils.kt b/formats/cbor/commonMain/src/kotlinx/serialization/cbor/internal/EncodingUtils.kt
--- a/formats/cbor/commonMain/src/kotlinx/serialization/cbor/internal/EncodingUtils.kt (revision d7397b263c44e452ae03b5a42ebb80677193660a)
+++ b/formats/cbor/commonMain/src/kotlinx/serialization/cbor/internal/EncodingUtils.kt (date 1719498283252)
@@ -46,7 +46,7 @@
@OptIn(ExperimentalSerializationApi::class)
internal fun SerialDescriptor.isByteString(index: Int): Boolean {
- return kotlin.runCatching { getElementAnnotations(index).find { it is ByteString } != null }.getOrDefault(false)
+ return getElementAnnotations(index).any { it is ByteString }
}
@@ -55,24 +55,29 @@
return isInline && isByteString(0)
}
+private inline fun <reified T : Annotation> List<Annotation>.firstIsInstance(): T? {
+ for (element in this) {
+ if (element is T) return element
+ }
+ return null
+}
+
@OptIn(ExperimentalSerializationApi::class)
internal fun SerialDescriptor.getValueTags(index: Int): ULongArray? {
- return kotlin.runCatching { (getElementAnnotations(index).find { it is ValueTags } as ValueTags?)?.tags }
- .getOrNull()
+ return getElementAnnotations(index).firstIsInstance<ValueTags>()?.tags
}
@OptIn(ExperimentalSerializationApi::class)
internal fun SerialDescriptor.getKeyTags(index: Int): ULongArray? {
- return kotlin.runCatching { (getElementAnnotations(index).find { it is KeyTags } as KeyTags?)?.tags }.getOrNull()
+ return getElementAnnotations(index).firstIsInstance<KeyTags>()?.tags
}
@OptIn(ExperimentalSerializationApi::class)
internal fun SerialDescriptor.getCborLabel(index: Int): Long? {
- return kotlin.runCatching { getElementAnnotations(index).filterIsInstance<CborLabel>().firstOrNull()?.label }
- .getOrNull()
+ return getElementAnnotations(index).firstIsInstance<CborLabel>()?.label
}
@OptIn(ExperimentalSerializationApi::class)
internal fun SerialDescriptor.hasArrayTag(): Boolean {
- return annotations.filterIsInstance<CborArray>().isNotEmpty()
+ return annotations.any { it is CborArray }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment