From 060a80e9ab0e097cc5d4383c68f37ad920458f31 Mon Sep 17 00:00:00 2001 From: Vsevolod Tolstopyatov Date: Wed, 8 May 2024 17:01:06 +0200 Subject: [PATCH 1/3] Adopt AutoCloseable from standard library for CloseableCoroutineDispatcher * It's stable in 2.0 * For JVM, use Java's AutoCloseable as on Kotlin 1.9.x there is no JVM counterpart --- kotlinx-coroutines-core/api/kotlinx-coroutines-core.api | 2 +- kotlinx-coroutines-core/api/kotlinx-coroutines-core.klib.api | 2 +- .../common/src/CloseableCoroutineDispatcher.kt | 5 +++-- .../jsAndWasmShared/src/CloseableCoroutineDispatcher.kt | 5 +++-- kotlinx-coroutines-core/jvm/src/Executors.kt | 2 +- .../native/src/CloseableCoroutineDispatcher.kt | 5 +++-- 6 files changed, 12 insertions(+), 9 deletions(-) diff --git a/kotlinx-coroutines-core/api/kotlinx-coroutines-core.api b/kotlinx-coroutines-core/api/kotlinx-coroutines-core.api index 1e786a2116..af4ffb1558 100644 --- a/kotlinx-coroutines-core/api/kotlinx-coroutines-core.api +++ b/kotlinx-coroutines-core/api/kotlinx-coroutines-core.api @@ -346,7 +346,7 @@ public final class kotlinx/coroutines/ExceptionsKt { public static final fun CancellationException (Ljava/lang/String;Ljava/lang/Throwable;)Ljava/util/concurrent/CancellationException; } -public abstract class kotlinx/coroutines/ExecutorCoroutineDispatcher : kotlinx/coroutines/CoroutineDispatcher, java/io/Closeable { +public abstract class kotlinx/coroutines/ExecutorCoroutineDispatcher : kotlinx/coroutines/CoroutineDispatcher, java/lang/AutoCloseable { public static final field Key Lkotlinx/coroutines/ExecutorCoroutineDispatcher$Key; public fun ()V public abstract fun close ()V diff --git a/kotlinx-coroutines-core/api/kotlinx-coroutines-core.klib.api b/kotlinx-coroutines-core/api/kotlinx-coroutines-core.klib.api index f14ab24842..1c6ad1aa67 100644 --- a/kotlinx-coroutines-core/api/kotlinx-coroutines-core.klib.api +++ b/kotlinx-coroutines-core/api/kotlinx-coroutines-core.klib.api @@ -45,7 +45,7 @@ abstract class <#A: kotlin/Any?> kotlinx.coroutines.flow/AbstractFlow : kotlinx. constructor () // kotlinx.coroutines.flow/AbstractFlow.|(){}[0] final suspend fun collect(kotlinx.coroutines.flow/FlowCollector<#A>) // kotlinx.coroutines.flow/AbstractFlow.collect|collect(kotlinx.coroutines.flow.FlowCollector<1:0>){}[0] } -abstract class kotlinx.coroutines/CloseableCoroutineDispatcher : kotlinx.coroutines/CoroutineDispatcher { // kotlinx.coroutines/CloseableCoroutineDispatcher|null[0] +abstract class kotlinx.coroutines/CloseableCoroutineDispatcher : kotlin/AutoCloseable, kotlinx.coroutines/CoroutineDispatcher { // kotlinx.coroutines/CloseableCoroutineDispatcher|null[0] abstract fun close() // kotlinx.coroutines/CloseableCoroutineDispatcher.close|close(){}[0] constructor () // kotlinx.coroutines/CloseableCoroutineDispatcher.|(){}[0] } diff --git a/kotlinx-coroutines-core/common/src/CloseableCoroutineDispatcher.kt b/kotlinx-coroutines-core/common/src/CloseableCoroutineDispatcher.kt index 1ed0eb1c4f..3335333aef 100644 --- a/kotlinx-coroutines-core/common/src/CloseableCoroutineDispatcher.kt +++ b/kotlinx-coroutines-core/common/src/CloseableCoroutineDispatcher.kt @@ -10,8 +10,9 @@ package kotlinx.coroutines * **The `CloseableCoroutineDispatcher` class is not stable for inheritance in 3rd party libraries**, as new methods * might be added to this interface in the future, but is stable for use. */ +@OptIn(ExperimentalStdlibApi::class) @ExperimentalCoroutinesApi -public expect abstract class CloseableCoroutineDispatcher() : CoroutineDispatcher { +public expect abstract class CloseableCoroutineDispatcher() : CoroutineDispatcher, AutoCloseable { /** * Initiate the closing sequence of the coroutine dispatcher. @@ -20,5 +21,5 @@ public expect abstract class CloseableCoroutineDispatcher() : CoroutineDispatche * * Invocations of `close` are idempotent and thread-safe. */ - public abstract fun close() + public abstract override fun close() } diff --git a/kotlinx-coroutines-core/jsAndWasmShared/src/CloseableCoroutineDispatcher.kt b/kotlinx-coroutines-core/jsAndWasmShared/src/CloseableCoroutineDispatcher.kt index 81e0c5d8c3..c0a823a123 100644 --- a/kotlinx-coroutines-core/jsAndWasmShared/src/CloseableCoroutineDispatcher.kt +++ b/kotlinx-coroutines-core/jsAndWasmShared/src/CloseableCoroutineDispatcher.kt @@ -1,5 +1,6 @@ package kotlinx.coroutines -public actual abstract class CloseableCoroutineDispatcher actual constructor() : CoroutineDispatcher() { - public actual abstract fun close() +@OptIn(ExperimentalStdlibApi::class) +public actual abstract class CloseableCoroutineDispatcher actual constructor() : CoroutineDispatcher(), AutoCloseable { + public actual abstract override fun close() } diff --git a/kotlinx-coroutines-core/jvm/src/Executors.kt b/kotlinx-coroutines-core/jvm/src/Executors.kt index 53ef77dcc0..7e56e2181b 100644 --- a/kotlinx-coroutines-core/jvm/src/Executors.kt +++ b/kotlinx-coroutines-core/jvm/src/Executors.kt @@ -13,7 +13,7 @@ import kotlin.coroutines.* * This class is generally used as a bridge between coroutine-based API and * asynchronous API that requires an instance of the [Executor]. */ -public abstract class ExecutorCoroutineDispatcher: CoroutineDispatcher(), Closeable { +public abstract class ExecutorCoroutineDispatcher: CoroutineDispatcher(), AutoCloseable { /** @suppress */ @ExperimentalStdlibApi public companion object Key : AbstractCoroutineContextKey( diff --git a/kotlinx-coroutines-core/native/src/CloseableCoroutineDispatcher.kt b/kotlinx-coroutines-core/native/src/CloseableCoroutineDispatcher.kt index 81e0c5d8c3..c0a823a123 100644 --- a/kotlinx-coroutines-core/native/src/CloseableCoroutineDispatcher.kt +++ b/kotlinx-coroutines-core/native/src/CloseableCoroutineDispatcher.kt @@ -1,5 +1,6 @@ package kotlinx.coroutines -public actual abstract class CloseableCoroutineDispatcher actual constructor() : CoroutineDispatcher() { - public actual abstract fun close() +@OptIn(ExperimentalStdlibApi::class) +public actual abstract class CloseableCoroutineDispatcher actual constructor() : CoroutineDispatcher(), AutoCloseable { + public actual abstract override fun close() } From 41be683ef2abb2186255faee0d21b3c9b9027936 Mon Sep 17 00:00:00 2001 From: Vsevolod Tolstopyatov Date: Fri, 10 May 2024 15:49:22 +0200 Subject: [PATCH 2/3] ~fix compilation --- kotlinx-coroutines-core/api/kotlinx-coroutines-core.api | 2 +- kotlinx-coroutines-core/jvm/src/Executors.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kotlinx-coroutines-core/api/kotlinx-coroutines-core.api b/kotlinx-coroutines-core/api/kotlinx-coroutines-core.api index af4ffb1558..a21c195e26 100644 --- a/kotlinx-coroutines-core/api/kotlinx-coroutines-core.api +++ b/kotlinx-coroutines-core/api/kotlinx-coroutines-core.api @@ -346,7 +346,7 @@ public final class kotlinx/coroutines/ExceptionsKt { public static final fun CancellationException (Ljava/lang/String;Ljava/lang/Throwable;)Ljava/util/concurrent/CancellationException; } -public abstract class kotlinx/coroutines/ExecutorCoroutineDispatcher : kotlinx/coroutines/CoroutineDispatcher, java/lang/AutoCloseable { +public abstract class kotlinx/coroutines/ExecutorCoroutineDispatcher : kotlinx/coroutines/CoroutineDispatcher, java/io/Closeable, java/lang/AutoCloseable { public static final field Key Lkotlinx/coroutines/ExecutorCoroutineDispatcher$Key; public fun ()V public abstract fun close ()V diff --git a/kotlinx-coroutines-core/jvm/src/Executors.kt b/kotlinx-coroutines-core/jvm/src/Executors.kt index 7e56e2181b..3ab2f31bec 100644 --- a/kotlinx-coroutines-core/jvm/src/Executors.kt +++ b/kotlinx-coroutines-core/jvm/src/Executors.kt @@ -13,7 +13,7 @@ import kotlin.coroutines.* * This class is generally used as a bridge between coroutine-based API and * asynchronous API that requires an instance of the [Executor]. */ -public abstract class ExecutorCoroutineDispatcher: CoroutineDispatcher(), AutoCloseable { +public abstract class ExecutorCoroutineDispatcher: CoroutineDispatcher(), Closeable, AutoCloseable { /** @suppress */ @ExperimentalStdlibApi public companion object Key : AbstractCoroutineContextKey( From 3e5041528542ae8dfdc1a37502653b690b12b2a6 Mon Sep 17 00:00:00 2001 From: Vsevolod Tolstopyatov Date: Fri, 17 May 2024 12:01:08 +0200 Subject: [PATCH 3/3] Post Kotlin-2.0 update --- .../common/src/CloseableCoroutineDispatcher.kt | 1 - .../jsAndWasmShared/src/CloseableCoroutineDispatcher.kt | 1 - kotlinx-coroutines-core/jvm/src/Executors.kt | 5 +++-- .../native/src/CloseableCoroutineDispatcher.kt | 1 - 4 files changed, 3 insertions(+), 5 deletions(-) diff --git a/kotlinx-coroutines-core/common/src/CloseableCoroutineDispatcher.kt b/kotlinx-coroutines-core/common/src/CloseableCoroutineDispatcher.kt index 3335333aef..cc56121f02 100644 --- a/kotlinx-coroutines-core/common/src/CloseableCoroutineDispatcher.kt +++ b/kotlinx-coroutines-core/common/src/CloseableCoroutineDispatcher.kt @@ -10,7 +10,6 @@ package kotlinx.coroutines * **The `CloseableCoroutineDispatcher` class is not stable for inheritance in 3rd party libraries**, as new methods * might be added to this interface in the future, but is stable for use. */ -@OptIn(ExperimentalStdlibApi::class) @ExperimentalCoroutinesApi public expect abstract class CloseableCoroutineDispatcher() : CoroutineDispatcher, AutoCloseable { diff --git a/kotlinx-coroutines-core/jsAndWasmShared/src/CloseableCoroutineDispatcher.kt b/kotlinx-coroutines-core/jsAndWasmShared/src/CloseableCoroutineDispatcher.kt index c0a823a123..3ea73ad7a1 100644 --- a/kotlinx-coroutines-core/jsAndWasmShared/src/CloseableCoroutineDispatcher.kt +++ b/kotlinx-coroutines-core/jsAndWasmShared/src/CloseableCoroutineDispatcher.kt @@ -1,6 +1,5 @@ package kotlinx.coroutines -@OptIn(ExperimentalStdlibApi::class) public actual abstract class CloseableCoroutineDispatcher actual constructor() : CoroutineDispatcher(), AutoCloseable { public actual abstract override fun close() } diff --git a/kotlinx-coroutines-core/jvm/src/Executors.kt b/kotlinx-coroutines-core/jvm/src/Executors.kt index 3ab2f31bec..1d32489f19 100644 --- a/kotlinx-coroutines-core/jvm/src/Executors.kt +++ b/kotlinx-coroutines-core/jvm/src/Executors.kt @@ -2,9 +2,10 @@ package kotlinx.coroutines import kotlinx.coroutines.flow.* import kotlinx.coroutines.internal.* -import java.io.* +import java.io.Closeable import java.util.concurrent.* import kotlin.coroutines.* +import kotlin.AutoCloseable /** * [CoroutineDispatcher] that has underlying [Executor] for dispatching tasks. @@ -13,7 +14,7 @@ import kotlin.coroutines.* * This class is generally used as a bridge between coroutine-based API and * asynchronous API that requires an instance of the [Executor]. */ -public abstract class ExecutorCoroutineDispatcher: CoroutineDispatcher(), Closeable, AutoCloseable { +public abstract class ExecutorCoroutineDispatcher : CoroutineDispatcher(), Closeable, AutoCloseable { /** @suppress */ @ExperimentalStdlibApi public companion object Key : AbstractCoroutineContextKey( diff --git a/kotlinx-coroutines-core/native/src/CloseableCoroutineDispatcher.kt b/kotlinx-coroutines-core/native/src/CloseableCoroutineDispatcher.kt index c0a823a123..3ea73ad7a1 100644 --- a/kotlinx-coroutines-core/native/src/CloseableCoroutineDispatcher.kt +++ b/kotlinx-coroutines-core/native/src/CloseableCoroutineDispatcher.kt @@ -1,6 +1,5 @@ package kotlinx.coroutines -@OptIn(ExperimentalStdlibApi::class) public actual abstract class CloseableCoroutineDispatcher actual constructor() : CoroutineDispatcher(), AutoCloseable { public actual abstract override fun close() }