|
1 | 1 | package cats |
2 | 2 | package instances |
3 | 3 |
|
4 | | -import scala.util.control.NonFatal |
5 | 4 | import scala.concurrent.{ExecutionContext, Future} |
| 5 | +import scala.util.{Failure, Success} |
6 | 6 |
|
7 | 7 | trait FutureInstances extends FutureInstances1 { |
8 | 8 |
|
9 | 9 | implicit def catsStdInstancesForFuture( |
10 | 10 | implicit ec: ExecutionContext |
11 | 11 | ): MonadError[Future, Throwable] with CoflatMap[Future] with Monad[Future] = |
12 | 12 | new FutureCoflatMap with MonadError[Future, Throwable] with Monad[Future] with StackSafeMonad[Future] { |
13 | | - def pure[A](x: A): Future[A] = Future.successful(x) |
14 | | - |
15 | | - def flatMap[A, B](fa: Future[A])(f: A => Future[B]): Future[B] = fa.flatMap(f) |
16 | | - |
17 | | - def handleErrorWith[A](fea: Future[A])(f: Throwable => Future[A]): Future[A] = fea.recoverWith { case t => f(t) } |
18 | | - |
19 | | - def raiseError[A](e: Throwable): Future[A] = Future.failed(e) |
20 | | - override def handleError[A](fea: Future[A])(f: Throwable => A): Future[A] = fea.recover { case t => f(t) } |
21 | | - |
| 13 | + override def pure[A](x: A): Future[A] = |
| 14 | + Future.successful(x) |
| 15 | + override def flatMap[A, B](fa: Future[A])(f: A => Future[B]): Future[B] = |
| 16 | + fa.flatMap(f) |
| 17 | + override def handleErrorWith[A](fea: Future[A])(f: Throwable => Future[A]): Future[A] = |
| 18 | + fea.recoverWith { case t => f(t) } |
| 19 | + override def raiseError[A](e: Throwable): Future[A] = |
| 20 | + Future.failed(e) |
| 21 | + override def handleError[A](fea: Future[A])(f: Throwable => A): Future[A] = |
| 22 | + fea.recover { case t => f(t) } |
22 | 23 | override def attempt[A](fa: Future[A]): Future[Either[Throwable, A]] = |
23 | | - (fa.map(a => Right[Throwable, A](a))).recover { case NonFatal(t) => Left(t) } |
24 | | - |
25 | | - override def recover[A](fa: Future[A])(pf: PartialFunction[Throwable, A]): Future[A] = fa.recover(pf) |
26 | | - |
| 24 | + fa.transformWith( |
| 25 | + r => |
| 26 | + Future.successful( |
| 27 | + r match { |
| 28 | + case Success(a) => Right(a) |
| 29 | + case Failure(e) => Left(e) |
| 30 | + } |
| 31 | + ) |
| 32 | + ) |
| 33 | + override def redeemWith[A, B](fa: Future[A])(recover: Throwable => Future[B], bind: A => Future[B]): Future[B] = |
| 34 | + fa.transformWith { |
| 35 | + case Success(a) => bind(a) |
| 36 | + case Failure(e) => recover(e) |
| 37 | + } |
| 38 | + override def recover[A](fa: Future[A])(pf: PartialFunction[Throwable, A]): Future[A] = |
| 39 | + fa.recover(pf) |
27 | 40 | override def recoverWith[A](fa: Future[A])(pf: PartialFunction[Throwable, Future[A]]): Future[A] = |
28 | 41 | fa.recoverWith(pf) |
29 | | - |
30 | | - override def map[A, B](fa: Future[A])(f: A => B): Future[B] = fa.map(f) |
31 | | - |
32 | | - override def catchNonFatal[A](a: => A)(implicit ev: Throwable <:< Throwable): Future[A] = Future(a) |
33 | | - |
34 | | - override def catchNonFatalEval[A](a: Eval[A])(implicit ev: Throwable <:< Throwable): Future[A] = Future(a.value) |
| 42 | + override def map[A, B](fa: Future[A])(f: A => B): Future[B] = |
| 43 | + fa.map(f) |
| 44 | + override def catchNonFatal[A](a: => A)(implicit ev: Throwable <:< Throwable): Future[A] = |
| 45 | + Future(a) |
| 46 | + override def catchNonFatalEval[A](a: Eval[A])(implicit ev: Throwable <:< Throwable): Future[A] = |
| 47 | + Future(a.value) |
35 | 48 | } |
36 | 49 | } |
37 | 50 |
|
|
0 commit comments