Skip to content

Commit 30dd5c1

Browse files
committed
equals/hashCode for wrapper CoroutineDispatcher impls
1 parent 531a8c5 commit 30dd5c1

File tree

5 files changed

+15
-7
lines changed
  • kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental
  • reactive
    • kotlinx-coroutines-reactor/src/main/kotlin/kotlinx/coroutines/experimental/reactor
    • kotlinx-coroutines-rx2/src/main/kotlin/kotlinx/coroutines/experimental/rx2
  • ui/kotlinx-coroutines-android/src/main/kotlin/kotlinx/coroutines/experimental/android

5 files changed

+15
-7
lines changed

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/Builders.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ public suspend fun <T> run(context: CoroutineContext, block: suspend () -> T): T
8989
// fast path #2 if the result is actually the same
9090
if (newContext === oldContext)
9191
return@sc block.startCoroutineUninterceptedOrReturn(cont)
92-
// fast path #3 if the new dispatcher is the same as the old one
93-
if (newContext[ContinuationInterceptor] === oldContext[ContinuationInterceptor]) {
92+
// fast path #3 if the new dispatcher is the same as the old one.
93+
// `equals` is used by design (see equals implementation is wrapper context like ExecutorCoroutineDispatcher)
94+
if (newContext[ContinuationInterceptor] == oldContext[ContinuationInterceptor]) {
9495
val newContinuation = RunContinuationDirect(newContext, cont)
9596
return@sc block.startCoroutineUninterceptedOrReturn(newContinuation)
9697
}

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/Executors.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public fun Executor.toCoroutineDispatcher(): CoroutineDispatcher =
3737
public fun Executor.asCoroutineDispatcher(): CoroutineDispatcher =
3838
ExecutorCoroutineDispatcher(this)
3939

40-
internal open class ExecutorCoroutineDispatcher(
40+
private class ExecutorCoroutineDispatcher(
4141
private val executor: Executor
4242
) : CoroutineDispatcher(), Delay {
4343
override fun dispatch(context: CoroutineContext, block: Runnable) = executor.execute(block)
@@ -55,6 +55,10 @@ internal open class ExecutorCoroutineDispatcher(
5555
scheduledExecutor.schedule(block, time, unit)
5656
return DisposableFutureHandle(timeout)
5757
}
58+
59+
override fun toString(): String = executor.toString()
60+
override fun equals(other: Any?): Boolean = other is ExecutorCoroutineDispatcher && other.executor === executor
61+
override fun hashCode(): Int = System.identityHashCode(executor)
5862
}
5963

6064
// --- reusing these classes in other places ---

reactive/kotlinx-coroutines-reactor/src/main/kotlin/kotlinx/coroutines/experimental/reactor/Scheduler.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ open class SchedulerCoroutineDispatcher(private val scheduler: Scheduler) : Coro
3232
}
3333

3434
override fun toString(): String = scheduler.toString()
35+
override fun equals(other: Any?): Boolean = other is SchedulerCoroutineDispatcher && other.scheduler === scheduler
36+
override fun hashCode(): Int = System.identityHashCode(scheduler)
3537
}
3638

3739
/**

reactive/kotlinx-coroutines-rx2/src/main/kotlin/kotlinx/coroutines/experimental/rx2/RxScheduler.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,6 @@ public class SchedulerCoroutineDispatcher(private val scheduler: Scheduler) : Co
5656
}
5757

5858
override fun toString(): String = scheduler.toString()
59+
override fun equals(other: Any?): Boolean = other is SchedulerCoroutineDispatcher && other.scheduler === scheduler
60+
override fun hashCode(): Int = System.identityHashCode(scheduler)
5961
}

ui/kotlinx-coroutines-android/src/main/kotlin/kotlinx/coroutines/experimental/android/HandlerContext.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ package kotlinx.coroutines.experimental.android
1818

1919
import android.os.Handler
2020
import android.os.Looper
21-
import kotlinx.coroutines.experimental.CancellableContinuation
22-
import kotlinx.coroutines.experimental.CoroutineDispatcher
23-
import kotlinx.coroutines.experimental.Delay
24-
import kotlinx.coroutines.experimental.DisposableHandle
21+
import kotlinx.coroutines.experimental.*
2522
import java.util.concurrent.TimeUnit
2623
import kotlin.coroutines.experimental.CoroutineContext
2724

@@ -64,4 +61,6 @@ public class HandlerContext(
6461
}
6562

6663
override fun toString() = name ?: handler.toString()
64+
override fun equals(other: Any?): Boolean = other is HandlerContext && other.handler === handler
65+
override fun hashCode(): Int = System.identityHashCode(handler)
6766
}

0 commit comments

Comments
 (0)