com.github.pshirshov.izumi.idealingua.runtime.rpc.IRTResult.scala Maven / Gradle / Ivy
The newest version!
package com.github.pshirshov.izumi.idealingua.runtime.rpc
import scala.concurrent.{ExecutionContext, Future}
import scala.language.{higherKinds, implicitConversions}
import scala.util.{Failure, Try}
trait IRTResult[R[_]] {
@inline def map[A, B](r: R[A])(f: A => B): R[B]
@inline def flatMap[A, B](r: R[A])(f: A => R[B]): R[B]
@inline def wrap[A](v: => A): R[A]
@inline def raiseError[A](e: Throwable): R[A]
}
object IRTResult {
type Id[T] = T
@inline implicit def toOps[R[_], A](value: R[A]): ResultOps[R, A] = new ResultOps[R, A](value)
class ResultOps[R[_], A](val value: R[A]) extends AnyVal {
@inline def map[B](f: A => B)(implicit serviceResult: IRTResult[R]): R[B] = serviceResult.map(value)(f)
@inline def flatMap[B](f: A => R[B])(implicit serviceResult: IRTResult[R]): R[B] = serviceResult.flatMap(value)(f)
}
implicit object ResultId extends IRTResult[Id] {
@inline override def map[A, B](r: Id[A])(f: A => B) = f(r)
@inline override def flatMap[A, B](fa: Id[A])(f: A => Id[B]): Id[B] = f(fa)
@inline override def wrap[A](v: => A): Id[A] = v
@inline override def raiseError[A](e: Throwable): Id[A] = throw e
}
implicit object ResultOption extends IRTResult[Option] {
@inline override def map[A, B](r: Option[A])(f: A => B): Option[B] = r.map(f)
@inline override def flatMap[A, B](r: Option[A])(f: A => Option[B]): Option[B] = r.flatMap(f)
@inline override def wrap[A](v: => A): Option[A] = Option(v)
@inline override def raiseError[A](e: Throwable): Option[A] = throw e
}
implicit object ResultTry extends IRTResult[Try] {
@inline override def map[A, B](r: Try[A])(f: A => B): Try[B] = r.map(f)
@inline override def flatMap[A, B](r: Try[A])(f: A => Try[B]): Try[B] = r.flatMap(f)
@inline override def wrap[A](v: => A): Try[A] = Try(v)
@inline override def raiseError[A](e: Throwable): Try[A] = Failure(e)
}
implicit def toResultFutureOps(implicit ec: ExecutionContext): ResultFuture = new ResultFuture
class ResultFuture(implicit ec: ExecutionContext) extends IRTResult[Future] {
@inline override def map[A, B](r: Future[A])(f: A => B): Future[B] = r.map(f)
@inline override def flatMap[A, B](r: Future[A])(f: A => Future[B]): Future[B] = r.flatMap(f)
@inline override def wrap[A](v: => A): Future[A] = Future(v)
@inline override def raiseError[A](e: Throwable): Future[A] = Future.failed(e)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy