@@ -41,12 +41,11 @@ public suspend fun CompletableSource.await(): Unit = suspendCancellableCoroutine
41
41
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this
42
42
* function immediately resumes with [CancellationException] and disposes of its subscription.
43
43
*/
44
- @Suppress(" UNCHECKED_CAST" )
45
- public suspend fun <T > MaybeSource<T>.awaitSingleOrNull (): T ? = suspendCancellableCoroutine { cont ->
46
- subscribe(object : MaybeObserver <T > {
44
+ public suspend fun <T > MaybeSource <T & Any >.awaitSingleOrNull (): T ? = suspendCancellableCoroutine { cont ->
45
+ subscribe(object : MaybeObserver <T & Any > {
47
46
override fun onSubscribe (d : Disposable ) { cont.disposeOnCancellation(d) }
48
47
override fun onComplete () { cont.resume(null ) }
49
- override fun onSuccess (t : T ) { cont.resume(t) }
48
+ override fun onSuccess (t : T & Any ) { cont.resume(t) }
50
49
override fun onError (error : Throwable ) { cont.resumeWithException(error) }
51
50
})
52
51
}
@@ -61,7 +60,7 @@ public suspend fun <T> MaybeSource<T>.awaitSingleOrNull(): T? = suspendCancellab
61
60
*
62
61
* @throws NoSuchElementException if no elements were produced by this [MaybeSource].
63
62
*/
64
- public suspend fun <T > MaybeSource<T>.awaitSingle (): T = awaitSingleOrNull() ? : throw NoSuchElementException ()
63
+ public suspend fun <T > MaybeSource <T & Any >.awaitSingle (): T = awaitSingleOrNull() ? : throw NoSuchElementException ()
65
64
66
65
/* *
67
66
* Awaits for completion of the maybe without blocking a thread.
@@ -84,7 +83,7 @@ public suspend fun <T> MaybeSource<T>.awaitSingle(): T = awaitSingleOrNull() ?:
84
83
level = DeprecationLevel .ERROR ,
85
84
replaceWith = ReplaceWith (" this.awaitSingleOrNull()" )
86
85
) // Warning since 1.5, error in 1.6, hidden in 1.7
87
- public suspend fun <T > MaybeSource<T>.await (): T ? = awaitSingleOrNull()
86
+ public suspend fun <T > MaybeSource <T & Any >.await (): T ? = awaitSingleOrNull()
88
87
89
88
/* *
90
89
* Awaits for completion of the maybe without blocking a thread.
@@ -107,7 +106,7 @@ public suspend fun <T> MaybeSource<T>.await(): T? = awaitSingleOrNull()
107
106
level = DeprecationLevel .ERROR ,
108
107
replaceWith = ReplaceWith (" this.awaitSingleOrNull() ?: default" )
109
108
) // Warning since 1.5, error in 1.6, hidden in 1.7
110
- public suspend fun <T > MaybeSource<T>.awaitOrDefault (default : T ): T = awaitSingleOrNull() ? : default
109
+ public suspend fun <T > MaybeSource <T & Any >.awaitOrDefault (default : T ): T = awaitSingleOrNull() ? : default
111
110
112
111
// ------------------------ SingleSource ------------------------
113
112
@@ -119,10 +118,10 @@ public suspend fun <T> MaybeSource<T>.awaitOrDefault(default: T): T = awaitSingl
119
118
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
120
119
* function immediately disposes of its subscription and resumes with [CancellationException].
121
120
*/
122
- public suspend fun <T > SingleSource<T>.await (): T = suspendCancellableCoroutine { cont ->
123
- subscribe(object : SingleObserver <T > {
121
+ public suspend fun <T > SingleSource <T & Any >.await (): T = suspendCancellableCoroutine { cont ->
122
+ subscribe(object : SingleObserver <T & Any > {
124
123
override fun onSubscribe (d : Disposable ) { cont.disposeOnCancellation(d) }
125
- override fun onSuccess (t : T ) { cont.resume(t) }
124
+ override fun onSuccess (t : T & Any ) { cont.resume(t) }
126
125
override fun onError (error : Throwable ) { cont.resumeWithException(error) }
127
126
})
128
127
}
@@ -139,7 +138,8 @@ public suspend fun <T> SingleSource<T>.await(): T = suspendCancellableCoroutine
139
138
*
140
139
* @throws NoSuchElementException if the observable does not emit any value
141
140
*/
142
- public suspend fun <T > ObservableSource<T>.awaitFirst (): T = awaitOne(Mode .FIRST )
141
+ @Suppress(" UNCHECKED_CAST" )
142
+ public suspend fun <T > ObservableSource <T & Any >.awaitFirst (): T = awaitOne(Mode .FIRST ) as T
143
143
144
144
/* *
145
145
* Awaits the first value from the given [Observable], or returns the [default] value if none is emitted, without
@@ -150,7 +150,9 @@ public suspend fun <T> ObservableSource<T>.awaitFirst(): T = awaitOne(Mode.FIRST
150
150
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
151
151
* function immediately disposes of its subscription and resumes with [CancellationException].
152
152
*/
153
- public suspend fun <T > ObservableSource<T>.awaitFirstOrDefault (default : T ): T = awaitOne(Mode .FIRST_OR_DEFAULT , default)
153
+ @Suppress(" UNCHECKED_CAST" )
154
+ public suspend fun <T > ObservableSource <T & Any >.awaitFirstOrDefault (default : T ): T =
155
+ awaitOne(Mode .FIRST_OR_DEFAULT , default) as T
154
156
155
157
/* *
156
158
* Awaits the first value from the given [Observable], or returns `null` if none is emitted, without blocking the
@@ -161,7 +163,7 @@ public suspend fun <T> ObservableSource<T>.awaitFirstOrDefault(default: T): T =
161
163
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
162
164
* function immediately disposes of its subscription and resumes with [CancellationException].
163
165
*/
164
- public suspend fun <T > ObservableSource<T>.awaitFirstOrNull (): T ? = awaitOne(Mode .FIRST_OR_DEFAULT )
166
+ public suspend fun <T > ObservableSource <T & Any >.awaitFirstOrNull (): T ? = awaitOne(Mode .FIRST_OR_DEFAULT )
165
167
166
168
/* *
167
169
* Awaits the first value from the given [Observable], or calls [defaultValue] to get a value if none is emitted,
@@ -172,7 +174,7 @@ public suspend fun <T> ObservableSource<T>.awaitFirstOrNull(): T? = awaitOne(Mod
172
174
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
173
175
* function immediately disposes of its subscription and resumes with [CancellationException].
174
176
*/
175
- public suspend fun <T > ObservableSource<T>.awaitFirstOrElse (defaultValue : () -> T ): T =
177
+ public suspend fun <T > ObservableSource <T & Any >.awaitFirstOrElse (defaultValue : () -> T ): T =
176
178
awaitOne(Mode .FIRST_OR_DEFAULT ) ? : defaultValue()
177
179
178
180
/* *
@@ -185,7 +187,8 @@ public suspend fun <T> ObservableSource<T>.awaitFirstOrElse(defaultValue: () ->
185
187
*
186
188
* @throws NoSuchElementException if the observable does not emit any value
187
189
*/
188
- public suspend fun <T > ObservableSource<T>.awaitLast (): T = awaitOne(Mode .LAST )
190
+ @Suppress(" UNCHECKED_CAST" )
191
+ public suspend fun <T > ObservableSource <T & Any >.awaitLast (): T = awaitOne(Mode .LAST ) as T
189
192
190
193
/* *
191
194
* Awaits the single value from the given observable without blocking the thread and returns the resulting value, or,
@@ -198,26 +201,27 @@ public suspend fun <T> ObservableSource<T>.awaitLast(): T = awaitOne(Mode.LAST)
198
201
* @throws NoSuchElementException if the observable does not emit any value
199
202
* @throws IllegalArgumentException if the observable emits more than one value
200
203
*/
201
- public suspend fun <T > ObservableSource<T>.awaitSingle (): T = awaitOne(Mode .SINGLE )
204
+ @Suppress(" UNCHECKED_CAST" )
205
+ public suspend fun <T > ObservableSource <T & Any >.awaitSingle (): T = awaitOne(Mode .SINGLE ) as T
202
206
203
207
// ------------------------ private ------------------------
204
208
205
209
internal fun CancellableContinuation <* >.disposeOnCancellation (d : Disposable ) =
206
210
invokeOnCancellation { d.dispose() }
207
211
208
- private enum class Mode (val s : String ) {
212
+ private enum class Mode (@JvmField val s : String ) {
209
213
FIRST (" awaitFirst" ),
210
214
FIRST_OR_DEFAULT (" awaitFirstOrDefault" ),
211
215
LAST (" awaitLast" ),
212
216
SINGLE (" awaitSingle" );
213
217
override fun toString (): String = s
214
218
}
215
219
216
- private suspend fun <T > ObservableSource<T>.awaitOne (
220
+ private suspend fun <T > ObservableSource <T & Any >.awaitOne (
217
221
mode : Mode ,
218
222
default : T ? = null
219
- ): T = suspendCancellableCoroutine { cont ->
220
- subscribe(object : Observer <T > {
223
+ ): T ? = suspendCancellableCoroutine { cont ->
224
+ subscribe(object : Observer <T & Any > {
221
225
private lateinit var subscription: Disposable
222
226
private var value: T ? = null
223
227
private var seenValue = false
@@ -227,7 +231,7 @@ private suspend fun <T> ObservableSource<T>.awaitOne(
227
231
cont.invokeOnCancellation { sub.dispose() }
228
232
}
229
233
230
- override fun onNext (t : T ) {
234
+ override fun onNext (t : T & Any ) {
231
235
when (mode) {
232
236
Mode .FIRST , Mode .FIRST_OR_DEFAULT -> {
233
237
if (! seenValue) {
0 commit comments