From 8f277853bd00907226f70a32c85ebc45b3082c96 Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Sat, 23 Aug 2025 23:42:34 +0300 Subject: [PATCH] Fixed typo in ES impl and CloseableWrapper --- .../CloseableStateCloseableWrapper.kt | 38 +++++++++++++++++++ ...t => OwnedErrorOnConcurrentAccessState.kt} | 2 +- .../closeable_state_1/children_scopes.kt | 12 ++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/commonMain/kotlin/ru/landgrafhomyak/utility/closeable_state_1/CloseableStateCloseableWrapper.kt rename src/commonMain/kotlin/ru/landgrafhomyak/utility/closeable_state_1/{OwnedErrorOnConcurrentAccesState.kt => OwnedErrorOnConcurrentAccessState.kt} (90%) diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/utility/closeable_state_1/CloseableStateCloseableWrapper.kt b/src/commonMain/kotlin/ru/landgrafhomyak/utility/closeable_state_1/CloseableStateCloseableWrapper.kt new file mode 100644 index 0000000..adb67f9 --- /dev/null +++ b/src/commonMain/kotlin/ru/landgrafhomyak/utility/closeable_state_1/CloseableStateCloseableWrapper.kt @@ -0,0 +1,38 @@ +package ru.landgrafhomyak.utility.closeable_state_1 + +public class CloseableStateCloseableWrapper( + private val _parent: CloseableState.ExternallySynchronized, + private val _self: CloseableState.ExternallySynchronized +) : CloseableState.ExternallySynchronized { + + override val isInUse: Boolean by this._self::isInUse + override val isClosed: Boolean by this._self::isInUse + + override fun throwClosed(): Nothing = + this._self.throwClosed() + + override fun throwInUse(): Nothing = + this._self.throwInUse() + + override fun assertNotClosed(): Unit = + this._self.assertNotClosed() + + @ManualStateManipulation + override fun startUsage() { + this._self.tryStartUsage { + this._parent.startUsage() + } + } + + @ManualStateManipulation + override fun finishUsage() { + this._self.tryFinishUsage { + this._parent.finishUsage() + } + } + + @ManualStateManipulation + override fun close() { + this._self.close() + } +} \ No newline at end of file diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/utility/closeable_state_1/OwnedErrorOnConcurrentAccesState.kt b/src/commonMain/kotlin/ru/landgrafhomyak/utility/closeable_state_1/OwnedErrorOnConcurrentAccessState.kt similarity index 90% rename from src/commonMain/kotlin/ru/landgrafhomyak/utility/closeable_state_1/OwnedErrorOnConcurrentAccesState.kt rename to src/commonMain/kotlin/ru/landgrafhomyak/utility/closeable_state_1/OwnedErrorOnConcurrentAccessState.kt index 1f8639d..04cf3c1 100644 --- a/src/commonMain/kotlin/ru/landgrafhomyak/utility/closeable_state_1/OwnedErrorOnConcurrentAccesState.kt +++ b/src/commonMain/kotlin/ru/landgrafhomyak/utility/closeable_state_1/OwnedErrorOnConcurrentAccessState.kt @@ -4,7 +4,7 @@ package ru.landgrafhomyak.utility.closeable_state_1 import kotlin.contracts.ExperimentalContracts -public class OwnedErrorOnConcurrentAccesState : ErrorOnConcurrentAccessState { +public class OwnedErrorOnConcurrentAccessState : ErrorOnConcurrentAccessState { public val _owner: Any public constructor(owner: Any) : super() { diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/utility/closeable_state_1/children_scopes.kt b/src/commonMain/kotlin/ru/landgrafhomyak/utility/closeable_state_1/children_scopes.kt index 20aefe4..358289d 100644 --- a/src/commonMain/kotlin/ru/landgrafhomyak/utility/closeable_state_1/children_scopes.kt +++ b/src/commonMain/kotlin/ru/landgrafhomyak/utility/closeable_state_1/children_scopes.kt @@ -37,4 +37,16 @@ public inline fun CloseableState.childES( action = { scope(childState) }, finally = childState::close ) +} + +@OptIn(ManualStateManipulation::class) +public inline fun CloseableState.ExternallySynchronized.closeableWrapper( + constructor: () -> CloseableState.ExternallySynchronized = ::ErrorOnConcurrentAccessState, + scope: (CloseableState.ExternallySynchronized) -> R, +): R { + contract { + callsInPlace(constructor, InvocationKind.EXACTLY_ONCE) + callsInPlace(scope, InvocationKind.EXACTLY_ONCE) + } + return CloseableStateCloseableWrapper(this, constructor()).use(scope) } \ No newline at end of file