Skip to content

Commit 19f3032

Browse files
committed
Update to 1.3-RC:
* Replace SuccessOrFailure with Result * Replace buildSequence and buildIterator with sequence and iterator * Apply @BuilderInference on all builders (including extension methods to workaround inference bug)
1 parent dee0d7a commit 19f3032

File tree

41 files changed

+146
-43
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+146
-43
lines changed

binary-compatibility-validator/reference-public-api/kotlinx-coroutines-core.txt

+2
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,10 @@ public abstract class kotlinx/coroutines/CoroutineDispatcher : kotlin/coroutines
154154
public fun <init> ()V
155155
public abstract fun dispatch (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Runnable;)V
156156
public fun dispatchYield (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Runnable;)V
157+
public fun get (Lkotlin/coroutines/CoroutineContext$Key;)Lkotlin/coroutines/CoroutineContext$Element;
157158
public fun interceptContinuation (Lkotlin/coroutines/Continuation;)Lkotlin/coroutines/Continuation;
158159
public fun isDispatchNeeded (Lkotlin/coroutines/CoroutineContext;)Z
160+
public fun minusKey (Lkotlin/coroutines/CoroutineContext$Key;)Lkotlin/coroutines/CoroutineContext;
159161
public final fun plus (Lkotlinx/coroutines/CoroutineDispatcher;)Lkotlinx/coroutines/CoroutineDispatcher;
160162
public fun releaseInterceptedContinuation (Lkotlin/coroutines/Continuation;)V
161163
public fun toString ()Ljava/lang/String;

