Skip to content

Commit 2ee57c3

Browse files
authored
Inline function Key.Private.withKeyOrNull (#562)
1 parent 00e7c81 commit 2ee57c3

File tree

8 files changed

+26
-18
lines changed

8 files changed

+26
-18
lines changed

library/runtime-core/api/runtime-core.api

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4084,7 +4084,6 @@ public abstract class io/matthewnelson/kmp/tor/runtime/core/key/Key$Private : io
40844084
public final fun hashCode ()I
40854085
public final fun isDestroyed ()Z
40864086
public final fun toString ()Ljava/lang/String;
4087-
protected final fun withKeyOrNull (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
40884087
}
40894088

40904089
public abstract class io/matthewnelson/kmp/tor/runtime/core/key/Key$Public : io/matthewnelson/kmp/tor/runtime/core/key/Key, java/security/PublicKey {

library/runtime-core/api/runtime-core.klib.api

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3608,7 +3608,6 @@ sealed class io.matthewnelson.kmp.tor.runtime.core.key/Key { // io.matthewnelson
36083608
sealed class Private : io.matthewnelson.kmp.tor.runtime.core.key/Key, io.matthewnelson.kmp.tor.runtime.core/Destroyable { // io.matthewnelson.kmp.tor.runtime.core.key/Key.Private|null[0]
36093609
constructor <init>(kotlin/ByteArray) // io.matthewnelson.kmp.tor.runtime.core.key/Key.Private.<init>|<init>(kotlin.ByteArray){}[0]
36103610

3611-
final fun <#A2: kotlin/Any> withKeyOrNull(kotlin/Function1<kotlin/ByteArray, #A2>): #A2? // io.matthewnelson.kmp.tor.runtime.core.key/Key.Private.withKeyOrNull|withKeyOrNull(kotlin.Function1<kotlin.ByteArray,0:0>){0§<kotlin.Any>}[0]
36123611
final fun base16(): kotlin/String // io.matthewnelson.kmp.tor.runtime.core.key/Key.Private.base16|base16(){}[0]
36133612
final fun base16OrNull(): kotlin/String? // io.matthewnelson.kmp.tor.runtime.core.key/Key.Private.base16OrNull|base16OrNull(){}[0]
36143613
final fun base32(): kotlin/String // io.matthewnelson.kmp.tor.runtime.core.key/Key.Private.base32|base32(){}[0]

library/runtime-core/src/commonMain/kotlin/io/matthewnelson/kmp/tor/runtime/core/EnqueuedJob.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,9 @@ public abstract class EnqueuedJob protected constructor(
443443
}
444444

445445
internal companion object {
446+
446447
@JvmSynthetic
447-
internal fun get(): BuilderScope =
448-
BuilderScope()
448+
internal fun get(): BuilderScope = BuilderScope()
449449
}
450450
}
451451

library/runtime-core/src/commonMain/kotlin/io/matthewnelson/kmp/tor/runtime/core/key/ED25519_V3.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
package io.matthewnelson.kmp.tor.runtime.core.key
1919

20-
import io.matthewnelson.encoding.base16.Base16
21-
import io.matthewnelson.encoding.base64.Base64
2220
import io.matthewnelson.kmp.tor.runtime.core.net.OnionAddress
2321
import io.matthewnelson.kmp.tor.runtime.core.net.OnionAddress.V3.Companion.toOnionAddressV3OrNull
2422
import io.matthewnelson.kmp.tor.runtime.core.internal.tryDecodeOrNull

library/runtime-core/src/commonMain/kotlin/io/matthewnelson/kmp/tor/runtime/core/key/Key.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ public expect sealed class Key private constructor() {
7171
public final override fun base32OrNull(): String?
7272
public final override fun base64OrNull(): String?
7373

74-
protected fun <T: Any> withKeyOrNull(block: (key: ByteArray) -> T): T?
75-
7674
/** @suppress */
7775
public final override fun equals(other: Any?): Boolean
7876
/** @suppress */

library/runtime-core/src/commonMain/kotlin/io/matthewnelson/kmp/tor/runtime/core/net/LocalHost.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ public sealed class LocalHost private constructor(): Address("localhost") {
6666
internal override fun fromCache(): IPAddress.V6? = Cache.getOrNull()?.firstOrNull()
6767
}
6868

69-
private class Cache private constructor(private val addresses: Set<IPAddress>) {
69+
private class Cache private constructor(addresses: Set<IPAddress>) {
7070

71+
private val addresses: Set<IPAddress> = addresses.toImmutableSet()
7172
private val timeMark = TimeSource.Monotonic.markNow()
7273

7374
private fun isNotExpired(): Boolean {
@@ -114,8 +115,9 @@ public sealed class LocalHost private constructor(): Address("localhost") {
114115
addresses.add(IPAddress.V4.loopback())
115116
addresses.add(IPAddress.V6.loopback())
116117

117-
_cache = Cache(addresses.toImmutableSet())
118-
return addresses
118+
val c = Cache(addresses)
119+
_cache = c
120+
return c.addresses
119121
}
120122

121123
@JvmStatic
@@ -135,9 +137,7 @@ public sealed class LocalHost private constructor(): Address("localhost") {
135137

136138
public companion object {
137139

138-
/**
139-
* @suppress
140-
* */
140+
/** @suppress */
141141
@InternalKmpTorApi
142142
@Throws(IOException::class)
143143
public fun refreshCache() { Cache.resolve(checkCache = false) }

library/runtime-core/src/jvmMain/kotlin/io/matthewnelson/kmp/tor/runtime/core/key/Key.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import io.matthewnelson.kmp.tor.common.core.synchronized
2828
import io.matthewnelson.kmp.tor.runtime.core.Destroyable
2929
import io.matthewnelson.kmp.tor.runtime.core.Destroyable.Companion.destroyedException
3030
import kotlin.concurrent.Volatile
31+
import kotlin.contracts.ExperimentalContracts
32+
import kotlin.contracts.InvocationKind
33+
import kotlin.contracts.contract
3134

3235
/**
3336
* Base abstraction for Public/Private keys used in tor.
@@ -104,10 +107,14 @@ public actual sealed class Key private actual constructor(): java.security.Key {
104107
public actual final override fun base32OrNull(): String? = withKeyOrNull { it.encodeToString(BASE_32) }
105108
public actual final override fun base64OrNull(): String? = withKeyOrNull { it.encodeToString(BASE_64) }
106109

107-
@OptIn(InternalKmpTorApi::class)
108-
protected actual fun <T : Any> withKeyOrNull(
110+
@OptIn(ExperimentalContracts::class, InternalKmpTorApi::class)
111+
private inline fun <T : Any> withKeyOrNull(
109112
block: (key: ByteArray) -> T
110113
): T? {
114+
contract {
115+
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
116+
}
117+
111118
if (_destroyed) return null
112119

113120
return synchronized(lock) {

library/runtime-core/src/nonJvmMain/kotlin/io/matthewnelson/kmp/tor/runtime/core/key/Key.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import io.matthewnelson.kmp.tor.common.core.synchronized
2828
import io.matthewnelson.kmp.tor.runtime.core.Destroyable
2929
import io.matthewnelson.kmp.tor.runtime.core.Destroyable.Companion.destroyedException
3030
import kotlin.concurrent.Volatile
31+
import kotlin.contracts.ExperimentalContracts
32+
import kotlin.contracts.InvocationKind
33+
import kotlin.contracts.contract
3134

3235
/**
3336
* Base abstraction for Public/Private keys used in tor.
@@ -97,10 +100,14 @@ public actual sealed class Key private actual constructor() {
97100
public actual final override fun base32OrNull(): String? = withKeyOrNull { it.encodeToString(BASE_32) }
98101
public actual final override fun base64OrNull(): String? = withKeyOrNull { it.encodeToString(BASE_64) }
99102

100-
@OptIn(InternalKmpTorApi::class)
101-
protected actual fun <T : Any> withKeyOrNull(
103+
@OptIn(ExperimentalContracts::class, InternalKmpTorApi::class)
104+
private inline fun <T : Any> withKeyOrNull(
102105
block: (key: ByteArray) -> T
103106
): T? {
107+
contract {
108+
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
109+
}
110+
104111
if (_destroyed) return null
105112

106113
return synchronized(lock) {

0 commit comments

Comments
 (0)