Skip to content

Add the missing type boundaries to the reactive integrations #2630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion reactive/kotlinx-coroutines-jdk9/src/Publish.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import org.reactivestreams.FlowAdapters
* to cancellation and error handling may change in the future.
*/
@ExperimentalCoroutinesApi // Since 1.3.x
public fun <T> flowPublish(
public fun <T : Any> flowPublish(
context: CoroutineContext = EmptyCoroutineContext,
@BuilderInference block: suspend ProducerScope<T>.() -> Unit
): Flow.Publisher<T> {
Expand Down
12 changes: 8 additions & 4 deletions reactive/kotlinx-coroutines-reactive/src/Convert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package kotlinx.coroutines.reactive

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import org.reactivestreams.*
import kotlin.coroutines.*
Expand All @@ -18,7 +19,10 @@ import kotlin.coroutines.*
@Deprecated(message = "Deprecated in the favour of consumeAsFlow()",
level = DeprecationLevel.WARNING, // Error in 1.4
replaceWith = ReplaceWith("this.consumeAsFlow().asPublisher()"))
public fun <T> ReceiveChannel<T>.asPublisher(context: CoroutineContext = EmptyCoroutineContext): Publisher<T> = publish(context) {
for (t in this@asPublisher)
send(t)
}
public fun <T> ReceiveChannel<T>.asPublisher(context: CoroutineContext = EmptyCoroutineContext): Publisher<T> =
// we call the deprecated version here because the non-deprecated one requires <T: Any> now
@Suppress("DEPRECATION_ERROR")
GlobalScope.publish(context) {
for (t in this@asPublisher)
send(t)
}
2 changes: 1 addition & 1 deletion reactive/kotlinx-coroutines-reactive/src/Publish.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import kotlin.internal.*
* to cancellation and error handling may change in the future.
*/
@ExperimentalCoroutinesApi
public fun <T> publish(
public fun <T : Any> publish(
context: CoroutineContext = EmptyCoroutineContext,
@BuilderInference block: suspend ProducerScope<T>.() -> Unit
): Publisher<T> {
Expand Down
13 changes: 8 additions & 5 deletions reactive/kotlinx-coroutines-reactor/src/Convert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public fun Job.asMono(context: CoroutineContext): Mono<Unit> = mono(context) { t
* @param context -- the coroutine context from which the resulting mono is going to be signalled
*/
@ExperimentalCoroutinesApi
public fun <T> Deferred<T?>.asMono(context: CoroutineContext): Mono<T> = mono(context) { [email protected]() }
public fun <T : Any> Deferred<T?>.asMono(context: CoroutineContext): Mono<T> = mono(context) { [email protected]() }

/**
* Converts a stream of elements received from the channel to the hot reactive flux.
Expand All @@ -48,7 +48,10 @@ public fun <T> Deferred<T?>.asMono(context: CoroutineContext): Mono<T> = mono(co
@Deprecated(message = "Deprecated in the favour of consumeAsFlow()",
level = DeprecationLevel.WARNING,
replaceWith = ReplaceWith("this.consumeAsFlow().asFlux()"))
public fun <T> ReceiveChannel<T>.asFlux(context: CoroutineContext = EmptyCoroutineContext): Flux<T> = flux(context) {
for (t in this@asFlux)
send(t)
}
public fun <T> ReceiveChannel<T>.asFlux(context: CoroutineContext = EmptyCoroutineContext): Flux<T> =
// use the deprecated version of `flux` here because the proper one has the <T : Any> boundary now
@Suppress("DEPRECATION_ERROR")
GlobalScope.flux(context) {
for (t in this@asFlux)
send(t)
}
2 changes: 1 addition & 1 deletion reactive/kotlinx-coroutines-reactor/src/Flux.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import kotlin.internal.*
* to cancellation and error handling may change in the future.
*/
@ExperimentalCoroutinesApi
public fun <T> flux(
public fun <T : Any> flux(
context: CoroutineContext = EmptyCoroutineContext,
@BuilderInference block: suspend ProducerScope<T>.() -> Unit
): Flux<T> {
Expand Down
2 changes: 1 addition & 1 deletion reactive/kotlinx-coroutines-reactor/src/Mono.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import kotlin.internal.*
*
* Method throws [IllegalArgumentException] if provided [context] contains a [Job] instance.
*/
public fun <T> mono(
public fun <T : Any> mono(
context: CoroutineContext = EmptyCoroutineContext,
block: suspend CoroutineScope.() -> T?
): Mono<T> {
Expand Down
2 changes: 1 addition & 1 deletion reactive/kotlinx-coroutines-reactor/src/ReactorFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import kotlin.coroutines.*
* is used, so calls are performed from an arbitrary thread.
*/
@JvmOverloads // binary compatibility
public fun <T: Any> Flow<T>.asFlux(context: CoroutineContext = EmptyCoroutineContext): Flux<T> =
public fun <T : Any> Flow<T>.asFlux(context: CoroutineContext = EmptyCoroutineContext): Flux<T> =
FlowAsFlux(this, Dispatchers.Unconfined + context)

private class FlowAsFlux<T : Any>(
Expand Down
4 changes: 2 additions & 2 deletions reactive/kotlinx-coroutines-reactor/test/FluxSingleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class FluxSingleTest : TestBase() {

@Test
fun testAwaitSingleOrNull() {
val flux = flux<String?> {
val flux = flux {
send(Flux.empty<String>().awaitSingleOrNull() ?: "OK")
}

Expand Down Expand Up @@ -169,7 +169,7 @@ class FluxSingleTest : TestBase() {

@Test
fun testAwaitFirstOrNull() {
val flux = flux<String?> {
val flux = flux {
send(Flux.empty<String>().awaitFirstOrNull() ?: "OK")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class ReactorContextTest : TestBase() {
}


private fun createFlux(): Flux<String?> = flux {
private fun createFlux(): Flux<String> = flux {
val ctx = reactorContext()
(1..3).forEach { send(ctx.getOrDefault(it, "noValue")) }
}
Expand Down
2 changes: 1 addition & 1 deletion reactive/kotlinx-coroutines-rx2/src/RxConvert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public fun Job.asCompletable(context: CoroutineContext): Completable = rxComplet
* @param context -- the coroutine context from which the resulting maybe is going to be signalled
*/
@ExperimentalCoroutinesApi
public fun <T> Deferred<T?>.asMaybe(context: CoroutineContext): Maybe<T> = rxMaybe(context) {
public fun <T : Any> Deferred<T?>.asMaybe(context: CoroutineContext): Maybe<T> = rxMaybe(context) {
[email protected]()
}

Expand Down
2 changes: 1 addition & 1 deletion reactive/kotlinx-coroutines-rx2/src/RxMaybe.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import kotlin.internal.*
* If the context does not have any dispatcher nor any other [ContinuationInterceptor], then [Dispatchers.Default] is used.
* Method throws [IllegalArgumentException] if provided [context] contains a [Job] instance.
*/
public fun <T> rxMaybe(
public fun <T : Any> rxMaybe(
context: CoroutineContext = EmptyCoroutineContext,
block: suspend CoroutineScope.() -> T?
): Maybe<T> {
Expand Down
2 changes: 1 addition & 1 deletion reactive/kotlinx-coroutines-rx3/src/RxConvert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public fun Job.asCompletable(context: CoroutineContext): Completable = rxComplet
* @param context -- the coroutine context from which the resulting maybe is going to be signalled
*/
@ExperimentalCoroutinesApi
public fun <T> Deferred<T?>.asMaybe(context: CoroutineContext): Maybe<T> = rxMaybe(context) {
public fun <T : Any> Deferred<T?>.asMaybe(context: CoroutineContext): Maybe<T> = rxMaybe(context) {
[email protected]()
}

Expand Down
2 changes: 1 addition & 1 deletion reactive/kotlinx-coroutines-rx3/src/RxMaybe.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import kotlin.coroutines.*
* If the context does not have any dispatcher nor any other [ContinuationInterceptor], then [Dispatchers.Default] is used.
* Method throws [IllegalArgumentException] if provided [context] contains a [Job] instance.
*/
public fun <T> rxMaybe(
public fun <T : Any> rxMaybe(
context: CoroutineContext = EmptyCoroutineContext,
block: suspend CoroutineScope.() -> T?
): Maybe<T> {
Expand Down