aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Haller <hallerp@gmail.com>2013-04-26 16:27:06 +0200
committerPhilipp Haller <hallerp@gmail.com>2013-04-26 17:26:51 +0200
commit6f8dc1bd2c44b896c1beb2c94cc3a6bc2c7843de (patch)
treed4cfdde2e9a82415ec5a414c53cf4a47f4f5ca01
parentde3c0b61184b37525ca8dca829290fa19d2aca3a (diff)
downloadscala-async-6f8dc1bd2c44b896c1beb2c94cc3a6bc2c7843de.tar.gz
scala-async-6f8dc1bd2c44b896c1beb2c94cc3a6bc2c7843de.tar.bz2
scala-async-6f8dc1bd2c44b896c1beb2c94cc3a6bc2c7843de.zip
Split FutureSystem's completeProm extension point into 3 parts
This replaces the single, `Try`-dependent `completeProm` method with: def completeProm[A: WeakTypeTag]( prom: Expr[Prom[A]], value: Expr[A]): Expr[Unit] def completePromWithExceptionTopLevel[A: WeakTypeTag]( prom: Expr[Prom[A]], exception: Expr[Throwable]): Expr[Unit] def completePromWithFailedResult[A: WeakTypeTag]( prom: Expr[Prom[A]], resultName: TermName): Expr[Unit]
-rw-r--r--src/main/scala/scala/async/ExprBuilder.scala11
-rw-r--r--src/main/scala/scala/async/FutureSystem.scala44
2 files changed, 43 insertions, 12 deletions
diff --git a/src/main/scala/scala/async/ExprBuilder.scala b/src/main/scala/scala/async/ExprBuilder.scala
index ca46a83..b8b24c2 100644
--- a/src/main/scala/scala/async/ExprBuilder.scala
+++ b/src/main/scala/scala/async/ExprBuilder.scala
@@ -89,11 +89,9 @@ private[async] final case class ExprBuilder[C <: Context, FS <: FutureSystem](c:
*/
val ifIsFailureTree =
If(Select(Ident(name.tr), Try_isFailure),
- futureSystemOps.completeProm[T](
+ futureSystemOps.completePromWithFailedResult[T](
c.Expr[futureSystem.Prom[T]](Ident(name.result)),
- c.Expr[scala.util.Try[T]](
- TypeApply(Select(Ident(name.tr), newTermName("asInstanceOf")),
- List(TypeTree(weakTypeOf[scala.util.Try[T]]))))).tree,
+ name.tr).tree,
Block(List(tryGetTree, mkStateTree(nextState)), mkResumeApply)
)
@@ -311,8 +309,7 @@ private[async] final case class ExprBuilder[C <: Context, FS <: FutureSystem](c:
val caseForLastState: CaseDef = {
val lastState = asyncStates.last
val lastStateBody = c.Expr[T](lastState.body)
- val rhs = futureSystemOps.completeProm(
- c.Expr[futureSystem.Prom[T]](Ident(name.result)), reify(scala.util.Success(lastStateBody.splice)))
+ val rhs = futureSystemOps.completeProm(c.Expr[futureSystem.Prom[T]](Ident(name.result)), lastStateBody)
mkHandlerCase(lastState.state, rhs.tree)
}
asyncStates.toList match {
@@ -363,7 +360,7 @@ private[async] final case class ExprBuilder[C <: Context, FS <: FutureSystem](c:
EmptyTree,
Block(List({
val t = c.Expr[Throwable](Ident(name.tr))
- futureSystemOps.completeProm[T](c.Expr[futureSystem.Prom[T]](Ident(name.result)), reify(scala.util.Failure(t.splice))).tree
+ futureSystemOps.completePromWithExceptionTopLevel[T](c.Expr[futureSystem.Prom[T]](Ident(name.result)), t).tree
}), c.literalUnit.tree))), EmptyTree))
}
}
diff --git a/src/main/scala/scala/async/FutureSystem.scala b/src/main/scala/scala/async/FutureSystem.scala
index a050bec..c151d51 100644
--- a/src/main/scala/scala/async/FutureSystem.scala
+++ b/src/main/scala/scala/async/FutureSystem.scala
@@ -50,7 +50,13 @@ trait FutureSystem {
execContext: Expr[ExecContext]): Expr[Unit]
/** Complete a promise with a value */
- def completeProm[A](prom: Expr[Prom[A]], value: Expr[scala.util.Try[A]]): Expr[Unit]
+ def completeProm[A: WeakTypeTag](prom: Expr[Prom[A]], value: Expr[A]): Expr[Unit]
+
+ /** Complete a promise with an exception */
+ def completePromWithExceptionTopLevel[A: WeakTypeTag](prom: Expr[Prom[A]], exception: Expr[Throwable]): Expr[Unit]
+
+ /** Complete a promise with a failed result */
+ def completePromWithFailedResult[A: WeakTypeTag](prom: Expr[Prom[A]], resultName: TermName): Expr[Unit]
def spawn(tree: context.Tree): context.Tree =
future(context.Expr[Unit](tree))(execContext).tree
@@ -99,11 +105,26 @@ object ScalaConcurrentFutureSystem extends FutureSystem {
future.splice.onComplete(fun.splice)(execContext.splice)
}
- def completeProm[A](prom: Expr[Prom[A]], value: Expr[scala.util.Try[A]]): Expr[Unit] = reify {
- prom.splice.complete(value.splice)
+ def completeProm[A: WeakTypeTag](prom: Expr[Prom[A]], value: Expr[A]): Expr[Unit] = reify {
+ prom.splice.success(value.splice)
+ context.literalUnit.splice
+ }
+
+ def completePromWithExceptionTopLevel[A: WeakTypeTag](prom: Expr[Prom[A]], exception: Expr[Throwable]): Expr[Unit] = reify {
+ prom.splice.failure(exception.splice)
context.literalUnit.splice
}
+ def completePromWithFailedResult[A: WeakTypeTag](prom: Expr[Prom[A]], resultName: TermName): Expr[Unit] = {
+ val result = c.Expr[scala.util.Try[A]](
+ TypeApply(Select(Ident(resultName), newTermName("asInstanceOf")),
+ List(TypeTree(weakTypeOf[scala.util.Try[A]]))))
+ reify {
+ prom.splice.complete(result.splice)
+ context.literalUnit.splice
+ }
+ }
+
def castTo[A: WeakTypeTag](future: Expr[Fut[Any]]): Expr[Fut[A]] = reify {
future.splice.asInstanceOf[Fut[A]]
}
@@ -147,11 +168,24 @@ object IdentityFutureSystem extends FutureSystem {
context.literalUnit.splice
}
- def completeProm[A](prom: Expr[Prom[A]], value: Expr[scala.util.Try[A]]): Expr[Unit] = reify {
- prom.splice.a = value.splice.get
+ def completeProm[A: WeakTypeTag](prom: Expr[Prom[A]], value: Expr[A]): Expr[Unit] = reify {
+ prom.splice.a = value.splice
context.literalUnit.splice
}
+ def completePromWithExceptionTopLevel[A: WeakTypeTag](prom: Expr[Prom[A]], exception: Expr[Throwable]): Expr[Unit] = reify {
+ throw exception.splice
+ }
+
+ def completePromWithFailedResult[A: WeakTypeTag](prom: Expr[Prom[A]], resultName: TermName): Expr[Unit] = {
+ val result = c.Expr[scala.util.Try[A]](
+ TypeApply(Select(Ident(resultName), newTermName("asInstanceOf")),
+ List(TypeTree(weakTypeOf[scala.util.Try[A]]))))
+ reify {
+ throw result.splice.asInstanceOf[scala.util.Failure[A]].exception
+ }
+ }
+
def castTo[A: WeakTypeTag](future: Expr[Fut[Any]]): Expr[Fut[A]] = ???
}
}