From 19a761a12cb15d16a2b8bb116ca0dbc533b52394 Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Mon, 21 Aug 2023 15:45:22 +0200 Subject: [PATCH 1/2] Use contracts for isPresent --- .../arbjerg/lavalink/protocol/v4/omissible.kt | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/omissible.kt b/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/omissible.kt index f3215cdcd..406392a3e 100644 --- a/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/omissible.kt +++ b/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/omissible.kt @@ -6,6 +6,8 @@ import kotlinx.serialization.SerializationException import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.contract import kotlin.jvm.JvmInline @Serializable(with = OmissableSerializer::class) @@ -70,9 +72,21 @@ class OmissableSerializer(private val childSerializer: KSerializer) : KSer } } -fun Omissible.isPresent() = this is Omissible.Present +@OptIn(ExperimentalContracts::class) +fun Omissible.isPresent(): Boolean { + contract { + returns(true) implies (this@isPresent is Omissible.Present) + } + return this is Omissible.Present +} -fun Omissible.isOmitted() = this is Omissible.Omitted +@OptIn(ExperimentalContracts::class) +fun Omissible.isOmitted(): Boolean { + contract { + returns(true) implies (this@isOmitted is Omissible.Present) + } + return this is Omissible.Omitted +} fun Omissible.takeIfPresent(predicate: (T) -> Boolean = { true }) = if (this is Omissible.Present) value.takeIf(predicate) else null From 26461e8305d11154c65896b02e923449fca607de Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Mon, 21 Aug 2023 15:48:21 +0200 Subject: [PATCH 2/2] Fix isOmitted --- .../kotlin/dev/arbjerg/lavalink/protocol/v4/omissible.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/omissible.kt b/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/omissible.kt index 406392a3e..a9fbb1aaf 100644 --- a/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/omissible.kt +++ b/protocol/src/commonMain/kotlin/dev/arbjerg/lavalink/protocol/v4/omissible.kt @@ -83,7 +83,7 @@ fun Omissible.isPresent(): Boolean { @OptIn(ExperimentalContracts::class) fun Omissible.isOmitted(): Boolean { contract { - returns(true) implies (this@isOmitted is Omissible.Present) + returns(true) implies (this@isOmitted is Omissible.Omitted) } return this is Omissible.Omitted }