Repository: Kotlin/kotlinx.coroutines
File: integration/kotlinx-coroutines-guava/src/ListenableFuture.kt (Kotlin)

Code before:
                // `this` future was cancelled by `Future.cancel`. In this case there's no cause or message.
                append("CANCELLED")
            } catch (e: ExecutionException) {
                append("FAILURE, cause=[${e.cause}]")
            } catch (t: Throwable) {
                // Violation of Future's contract, should never happen.
                append("UNKNOWN, cause=[${t.javaClass} thrown from get()]")
            }
        } else {
            append("PENDING, delegate=[$auxFuture]")
        }
    }
}

/**
 * Represents the result of a `Coroutine` in the sense of [Future].
 *
 * - When coroutine [isCompleted][Job.isCompleted] successfully, its result is wrapped into [Success].
 * - When coroutine [isCompleted][Job.isCompleted] exceptionally, meaning [isCancelled]
 *   returns `true`. According to [Future]'s contract, the exception can be of two types:
 *     - [CancellationException] if the coroutine was _cancelled normally_, meaning it threw [CancellationException].
 *       In this case the exception is wrapped into [Result.Cancellation].
 *     - [ExecutionException] with the original [Throwable] in its [cause][ExecutionException.cause] otherwise.
 *       This case is handled separately. See [JobListenableFuture.completeExceptionally].
 */
private sealed class Result<out T> {
    /** Returns cancellation _in the sense of [Future]_. This is _not_ equivalent to [Job.isCancelled] */
    open val isCancelled get() = false

    class Success<T>(val value: T) : Result<T>()
    class Cancellation<T>(val exception: CancellationException) : Result<T>() {
        override val isCancelled get() = true
    }
}


Review comment (suggestion):
Thanks for the explanation!
Would suggest getting rid of the `Result` clas tho: `Result.success` means a different thing in Kotlin (name clash with a popular stdlib class is not a great thing to have) and we also have to allocate on successful completion.

We can use raw value for the successful results and a `Cancelled(exception)` wrapper for cancellation exception. `isCancelled` then can be replaced with `val Any.is[Future]Cancelled() get() = this is Cancelled`

Code after:
                when (val result = Uninterruptibles.getUninterruptibly(auxFuture)) {
                    is Cancelled -> append("CANCELLED, cause=[${result.exception}]")
                    else -> append("SUCCESS, result=[$result")
                }
            } catch (e: CancellationException) {
                // `this` future was cancelled by `Future.cancel`. In this case there's no cause or message.
                append("CANCELLED")
            } catch (e: ExecutionException) {
                append("FAILURE, cause=[${e.cause}]")
            } catch (t: Throwable) {
                // Violation of Future's contract, should never happen.
                append("UNKNOWN, cause=[${t.javaClass} thrown from get()]")
            }
        } else {
            append("PENDING, delegate=[$auxFuture]")
        }
    }
}

/**
 * A wrapper for `Coroutine`'s [CancellationException].
 *
 * If the coroutine is _cancelled normally_, we want to show the reason of cancellation to the user. Unfortunately,
 * [SettableFuture] can't store the reason of cancellation. To mitigate this, we wrap cancellation exception into this
 * class and pass it into [SettableFuture.complete]. See implementation of [JobListenableFuture].
 */
private class Cancelled(@JvmField val exception: CancellationException)