SauceNaoAPI/src/commonMain/kotlin/dev/inmo/saucenaoapi/utils/SauceCloseable.kt

23 lines
436 B
Kotlin
Raw Normal View History

2020-12-02 08:39:54 +00:00
package dev.inmo.saucenaoapi.utils
2020-08-29 13:09:54 +00:00
import kotlinx.coroutines.supervisorScope
interface SauceCloseable {
fun close()
}
2022-05-14 07:43:28 +00:00
inline fun <T> SauceCloseable.use(block: (SauceCloseable) -> T): T = try {
2020-08-29 13:09:54 +00:00
block(this)
} finally {
close()
}
2022-05-14 07:43:28 +00:00
@Deprecated("Useless")
2020-08-29 13:09:54 +00:00
suspend fun <T> SauceCloseable.useSafe(block: suspend (SauceCloseable) -> T): T = try {
supervisorScope {
block(this@useSafe)
}
} finally {
close()
}