Skip to content

Commit

Permalink
Added SerialDescriptor object for NothingSerializer (Kotlin#2150)
Browse files Browse the repository at this point in the history
Improvement of Kotlin#932
  • Loading branch information
shanshin committed Jan 17, 2023
1 parent 1f4a9e5 commit 694e2f7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal object DurationSerializer : KSerializer<Duration> {

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

override fun serialize(encoder: Encoder, value: Nothing) {
throw SerializationException("'kotlin.Nothing' cannot be serialized")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

@file:OptIn(ExperimentalSerializationApi::class)

package kotlinx.serialization.internal

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.SerialKind
import kotlinx.serialization.descriptors.StructureKind

internal object NothingSerialDescriptor : SerialDescriptor {
public override val kind: SerialKind = StructureKind.OBJECT

public override val serialName: String = "kotlin.Nothing"

override val elementsCount: Int get() = 0
override fun getElementName(index: Int): String = error()
override fun getElementIndex(name: String): Int = error()
override fun isElementOptional(index: Int): Boolean = error()
override fun getElementDescriptor(index: Int): SerialDescriptor = error()
override fun getElementAnnotations(index: Int): List<Annotation> = error()
override fun toString(): String = "NothingSerialDescriptor"
override fun equals(other: Any?): Boolean {
return this === other
}

override fun hashCode(): Int = serialName.hashCode() + 31 * kind.hashCode()
private fun error(): Nothing =
throw IllegalStateException("Descriptor for type `kotlin.Nothing` does not have elements")
}

0 comments on commit 694e2f7

Please sign in to comment.