diff --git a/library/xof/api/xof.klib.api b/library/xof/api/xof.klib.api index 3ae2fbb..30b219c 100644 --- a/library/xof/api/xof.klib.api +++ b/library/xof/api/xof.klib.api @@ -37,9 +37,9 @@ sealed class <#A: org.kotlincrypto.core.xof/XofAlgorithm> org.kotlincrypto.core. final fun (): #A // org.kotlincrypto.core.xof/Xof.delegate.|(){}[0] abstract fun newReader(): org.kotlincrypto.core.xof/Xof.Reader<#A> // org.kotlincrypto.core.xof/Xof.newReader|newReader(){}[0] - final fun <#A1: kotlin/Any?> use(kotlin/Boolean =..., kotlin/Function1, #A1>): #A1 // org.kotlincrypto.core.xof/Xof.use|use(kotlin.Boolean;kotlin.Function1,0:0>){0§}[0] final fun reader(kotlin/Boolean =...): org.kotlincrypto.core.xof/Xof.Reader<#A> // org.kotlincrypto.core.xof/Xof.reader|reader(kotlin.Boolean){}[0] final fun toString(): kotlin/String // org.kotlincrypto.core.xof/Xof.toString|toString(){}[0] + final inline fun <#A1: kotlin/Any?> use(kotlin/Boolean =..., kotlin/Function1, #A1>): #A1 // org.kotlincrypto.core.xof/Xof.use|use(kotlin.Boolean;kotlin.Function1,0:0>){0§}[0] abstract inner class Reader { // org.kotlincrypto.core.xof/Xof.Reader|null[0] constructor () // org.kotlincrypto.core.xof/Xof.Reader.|(){}[0] @@ -52,11 +52,11 @@ sealed class <#A: org.kotlincrypto.core.xof/XofAlgorithm> org.kotlincrypto.core. abstract fun closeProtected() // org.kotlincrypto.core.xof/Xof.Reader.closeProtected|closeProtected(){}[0] abstract fun readProtected(kotlin/ByteArray, kotlin/Int, kotlin/Int): kotlin/Int // org.kotlincrypto.core.xof/Xof.Reader.readProtected|readProtected(kotlin.ByteArray;kotlin.Int;kotlin.Int){}[0] - final fun <#A2: kotlin/Any?> use(kotlin/Function1, #A2>): #A2 // org.kotlincrypto.core.xof/Xof.Reader.use|use(kotlin.Function1,0:0>){0§}[0] final fun close() // org.kotlincrypto.core.xof/Xof.Reader.close|close(){}[0] final fun read(kotlin/ByteArray): kotlin/Int // org.kotlincrypto.core.xof/Xof.Reader.read|read(kotlin.ByteArray){}[0] final fun read(kotlin/ByteArray, kotlin/Int, kotlin/Int): kotlin/Int // org.kotlincrypto.core.xof/Xof.Reader.read|read(kotlin.ByteArray;kotlin.Int;kotlin.Int){}[0] final fun toString(): kotlin/String // org.kotlincrypto.core.xof/Xof.Reader.toString|toString(){}[0] + final inline fun <#A2: kotlin/Any?> use(kotlin/Function1, #A2>): #A2 // org.kotlincrypto.core.xof/Xof.Reader.use|use(kotlin.Function1,0:0>){0§}[0] } final object Companion { // org.kotlincrypto.core.xof/Xof.Companion|null[0] diff --git a/library/xof/src/commonMain/kotlin/org/kotlincrypto/core/xof/Xof.kt b/library/xof/src/commonMain/kotlin/org/kotlincrypto/core/xof/Xof.kt index f060fa9..2409f02 100644 --- a/library/xof/src/commonMain/kotlin/org/kotlincrypto/core/xof/Xof.kt +++ b/library/xof/src/commonMain/kotlin/org/kotlincrypto/core/xof/Xof.kt @@ -18,6 +18,9 @@ package org.kotlincrypto.core.xof import org.kotlincrypto.core.* +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.InvocationKind +import kotlin.contracts.contract import kotlin.jvm.JvmField import kotlin.jvm.JvmName import kotlin.jvm.JvmOverloads @@ -86,7 +89,16 @@ public sealed class Xof( * initial state after taking the snapshot. * */ @JvmOverloads - public fun use(resetXof: Boolean = true, action: Reader.() -> T): T = reader(resetXof).use(action) + @OptIn(ExperimentalContracts::class) + public inline fun use( + resetXof: Boolean = true, + action: Reader.() -> T, + ): T { + contract { + callsInPlace(action, InvocationKind.EXACTLY_ONCE) + } + return reader(resetXof).use(action) + } /** * Takes a snapshot of the current [Xof]'s state and produces @@ -145,9 +157,13 @@ public sealed class Xof( * Helper function which automatically invokes [close] * once action completes. * */ - public fun use(action: Reader.() -> T): T { - return try { - action(this) + @OptIn(ExperimentalContracts::class) + public inline fun use(action: Reader.() -> T): T { + contract { + callsInPlace(action, InvocationKind.EXACTLY_ONCE) + } + try { + return action(this) } finally { close() }