-
Notifications
You must be signed in to change notification settings - Fork 509
/
Outcome.scala
248 lines (207 loc) · 7.79 KB
/
Outcome.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* Copyright 2020-2022 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cats.effect.kernel
import cats.{
~>,
Applicative,
ApplicativeError,
Bifunctor,
Eq,
Monad,
MonadError,
Order,
Show,
Traverse
}
import cats.syntax.all._
import scala.annotation.tailrec
import scala.util.{Either, Left, Right}
/**
* Represents the result of the execution of a fiber. It may terminate in one of 3 states:
* 1. Succeeded(fa) The fiber completed with a value.
*
* A commonly asked question is why this wraps a value of type `F[A]` rather than one of type
* `A`. This is to support monad transformers. Consider
*
* {{{
* val oc: OutcomeIO[Int] =
* for {
* fiber <- Spawn[OptionT[IO, *]].start(OptionT.none[IO, Int])
* oc <- fiber.join
* } yield oc
* }}}
*
* If the fiber succeeds then there is no value of type `Int` to be wrapped in `Succeeded`,
* hence `Succeeded` contains a value of type `OptionT[IO, Int]` instead.
*
* In general you can assume that binding on the value of type `F[A]` contained in `Succeeded`
* does not perform further effects. In the case of `IO` that means that the outcome has been
* constructed as `Outcome.Succeeded(IO.pure(result))`.
*
* 2. Errored(e) The fiber exited with an error.
*
* 3. Canceled() The fiber was canceled, either externally or self-canceled via
* `MonadCancel[F]#canceled`.
*/
sealed trait Outcome[F[_], E, A] extends Product with Serializable {
import Outcome._
def embed(onCancel: F[A])(implicit F: MonadCancel[F, E]): F[A] =
fold(onCancel, F.raiseError, identity)
def embedNever(implicit F: GenSpawn[F, E]): F[A] =
embed(F.never)
/**
* Allows the restoration to a normal development flow from an Outcome.
*
* This can be useful for storing the state of a running computation and then waiters for that
* data can act and continue forward on that shared outcome. Cancelation is encoded as a
* `CancellationException`.
*/
def embedError(implicit F: MonadCancel[F, E], ev: Throwable <:< E): F[A] =
embed(
F.raiseError(ev(new java.util.concurrent.CancellationException("Outcome was Canceled"))))
def fold[B](canceled: => B, errored: E => B, completed: F[A] => B): B =
this match {
case Canceled() => canceled
case Errored(e) => errored(e)
case Succeeded(fa) => completed(fa)
}
def mapK[G[_]](f: F ~> G): Outcome[G, E, A] =
this match {
case Canceled() => Canceled()
case Errored(e) => Errored(e)
case Succeeded(fa) => Succeeded(f(fa))
}
def isSuccess: Boolean = this match {
case Canceled() => false
case Errored(_) => false
case Succeeded(_) => true
}
def isError: Boolean = this match {
case Canceled() => false
case Errored(_) => true
case Succeeded(_) => false
}
def isCanceled: Boolean = this match {
case Canceled() => true
case Errored(_) => false
case Succeeded(_) => false
}
}
private[kernel] trait LowPriorityImplicits {
import Outcome.{Canceled, Errored, Succeeded}
// variant for when F[A] doesn't have a Show (which is, like, most of the time)
implicit def showUnknown[F[_], E, A](implicit E: Show[E]): Show[Outcome[F, E, A]] =
Show show {
case Canceled() => "Canceled"
case Errored(left) => s"Errored(${left.show})"
case Succeeded(_) => s"Succeeded(...)"
}
implicit def eq[F[_], E: Eq, A](implicit FA: Eq[F[A]]): Eq[Outcome[F, E, A]] =
Eq instance {
case (Canceled(), Canceled()) => true
case (Errored(left), Errored(right)) => left === right
case (Succeeded(left), Succeeded(right)) => left === right
case _ => false
}
implicit def applicativeError[F[_], E](
implicit F: Applicative[F]): ApplicativeError[Outcome[F, E, *], E] =
new OutcomeApplicativeError[F, E]
protected class OutcomeApplicativeError[F[_]: Applicative, E]
extends ApplicativeError[Outcome[F, E, *], E]
with Bifunctor[Outcome[F, *, *]] {
def pure[A](x: A): Outcome[F, E, A] = Succeeded(x.pure[F])
def handleErrorWith[A](fa: Outcome[F, E, A])(f: E => Outcome[F, E, A]): Outcome[F, E, A] =
fa.fold(Canceled(), f, Succeeded(_: F[A]))
def raiseError[A](e: E): Outcome[F, E, A] = Errored(e)
def ap[A, B](ff: Outcome[F, E, A => B])(fa: Outcome[F, E, A]): Outcome[F, E, B] =
(ff, fa) match {
case (Succeeded(cfa), Succeeded(fa)) =>
Succeeded(cfa.ap(fa))
case (Errored(e), _) =>
Errored(e)
case (Canceled(), _) =>
Canceled()
case (_, Errored(e)) =>
Errored(e)
case (_, Canceled()) =>
Canceled()
}
def bimap[A, B, C, D](fab: Outcome[F, A, B])(f: A => C, g: B => D): Outcome[F, C, D] =
fab match {
case Succeeded(fa) => Succeeded(fa.map(g))
case Errored(e) => Errored(f(e))
case Canceled() => Canceled()
}
}
}
object Outcome extends LowPriorityImplicits {
def succeeded[F[_], E, A](fa: F[A]): Outcome[F, E, A] =
Succeeded(fa)
def errored[F[_], E, A](e: E): Outcome[F, E, A] =
Errored(e)
def canceled[F[_], E, A]: Outcome[F, E, A] =
Canceled()
def fromEither[F[_]: Applicative, E, A](either: Either[E, A]): Outcome[F, E, A] =
either.fold(Errored(_), a => Succeeded(a.pure[F]))
implicit def order[F[_], E: Order, A](implicit FA: Order[F[A]]): Order[Outcome[F, E, A]] =
Order.from {
case (Canceled(), Canceled()) => 0
case (Errored(left), Errored(right)) => left.compare(right)
case (Succeeded(lfa), Succeeded(rfa)) => lfa.compare(rfa)
case (Canceled(), _) => -1
case (_, Canceled()) => 1
case (Errored(_), Succeeded(_)) => -1
case (Succeeded(_), Errored(_)) => 1
}
implicit def show[F[_], E, A](implicit FA: Show[F[A]], E: Show[E]): Show[Outcome[F, E, A]] =
Show show {
case Canceled() => "Canceled"
case Errored(left) => s"Errored(${left.show})"
case Succeeded(right) => s"Succeeded(${right.show})"
}
implicit def monadError[F[_], E](
implicit F: Monad[F],
FT: Traverse[F]): MonadError[Outcome[F, E, *], E] =
new OutcomeApplicativeError[F, E]()(F) with MonadError[Outcome[F, E, *], E] {
override def map[A, B](fa: Outcome[F, E, A])(f: A => B): Outcome[F, E, B] =
bimap(fa)(identity, f)
def flatMap[A, B](fa: Outcome[F, E, A])(f: A => Outcome[F, E, B]): Outcome[F, E, B] =
fa match {
case Succeeded(ifa) =>
Traverse[F].traverse(ifa)(f) match {
case Succeeded(ifaa) => Succeeded(Monad[F].flatten(ifaa))
case Errored(e) => Errored(e)
case Canceled() => Canceled()
}
case Errored(e) => Errored(e)
case Canceled() => Canceled()
}
@tailrec
def tailRecM[A, B](a: A)(f: A => Outcome[F, E, Either[A, B]]): Outcome[F, E, B] =
f(a) match {
case Succeeded(fa) =>
Traverse[F].sequence[Either[A, *], B](fa) match { // Dotty can't infer this
case Left(a) => tailRecM(a)(f)
case Right(fb) => Succeeded(fb)
}
case Errored(e) => Errored(e)
case Canceled() => Canceled()
}
}
final case class Succeeded[F[_], E, A](fa: F[A]) extends Outcome[F, E, A]
final case class Errored[F[_], E, A](e: E) extends Outcome[F, E, A]
final case class Canceled[F[_], E, A]() extends Outcome[F, E, A]
}