Replacement of AutoCloseable.use{}
This commit is contained in:
parent
826f286404
commit
999ff63ccf
@ -7,6 +7,23 @@ import kotlin.contracts.contract
|
|||||||
import kotlin.jvm.JvmName
|
import kotlin.jvm.JvmName
|
||||||
import ru.landgrafhomyak.utility.highlevel_try_finally.safeAutoClose1
|
import ru.landgrafhomyak.utility.highlevel_try_finally.safeAutoClose1
|
||||||
|
|
||||||
|
|
||||||
|
@OptIn(ManualStateManipulation::class)
|
||||||
|
public inline fun <S : CloseableState, R> 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)
|
@OptIn(ManualStateManipulation::class)
|
||||||
public inline fun <R> CloseableState.childAC(
|
public inline fun <R> CloseableState.childAC(
|
||||||
constructor: () -> CloseableState.AllowsConcurrency = ::UsagesCounter,
|
constructor: () -> CloseableState.AllowsConcurrency = ::UsagesCounter,
|
||||||
@ -48,5 +65,8 @@ public inline fun <R> CloseableState.ExternallySynchronized.closeableWrapper(
|
|||||||
callsInPlace(constructor, InvocationKind.EXACTLY_ONCE)
|
callsInPlace(constructor, InvocationKind.EXACTLY_ONCE)
|
||||||
callsInPlace(scope, InvocationKind.EXACTLY_ONCE)
|
callsInPlace(scope, InvocationKind.EXACTLY_ONCE)
|
||||||
}
|
}
|
||||||
return CloseableStateCloseableWrapper(this, constructor()).use(scope)
|
return autoClosed(
|
||||||
|
constructor = { CloseableStateCloseableWrapper(this, constructor()) },
|
||||||
|
scope = scope
|
||||||
|
)
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user