diff options
author | phaller <hallerp@gmail.com> | 2012-10-11 22:47:10 +0200 |
---|---|---|
committer | phaller <hallerp@gmail.com> | 2012-10-11 22:47:10 +0200 |
commit | 0bf18b09183878b9aae07bb1aa4093ca63ddef2b (patch) | |
tree | 593dd53fb9060cd7e94dfa8226ef68e05404aa64 | |
parent | 3fbc7a7ef86ee0afa95ff0af44e135db5f848c9d (diff) | |
download | scala-async-0bf18b09183878b9aae07bb1aa4093ca63ddef2b.tar.gz scala-async-0bf18b09183878b9aae07bb1aa4093ca63ddef2b.tar.bz2 scala-async-0bf18b09183878b9aae07bb1aa4093ca63ddef2b.zip |
Move expression utility to separate class outside asyncImpl
-rw-r--r-- | src/async/library/scala/async/Async.scala | 46 |
1 files changed, 26 insertions, 20 deletions
diff --git a/src/async/library/scala/async/Async.scala b/src/async/library/scala/async/Async.scala index adbf5dc..b72d088 100644 --- a/src/async/library/scala/async/Async.scala +++ b/src/async/library/scala/async/Async.scala @@ -11,6 +11,29 @@ import scala.concurrent.{ Future, Promise } import scala.util.control.NonFatal import scala.collection.mutable.ListBuffer +class ExprBuilder[C <: Context with Singleton](val c: C) { + import c.universe.{ reify, Literal, Constant } + + /* Make a partial function literal handling case #num: + * + * { + * case any if any == num => rhs + * } + */ + def mkHandler(num: Int, rhs: c.Expr[Unit]): c.Expr[PartialFunction[Int, Unit]] = { + val numLiteral = c.Expr[Int](Literal(Constant(num))) + + reify(new PartialFunction[Int, Unit] { + def isDefinedAt(`x$1`: Int) = + `x$1` == numLiteral.splice + def apply(`x$1`: Int) = `x$1` match { + case any: Int if any == numLiteral.splice => + rhs.splice + } + }) + } +} + /* * @author Philipp Haller */ @@ -23,24 +46,7 @@ object Async extends AsyncUtils { def asyncImpl[T: c.WeakTypeTag](c: Context)(body: c.Expr[T]): c.Expr[Future[T]] = { import c.universe._ - /* Make a partial function literal handling case #num: - * - * { - * case any if any == num => rhs - * } - */ - def mkHandler(num: Int, rhs: c.Expr[Unit]): c.Expr[PartialFunction[Int, Unit]] = { - val numLiteral = c.Expr[Int](Literal(Constant(num))) - - reify(new PartialFunction[Int, Unit] { - def isDefinedAt(`x$1`: Int) = - `x$1` == numLiteral.splice - def apply(`x$1`: Int) = `x$1` match { - case any: Int if any == numLiteral.splice => - rhs.splice - } - }) - } + val builder = new ExprBuilder[c.type](c) class AsyncStateBuilder { /* Statements preceding an await call. */ @@ -117,7 +123,7 @@ object Async extends AsyncUtils { assert(awaitable != null) val nakedStats = stats.map(stat => c.resetAllAttrs(stat.duplicate)) val block = Block((nakedStats :+ mkOnCompleteTree): _*) - mkHandler(num, c.Expr[Unit](block)) + builder.mkHandler(num, c.Expr[Unit](block)) } def lastExprTree: c.Tree = { @@ -197,7 +203,7 @@ object Async extends AsyncUtils { val handlerForLastState: c.Expr[PartialFunction[Int, Unit]] = { val tree = Apply(Select(Ident("result"), c.universe.newTermName("success")), List(asyncStates(indexOfLastState).lastExprTree)) - mkHandler(indexOfLastState + 1, c.Expr[Unit](tree)) + builder.mkHandler(indexOfLastState + 1, c.Expr[Unit](tree)) } vprintln("GENERATED handler for last state:") |