55package kotlinx.coroutines.guava
66
77import 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.*
109import kotlinx.coroutines.*
1110import java.util.concurrent.*
1211import java.util.concurrent.CancellationException
@@ -138,10 +137,10 @@ public fun <T> ListenableFuture<T>.asDeferred(): Deferred<T> {
138137 // Finally, if this isn't done yet, attach a Listener that will complete the Deferred.
139138 val deferred = CompletableDeferred <T >()
140139 Futures .addCallback(this , object : FutureCallback <T > {
141- override fun onSuccess (result : T ? ) {
140+ override fun onSuccess (result : T ) {
142141 // Here we work with flexible types, so we unchecked cast to trick the type system
143142 @Suppress(" UNCHECKED_CAST" )
144- runCatching { deferred.complete(result as T ) }
143+ runCatching { deferred.complete(result) }
145144 .onFailure { handleCoroutineException(EmptyCoroutineContext , it) }
146145 }
147146
@@ -160,6 +159,7 @@ public fun <T> ListenableFuture<T>.asDeferred(): Deferred<T> {
160159 cancel(false )
161160 }
162161 // Return hides the CompletableDeferred. This should prevent casting.
162+ @OptIn(InternalForInheritanceCoroutinesApi ::class )
163163 return object : Deferred <T > by deferred {}
164164}
165165
@@ -225,7 +225,7 @@ public fun <T> Deferred<T>.asListenableFuture(): ListenableFuture<T> {
225225 *
226226 * This suspend function is cancellable.
227227 *
228- * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
228+ * If the [Job] of the current coroutine is cancelled while this suspending function is waiting, this function
229229 * stops waiting for the future and immediately resumes with [CancellationException][kotlinx.coroutines.CancellationException].
230230 *
231231 * This method is intended to be used with one-shot Futures, so on coroutine cancellation, the Future is cancelled as well.
@@ -247,8 +247,7 @@ public suspend fun <T> ListenableFuture<T>.await(): T {
247247 return suspendCancellableCoroutine { cont: CancellableContinuation <T > ->
248248 addListener(
249249 ToContinuation (this , cont),
250- MoreExecutors .directExecutor()
251- )
250+ MoreExecutors .directExecutor())
252251 cont.invokeOnCancellation {
253252 cancel(false )
254253 }
@@ -265,7 +264,7 @@ public suspend fun <T> ListenableFuture<T>.await(): T {
265264private class ToContinuation <T >(
266265 val futureToObserve : ListenableFuture <T >,
267266 val continuation : CancellableContinuation <T >
268- ) : Runnable {
267+ ): Runnable {
269268 override fun run () {
270269 if (futureToObserve.isCancelled) {
271270 continuation.cancel()
@@ -346,7 +345,7 @@ private class ListenableFutureCoroutine<T>(
346345 * could probably be compressed into one subclass of [AbstractFuture] to save an allocation, at the
347346 * cost of the implementation's readability.
348347 */
349- private class JobListenableFuture <T >(private val jobToCancel : Job ) : ListenableFuture<T> {
348+ private class JobListenableFuture <T >(private val jobToCancel : Job ): ListenableFuture<T> {
350349 /* *
351350 * Serves as a state machine for [Future] cancellation.
352351 *
@@ -356,7 +355,7 @@ private class JobListenableFuture<T>(private val jobToCancel: Job) : ListenableF
356355 *
357356 * To preserve Coroutine's [CancellationException], this future points to either `T` or [Cancelled].
358357 */
359- private val auxFuture = SettableFuture .create<Any >()
358+ private val auxFuture = SettableFuture .create<Any ? >()
360359
361360 /* *
362361 * `true` if [auxFuture.get][ListenableFuture.get] throws [ExecutionException].
@@ -441,7 +440,7 @@ private class JobListenableFuture<T>(private val jobToCancel: Job) : ListenableF
441440 }
442441
443442 /* * See [get()]. */
444- private fun getInternal (result : Any ): T = if (result is Cancelled ) {
443+ private fun getInternal (result : Any? ): T = if (result is Cancelled ) {
445444 throw CancellationException ().initCause(result.exception)
446445 } else {
447446 // We know that `auxFuture` can contain either `T` or `Cancelled`.
0 commit comments