Skip to content

Commit 694e2f7

Browse files
authored
Added SerialDescriptor object for NothingSerializer (#2150)
Improvement of #932
1 parent 1f4a9e5 commit 694e2f7

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

core/commonMain/src/kotlinx/serialization/internal/BuiltInSerializers.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal object DurationSerializer : KSerializer<Duration> {
2727

2828
@PublishedApi
2929
internal object NothingSerializer : KSerializer<Nothing> {
30-
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("kotlin.Nothing", PrimitiveKind.INT)
30+
override val descriptor: SerialDescriptor = NothingSerialDescriptor
3131

3232
override fun serialize(encoder: Encoder, value: Nothing) {
3333
throw SerializationException("'kotlin.Nothing' cannot be serialized")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
@file:OptIn(ExperimentalSerializationApi::class)
6+
7+
package kotlinx.serialization.internal
8+
9+
import kotlinx.serialization.ExperimentalSerializationApi
10+
import kotlinx.serialization.descriptors.SerialDescriptor
11+
import kotlinx.serialization.descriptors.SerialKind
12+
import kotlinx.serialization.descriptors.StructureKind
13+
14+
internal object NothingSerialDescriptor : SerialDescriptor {
15+
public override val kind: SerialKind = StructureKind.OBJECT
16+
17+
public override val serialName: String = "kotlin.Nothing"
18+
19+
override val elementsCount: Int get() = 0
20+
override fun getElementName(index: Int): String = error()
21+
override fun getElementIndex(name: String): Int = error()
22+
override fun isElementOptional(index: Int): Boolean = error()
23+
override fun getElementDescriptor(index: Int): SerialDescriptor = error()
24+
override fun getElementAnnotations(index: Int): List<Annotation> = error()
25+
override fun toString(): String = "NothingSerialDescriptor"
26+
override fun equals(other: Any?): Boolean {
27+
return this === other
28+
}
29+
30+
override fun hashCode(): Int = serialName.hashCode() + 31 * kind.hashCode()
31+
private fun error(): Nothing =
32+
throw IllegalStateException("Descriptor for type `kotlin.Nothing` does not have elements")
33+
}

0 commit comments

Comments
 (0)