Skip to content

Inline Xof.use & Xof.Reader.use functions #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading