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 358289d..14e08c4 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 @@ -7,6 +7,23 @@ import kotlin.contracts.contract import kotlin.jvm.JvmName import ru.landgrafhomyak.utility.highlevel_try_finally.safeAutoClose1 + +@OptIn(ManualStateManipulation::class) +public inline fun autoClosed( + constructor: () -> S, + scope: (S) -> R, +): R { + contract { + callsInPlace(constructor, InvocationKind.EXACTLY_ONCE) + callsInPlace(scope, InvocationKind.EXACTLY_ONCE) + } + val state = constructor() + return safeAutoClose1( + action = { scope(state) }, + finally = state::close + ) +} + @OptIn(ManualStateManipulation::class) public inline fun CloseableState.childAC( constructor: () -> CloseableState.AllowsConcurrency = ::UsagesCounter, @@ -48,5 +65,8 @@ public inline fun CloseableState.ExternallySynchronized.closeableWrapper( callsInPlace(constructor, InvocationKind.EXACTLY_ONCE) callsInPlace(scope, InvocationKind.EXACTLY_ONCE) } - return CloseableStateCloseableWrapper(this, constructor()).use(scope) + return autoClosed( + constructor = { CloseableStateCloseableWrapper(this, constructor()) }, + scope = scope + ) } \ No newline at end of file