scoop:scoop.asyncio:0.1.0
src/resources.scoop
Selected production source · src/resources.scoop
1
package scoop.asyncio3
/**4
* A resource whose cleanup may suspend until active native operations and borrows have finished.5
*/6
public 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
*/16
public 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 primary22
}23
close()24
return result25
}27
internal 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
}32
}