5
5
package kotlinx.coroutines.guava
6
6
7
7
import com.google.common.util.concurrent.*
8
- import com.google.common.util.concurrent.internal.InternalFutureFailureAccess
9
- import com.google.common.util.concurrent.internal.InternalFutures
8
+ import com.google.common.util.concurrent.internal.*
10
9
import kotlinx.coroutines.*
11
10
import java.util.concurrent.*
12
11
import java.util.concurrent.CancellationException
@@ -138,10 +137,10 @@ public fun <T> ListenableFuture<T>.asDeferred(): Deferred<T> {
138
137
// Finally, if this isn't done yet, attach a Listener that will complete the Deferred.
139
138
val deferred = CompletableDeferred <T >()
140
139
Futures .addCallback(this , object : FutureCallback <T > {
141
- override fun onSuccess (result : T ? ) {
140
+ override fun onSuccess (result : T ) {
142
141
// Here we work with flexible types, so we unchecked cast to trick the type system
143
142
@Suppress(" UNCHECKED_CAST" )
144
- runCatching { deferred.complete(result as T ) }
143
+ runCatching { deferred.complete(result) }
145
144
.onFailure { handleCoroutineException(EmptyCoroutineContext , it) }
146
145
}
147
146
@@ -225,7 +224,7 @@ public fun <T> Deferred<T>.asListenableFuture(): ListenableFuture<T> {
225
224
*
226
225
* This suspend function is cancellable.
227
226
*
228
- * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
227
+ * If the [Job] of the current coroutine is cancelled while this suspending function is waiting, this function
229
228
* stops waiting for the future and immediately resumes with [CancellationException][kotlinx.coroutines.CancellationException].
230
229
*
231
230
* This method is intended to be used with one-shot Futures, so on coroutine cancellation, the Future is cancelled as well.
@@ -247,8 +246,7 @@ public suspend fun <T> ListenableFuture<T>.await(): T {
247
246
return suspendCancellableCoroutine { cont: CancellableContinuation <T > ->
248
247
addListener(
249
248
ToContinuation (this , cont),
250
- MoreExecutors .directExecutor()
251
- )
249
+ MoreExecutors .directExecutor())
252
250
cont.invokeOnCancellation {
253
251
cancel(false )
254
252
}
@@ -265,7 +263,7 @@ public suspend fun <T> ListenableFuture<T>.await(): T {
265
263
private class ToContinuation <T >(
266
264
val futureToObserve : ListenableFuture <T >,
267
265
val continuation : CancellableContinuation <T >
268
- ) : Runnable {
266
+ ): Runnable {
269
267
override fun run () {
270
268
if (futureToObserve.isCancelled) {
271
269
continuation.cancel()
@@ -346,7 +344,7 @@ private class ListenableFutureCoroutine<T>(
346
344
* could probably be compressed into one subclass of [AbstractFuture] to save an allocation, at the
347
345
* cost of the implementation's readability.
348
346
*/
349
- private class JobListenableFuture <T >(private val jobToCancel : Job ) : ListenableFuture<T> {
347
+ private class JobListenableFuture <T >(private val jobToCancel : Job ): ListenableFuture<T> {
350
348
/* *
351
349
* Serves as a state machine for [Future] cancellation.
352
350
*
@@ -356,7 +354,7 @@ private class JobListenableFuture<T>(private val jobToCancel: Job) : ListenableF
356
354
*
357
355
* To preserve Coroutine's [CancellationException], this future points to either `T` or [Cancelled].
358
356
*/
359
- private val auxFuture = SettableFuture .create<Any >()
357
+ private val auxFuture = SettableFuture .create<Any ? >()
360
358
361
359
/* *
362
360
* `true` if [auxFuture.get][ListenableFuture.get] throws [ExecutionException].
@@ -441,7 +439,7 @@ private class JobListenableFuture<T>(private val jobToCancel: Job) : ListenableF
441
439
}
442
440
443
441
/* * See [get()]. */
444
- private fun getInternal (result : Any ): T = if (result is Cancelled ) {
442
+ private fun getInternal (result : Any? ): T = if (result is Cancelled ) {
445
443
throw CancellationException ().initCause(result.exception)
446
444
} else {
447
445
// We know that `auxFuture` can contain either `T` or `Cancelled`.
0 commit comments