Scoop API

scoop:scoop.asyncio:0.1.0

src/resources.scoop

Selected production source · src/resources.scoop

1package scoop.asyncio
3/**
4 * A resource whose cleanup may suspend until active native operations and borrows have finished.
5 */
6public interface AsyncCloseable {
7 /**
8 * Closes the resource under its implementation's cleanup and repeat-call contract.
9 */
10 public suspend fun close() }
12/**
13 * Runs the block and always awaits resource close. If the body and cleanup both fail,
14 * ResourceUseException retains both errors.
15 */
16public suspend fun <R : AsyncCloseable, T> R.useAsync(block: suspend (R) -> T): T {
17 val result = try { block(this) }
18 catch (primary: Throwable) {
19 try { close() }
20 catch (cleanup: Throwable) { throw ResourceUseException(primary, cleanup) }
21 throw primary
22 }
23 close()
24 return result
27internal fun finishUnit(continuation: Continuation<Unit>, failure: Throwable?) {
28 when (failure) {
29 case Some(val error) -> continuation.resumeWithException(error)
30 case None -> continuation.resume(Unit)
31 }