Skip to content

Commit

Permalink
Inline Xof.use & Xof.Reader.use functions
Browse files Browse the repository at this point in the history
  • Loading branch information
05nelsonm committed Jan 13, 2025
1 parent 5cbbaa8 commit 59ff20d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions library/xof/api/xof.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ sealed class <#A: org.kotlincrypto.core.xof/XofAlgorithm> org.kotlincrypto.core.
final fun <get-delegate>(): #A // org.kotlincrypto.core.xof/Xof.delegate.<get-delegate>|<get-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<org.kotlincrypto.core.xof/Xof.Reader<#A>, #A1>): #A1 // org.kotlincrypto.core.xof/Xof.use|use(kotlin.Boolean;kotlin.Function1<org.kotlincrypto.core.xof.Xof.Reader<1:0>,0:0>){0§<kotlin.Any?>}[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<org.kotlincrypto.core.xof/Xof.Reader<#A>, #A1>): #A1 // org.kotlincrypto.core.xof/Xof.use|use(kotlin.Boolean;kotlin.Function1<org.kotlincrypto.core.xof.Xof.Reader<1:0>,0:0>){0§<kotlin.Any?>}[0]

abstract inner class Reader { // org.kotlincrypto.core.xof/Xof.Reader|null[0]
constructor <init>() // org.kotlincrypto.core.xof/Xof.Reader.<init>|<init>(){}[0]
Expand All @@ -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<org.kotlincrypto.core.xof/Xof.Reader<#A>, #A2>): #A2 // org.kotlincrypto.core.xof/Xof.Reader.use|use(kotlin.Function1<org.kotlincrypto.core.xof.Xof.Reader<2:0>,0:0>){0§<kotlin.Any?>}[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<org.kotlincrypto.core.xof/Xof.Reader<#A>, #A2>): #A2 // org.kotlincrypto.core.xof/Xof.Reader.use|use(kotlin.Function1<org.kotlincrypto.core.xof.Xof.Reader<2:0>,0:0>){0§<kotlin.Any?>}[0]
}

final object Companion { // org.kotlincrypto.core.xof/Xof.Companion|null[0]
Expand Down
24 changes: 20 additions & 4 deletions library/xof/src/commonMain/kotlin/org/kotlincrypto/core/xof/Xof.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -86,7 +89,16 @@ public sealed class Xof<A: XofAlgorithm>(
* initial state after taking the snapshot.
* */
@JvmOverloads
public fun <T: Any?> use(resetXof: Boolean = true, action: Reader.() -> T): T = reader(resetXof).use(action)
@OptIn(ExperimentalContracts::class)
public inline fun <T: Any?> 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
Expand Down Expand Up @@ -145,9 +157,13 @@ public sealed class Xof<A: XofAlgorithm>(
* Helper function which automatically invokes [close]
* once action completes.
* */
public fun <T: Any?> use(action: Reader.() -> T): T {
return try {
action(this)
@OptIn(ExperimentalContracts::class)
public inline fun <T: Any?> use(action: Reader.() -> T): T {
contract {
callsInPlace(action, InvocationKind.EXACTLY_ONCE)
}
try {
return action(this)
} finally {
close()
}
Expand Down

0 comments on commit 59ff20d

Please sign in to comment.