build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ allprojects {
5454

5555
repositories {
5656
jcenter()
57+
maven { url 'http://dl.bintray.com/kotlin/kotlin-dev' }
5758
maven { url "https://kotlin.bintray.com/kotlin-dev" }
5859
maven { url "https://kotlin.bintray.com/kotlinx" }
5960
}

common/kotlinx-coroutines-core-common/src/AbstractContinuation.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ internal abstract class AbstractContinuation<in T>(
139139
return getSuccessfulResult(state)
140140
}
141141

142-
override fun resumeWith(result: SuccessOrFailure<T>) =
142+
override fun resumeWith(result: Result<T>) =
143143
resumeImpl(result.toState(), resumeMode)
144144

145145
public fun invokeOnCancellation(handler: CompletionHandler) {

common/kotlinx-coroutines-core-common/src/AbstractCoroutine.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public abstract class AbstractCoroutine<in T>(
101101
/**
102102
* Completes execution of this with coroutine with the specified result.
103103
*/
104-
public final override fun resumeWith(result: SuccessOrFailure<T>) {
104+
public final override fun resumeWith(result: Result<T>) {
105105
makeCompletingOnce(result.toState(), defaultResumeMode)
106106
}
107107

common/kotlinx-coroutines-core-common/src/Builders.common.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
@file:JvmMultifileClass
66
@file:JvmName("BuildersKt")
7+
@file:UseExperimental(ExperimentalTypeInference::class)
78

89
package kotlinx.coroutines
910

1011
import kotlinx.coroutines.internal.*
1112
import kotlinx.coroutines.intrinsics.*
1213
import kotlin.coroutines.*
1314
import kotlin.coroutines.intrinsics.*
15+
import kotlin.experimental.*
1416

1517
// --------------- basic coroutine builders ---------------
1618

@@ -40,6 +42,7 @@ import kotlin.coroutines.intrinsics.*
4042
* @param onCompletion optional completion handler for the coroutine (see [Job.invokeOnCompletion]).
4143
* @param block the coroutine code which will be invoked in the context of the provided scope.
4244
**/
45+
@BuilderInference
4346
public fun CoroutineScope.launch(
4447
context: CoroutineContext = EmptyCoroutineContext,
4548
start: CoroutineStart = CoroutineStart.DEFAULT,
@@ -236,7 +239,7 @@ private class RunContinuationUnintercepted<in T>(
236239
override val context: CoroutineContext,
237240
private val continuation: Continuation<T>
238241
): Continuation<T> {
239-
override fun resumeWith(result: SuccessOrFailure<T>) {
242+
override fun resumeWith(result: Result<T>) {
240243
withCoroutineContext(continuation.context) {
241244
continuation.resumeWith(result)
242245
}

common/kotlinx-coroutines-core-common/src/CompletedExceptionally.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import kotlin.coroutines.*
1010
/**
1111
* @suppress **This is unstable API and it is subject to change.**
1212
*/
13-
public fun <T> SuccessOrFailure<T>.toState(): Any? =
13+
public fun <T> Result<T>.toState(): Any? =
1414
if (isSuccess) getOrThrow() else CompletedExceptionally(exceptionOrNull()!!) // todo: need to do it better
1515

1616
/**

common/kotlinx-coroutines-core-common/src/CoroutineContext.common.kt

+4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5+
@file:UseExperimental(ExperimentalTypeInference::class)
6+
57
package kotlinx.coroutines
68

79
import kotlin.coroutines.*
10+
import kotlin.experimental.*
811

12+
@BuilderInference
913
public expect fun CoroutineScope.newCoroutineContext(context: CoroutineContext): CoroutineContext
1014

1115
@Suppress("PropertyName")

common/kotlinx-coroutines-core-common/src/CoroutineScope.kt

+5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5+
@file:UseExperimental(ExperimentalTypeInference::class)
6+
57
package kotlinx.coroutines
68

79
import kotlinx.coroutines.internal.*
810
import kotlin.coroutines.*
11+
import kotlin.experimental.*
912

1013
/**
1114
* Defines a scope for new coroutines. Every coroutine builder
@@ -82,6 +85,7 @@ public interface CoroutineScope {
8285
*
8386
* This is a shorthand for `CoroutineScope(thisScope + context)`.
8487
*/
88+
@BuilderInference
8589
public operator fun CoroutineScope.plus(context: CoroutineContext): CoroutineScope =
8690
CoroutineScope(coroutineContext + context)
8791

@@ -101,6 +105,7 @@ public operator fun CoroutineScope.plus(context: CoroutineContext): CoroutineSco
101105
* [isActive][kotlinx.coroutines.isActive] and [Job.isActive].
102106
*/
103107
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
108+
@BuilderInference
104109
public val CoroutineScope.isActive: Boolean
105110
get() = coroutineContext[Job]?.isActive ?: true
106111

common/kotlinx-coroutines-core-common/src/Deferred.kt

+4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5+
@file:UseExperimental(ExperimentalTypeInference::class)
6+
57
package kotlinx.coroutines
68

79
import kotlinx.coroutines.intrinsics.*
810
import kotlinx.coroutines.selects.*
911
import kotlin.coroutines.*
12+
import kotlin.experimental.*
1013

1114
/**
1215
* Deferred value is a non-blocking cancellable future.
@@ -142,6 +145,7 @@ public interface Deferred<out T> : Job {
142145
* @param onCompletion optional completion handler for the coroutine (see [Job.invokeOnCompletion]).
143146
* @param block the coroutine code.
144147
*/
148+
@BuilderInference
145149
public fun <T> CoroutineScope.async(
146150
context: CoroutineContext = EmptyCoroutineContext,
147151
start: CoroutineStart = CoroutineStart.DEFAULT,

common/kotlinx-coroutines-core-common/src/Dispatched.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal class DispatchedContinuation<in T>(
2727
override val delegate: Continuation<T>
2828
get() = this
2929

30-
override fun resumeWith(result: SuccessOrFailure<T>) {
30+
override fun resumeWith(result: Result<T>) {
3131
val context = continuation.context
3232
if (dispatcher.isDispatchNeeded(context)) {
3333
_state = result.toState()
@@ -60,7 +60,7 @@ internal class DispatchedContinuation<in T>(
6060
}
6161

6262
@Suppress("NOTHING_TO_INLINE") // we need it inline to save us an entry on the stack
63-
inline fun resumeUndispatchedWith(result: SuccessOrFailure<T>) {
63+
inline fun resumeUndispatchedWith(result: Result<T>) {
6464
withCoroutineContext(context) {
6565
continuation.resumeWith(result)
6666
}

common/kotlinx-coroutines-core-common/src/JobSupport.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ internal open class JobSupport constructor(active: Boolean) : Job, SelectClause0
761761
}
762762
}
763763

764-
public final override val children: Sequence<Job> get() = buildSequence {
764+
public final override val children: Sequence<Job> get() = sequence {
765765
val state = this@JobSupport.state
766766
when (state) {
767767
is ChildJob -> yield(state.childJob)

common/kotlinx-coroutines-core-common/src/channels/Broadcast.kt

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5+
@file:UseExperimental(ExperimentalTypeInference::class)
6+
57
package kotlinx.coroutines.channels
68

79
import kotlinx.coroutines.*
810
import kotlinx.coroutines.channels.Channel.Factory.CONFLATED
911
import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED
1012
import kotlinx.coroutines.intrinsics.*
1113
import kotlin.coroutines.*
14+
import kotlin.experimental.*
1215

1316
/**
1417
* Broadcasts all elements of the channel.
@@ -43,7 +46,7 @@ public fun <E> broadcast(
4346
start: CoroutineStart = CoroutineStart.LAZY,
4447
parent: Job? = null,
4548
onCompletion: CompletionHandler? = null,
46-
block: suspend ProducerScope<E>.() -> Unit
49+
@BuilderInference block: suspend ProducerScope<E>.() -> Unit
4750
): BroadcastChannel<E> =
4851
GlobalScope.broadcast(context + (parent ?: EmptyCoroutineContext), capacity, start, onCompletion, block)
4952

@@ -83,12 +86,13 @@ public fun <E> broadcast(
8386
* @param onCompletion optional completion handler for the producer coroutine (see [Job.invokeOnCompletion]).
8487
* @param block the coroutine code.
8588
*/
89+
@BuilderInference
8690
public fun <E> CoroutineScope.broadcast(
8791
context: CoroutineContext = EmptyCoroutineContext,
8892
capacity: Int = 1,
8993
start: CoroutineStart = CoroutineStart.LAZY,
9094
onCompletion: CompletionHandler? = null,
91-
block: suspend ProducerScope<E>.() -> Unit
95+
@BuilderInference block: suspend ProducerScope<E>.() -> Unit
9296
): BroadcastChannel<E> {
9397
val newContext = newCoroutineContext(context)
9498
val channel = BroadcastChannel<E>(capacity)

common/kotlinx-coroutines-core-common/src/channels/Produce.kt

+10-6
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5+
@file:UseExperimental(ExperimentalTypeInference::class)
6+
57
package kotlinx.coroutines.channels
68

79
import kotlinx.coroutines.*
810
import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED
911
import kotlin.coroutines.*
12+
import kotlin.experimental.*
1013

1114
/**
1215
* Scope for [produce][CoroutineScope.produce] coroutine builder.
@@ -65,11 +68,12 @@ interface ProducerJob<out E> : ReceiveChannel<E>, Job {
6568
* @param onCompletion optional completion handler for the producer coroutine (see [Job.invokeOnCompletion]).
6669
* @param block the coroutine code.
6770
*/
71+
@BuilderInference
6872
public fun <E> CoroutineScope.produce(
6973
context: CoroutineContext = EmptyCoroutineContext,
7074
capacity: Int = 0,
7175
onCompletion: CompletionHandler? = null,
72-
block: suspend ProducerScope<E>.() -> Unit
76+
@BuilderInference block: suspend ProducerScope<E>.() -> Unit
7377
): ReceiveChannel<E> {
7478
val channel = Channel<E>(capacity)
7579
val newContext = newCoroutineContext(context)
@@ -93,7 +97,7 @@ public fun <E> produce(
9397
context: CoroutineContext = Dispatchers.Default,
9498
capacity: Int = 0,
9599
onCompletion: CompletionHandler? = null,
96-
block: suspend ProducerScope<E>.() -> Unit
100+
@BuilderInference block: suspend ProducerScope<E>.() -> Unit
97101
): ReceiveChannel<E> =
98102
GlobalScope.produce(context, capacity, onCompletion, block)
99103

@@ -112,7 +116,7 @@ public fun <E> produce(
112116
capacity: Int = 0,
113117
parent: Job? = null,
114118
onCompletion: CompletionHandler? = null,
115-
block: suspend ProducerScope<E>.() -> Unit
119+
@BuilderInference block: suspend ProducerScope<E>.() -> Unit
116120
): ReceiveChannel<E> =
117121
GlobalScope.produce(context + (parent ?: EmptyCoroutineContext), capacity, onCompletion, block)
118122

@@ -122,15 +126,15 @@ public fun <E> produce(
122126
context: CoroutineContext = Dispatchers.Default,
123127
capacity: Int = 0,
124128
parent: Job? = null,
125-
block: suspend ProducerScope<E>.() -> Unit
129+
@BuilderInference block: suspend ProducerScope<E>.() -> Unit
126130
): ReceiveChannel<E> = GlobalScope.produce(context + (parent ?: EmptyCoroutineContext), capacity, block = block)
127131

128132
/** @suppress **Deprecated**: Binary compatibility */
129133
@Deprecated(message = "Binary compatibility", level = DeprecationLevel.HIDDEN)
130134
public fun <E> produce(
131135
context: CoroutineContext = Dispatchers.Default,
132136
capacity: Int = 0,
133-
block: suspend ProducerScope<E>.() -> Unit
137+
@BuilderInference block: suspend ProducerScope<E>.() -> Unit
134138
): ProducerJob<E> =
135139
GlobalScope.produce(context, capacity, block = block) as ProducerJob<E>
136140

@@ -141,7 +145,7 @@ public fun <E> produce(
141145
public fun <E> buildChannel(
142146
context: CoroutineContext,
143147
capacity: Int = 0,
144-
block: suspend ProducerScope<E>.() -> Unit
148+
@BuilderInference block: suspend ProducerScope<E>.() -> Unit
145149
): ProducerJob<E> =
146150
GlobalScope.produce(context, capacity, block = block) as ProducerJob<E>
147151

common/kotlinx-coroutines-core-common/src/selects/Select.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ internal class SelectBuilderImpl<in R>(
235235
}
236236

237237
// Resumes in MODE_DIRECT
238-
override fun resumeWith(result: SuccessOrFailure<R>) {
238+
override fun resumeWith(result: Result<R>) {
239239
doResume({ result.toState() }) {
240240
uCont.resumeWith(result)
241241
}

common/kotlinx-coroutines-core-common/test/selects/SelectBuilderImplTest.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class SelectBuilderImplTest {
1414
var resumed = false
1515
val delegate = object : Continuation<String> {
1616
override val context: CoroutineContext get() = EmptyCoroutineContext
17-
override fun resumeWith(result: SuccessOrFailure<String>) {
17+
override fun resumeWith(result: Result<String>) {
1818
check(result.getOrNull() == "OK")
1919
resumed = true
2020
}
@@ -39,7 +39,7 @@ class SelectBuilderImplTest {
3939
var resumed = false
4040
val delegate = object : Continuation<String> {
4141
override val context: CoroutineContext get() = EmptyCoroutineContext
42-
override fun resumeWith(result: SuccessOrFailure<String>) {
42+
override fun resumeWith(result: Result<String>) {
4343
check(result.getOrNull() == "OK")
4444
resumed = true
4545
}
@@ -64,7 +64,7 @@ class SelectBuilderImplTest {
6464
var resumed = false
6565
val delegate = object : Continuation<String> {
6666
override val context: CoroutineContext get() = EmptyCoroutineContext
67-
override fun resumeWith(result: SuccessOrFailure<String>) {
67+
override fun resumeWith(result: Result<String>) {
6868
check(result.exceptionOrNull() is TestException)
6969
resumed = true
7070
}
@@ -94,7 +94,7 @@ class SelectBuilderImplTest {
9494
var resumed = false
9595
val delegate = object : Continuation<String> {
9696
override val context: CoroutineContext get() = EmptyCoroutineContext
97-
override fun resumeWith(result: SuccessOrFailure<String>) {
97+
override fun resumeWith(result: Result<String>) {
9898
check(result.exceptionOrNull() is TestException)
9999
resumed = true
100100
}

core/kotlinx-coroutines-core/src/CoroutineContext.kt

+4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5+
@file:UseExperimental(ExperimentalTypeInference::class)
6+
57
package kotlinx.coroutines
68

79
import kotlinx.coroutines.internal.*
810
import kotlinx.coroutines.scheduling.*
911
import java.util.concurrent.atomic.*
1012
import kotlin.coroutines.*
13+
import kotlin.experimental.*
1114

1215
/**
1316
* Name of the property that controls coroutine debugging. See [newCoroutineContext][CoroutineScope.newCoroutineContext].
@@ -102,6 +105,7 @@ public val IO: CoroutineDispatcher
102105
* Coroutine name can be explicitly assigned using [CoroutineName] context element.
103106
* The string "coroutine" is used as a default name.
104107
*/
108+
@BuilderInference
105109
public actual fun CoroutineScope.newCoroutineContext(context: CoroutineContext): CoroutineContext {
106110
val combined = coroutineContext + context
107111
val debug = if (DEBUG) combined + CoroutineId(COROUTINE_ID.incrementAndGet()) else combined

core/kotlinx-coroutines-core/src/channels/Actor.kt

+4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5+
@file:UseExperimental(ExperimentalTypeInference::class)
6+
57
package kotlinx.coroutines.channels
68

79
import kotlinx.coroutines.*
810
import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED
911
import kotlinx.coroutines.intrinsics.*
1012
import kotlinx.coroutines.selects.*
1113
import kotlin.coroutines.*
14+
import kotlin.experimental.*
1215

1316
/**
1417
* Scope for [actor][GlobalScope.actor] coroutine builder.
@@ -111,6 +114,7 @@ interface ActorJob<in E> : SendChannel<E> {
111114
* @param onCompletion optional completion handler for the actor coroutine (see [Job.invokeOnCompletion]).
112115
* @param block the coroutine code.
113116
*/
117+
@BuilderInference
114118
public fun <E> CoroutineScope.actor(
115119
context: CoroutineContext = EmptyCoroutineContext,
116120
capacity: Int = 0,

core/kotlinx-coroutines-core/test/DelayJvmTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class DelayJvmTest : TestBase() {
6565
override val context: CoroutineContext
6666
get() = cont.context
6767

68-
override fun resumeWith(result: SuccessOrFailure<T>) {
68+
override fun resumeWith(result: Result<T>) {
6969
pool.execute { cont.resumeWith(result) }
7070
}
7171
}

core/kotlinx-coroutines-core/test/IODispatcherTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class IODispatcherTest : TestBase() {
1717
expect(2)
1818
assertNotSame(mainThread, Thread.currentThread())
1919
}
20+
2021
expect(3)
2122
assertSame(mainThread, Thread.currentThread())
2223
finish(4)

0 commit comments

Comments
 (0)