Skip to content

Commit

Permalink
Merge pull request #87 from marcgrue/master
Browse files Browse the repository at this point in the history
Optimise materialised code + allow `= ???` and accessor methods
  • Loading branch information
mathieuleclaire authored May 22, 2020
2 parents c6f7246 + e6e8d11 commit fa1c5e2
Show file tree
Hide file tree
Showing 13 changed files with 340 additions and 340 deletions.
7 changes: 3 additions & 4 deletions autowire/shared/src/main/scala/autowire/Bounds.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package autowire
import acyclic.file
/**
* Utility classes to fit 0 or 2 context bounds into 1
*/
Expand All @@ -12,9 +11,9 @@ object Bounds{
*/
class Two[T, T1[_], T2[_]]()(implicit val t1: T1[T], val t2: T2[T])
object Two{
implicit def twoBounds[T, T1[_], T2[_]](implicit t1: T1[T], t2: T2[T]) = new Two()(t1, t2)
implicit def twoBounds[T, T1[_], T2[_]](implicit t1: T1[T], t2: T2[T]): Two[T, T1, T2] = new Two()(t1, t2)

def apply[T, T1[_], T2[_]]()(implicit two: Two[T, T1, T2]) = (two.t1, two.t2)
def apply[T, T1[_], T2[_]]()(implicit two: Two[T, T1, T2]): (T1[T], T2[T]) = (two.t1, two.t2)
}

/**
Expand All @@ -23,6 +22,6 @@ object Bounds{
*/
class None[T]
object None{
implicit def noBound[T] = new None[T]
implicit def noBound[T]: None[T] = new None[T]
}
}
12 changes: 6 additions & 6 deletions autowire/shared/src/main/scala/autowire/ClientServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package autowire

import scala.concurrent.Future
import scala.language.experimental.macros

/**
* A client to make autowire'd function calls to a particular interface.
* A single client can only make calls to one interface, but it's not a
Expand All @@ -16,7 +17,8 @@ trait Client[PickleType, Reader[_], Writer[_]] extends Serializers[PickleType, R
* @tparam Trait The interface that this autowire client makes its requests
* against.
*/
def apply[Trait] = ClientProxy[Trait, PickleType, Reader, Writer](this)
def apply[Trait]: ClientProxy[Trait, PickleType, Reader, Writer] =
ClientProxy[Trait, PickleType, Reader, Writer](this)

/**
* A method for you to override, that actually performs the heavy
Expand All @@ -32,11 +34,9 @@ trait Client[PickleType, Reader[_], Writer[_]] extends Serializers[PickleType, R
* followed by a `.call()` call) will turn into an RPC using the original
* [[Client]]
*/
case class ClientProxy[Trait,
PickleType,
Reader[_],
Writer[_]]
(self: Client[PickleType, Reader, Writer])
case class ClientProxy[Trait, PickleType, Reader[_], Writer[_]](
self: Client[PickleType, Reader, Writer]
)

trait Server[PickleType, Reader[_], Writer[_]] extends Serializers[PickleType, Reader, Writer] {
type Request = Core.Request[PickleType]
Expand Down
10 changes: 6 additions & 4 deletions autowire/shared/src/main/scala/autowire/Core.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,22 @@ object Core {
* exactly match the default value are omitted, and are
* simply re-constituted by the receiver.
*/
case class Request[PickleType](path : Seq[String], args: Map[String, PickleType])
case class Request[PickleType](path: Seq[String], args: Map[String, PickleType])
}

trait Error extends Exception
object Error{
object Error {
/**
* Signifies that something went wrong when de-serializing the
* raw input into structured data.
*
* This can contain multiple exceptions, one for each parameter.
*/
case class InvalidInput(exs: Param*) extends Exception with Error
case class InvalidInput(exs: Param*) extends Exception with Error {
override def toString: String = exs.mkString("InvalidInput(\n ", "\n ", "\n)")
}
sealed trait Param
object Param{
object Param {

/**
* Some parameter was missing from the input.
Expand Down
21 changes: 10 additions & 11 deletions autowire/shared/src/main/scala/autowire/Internal.scala
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
package autowire


import scala.concurrent.Future
import language.experimental.macros

import scala.language.implicitConversions

/**
* Holds a bunch of implementation details, which need to be public
* for various reasons, but really shouldn't be used directly.
*/
object Internal{
object Internal {

/**
* Low priority call-anything extension-method-holding trait, to give the
* call-Future extension method a chance to run first
*/
trait LowPri {
implicit def clientCallable[T](t: T) = new Internal.ClientCallable[T]
implicit def clientCallable[T](t: T): ClientCallable[T] = new Internal.ClientCallable[T]
}

/**
* A synthetic type purely meant to hold the `.call()` macro; gets
* erased completely when the macro-implementation of `.call()` runs
*/
class ClientCallable[T]{
class ClientCallable[T] {
@ScalaVersionStubs.compileTimeOnly(".call() method is synthetic and should not be used directly")
def call(): Future[T] = macro Macros.clientMacro[T]
}
Expand All @@ -34,24 +33,24 @@ object Internal{
def validate(current: List[FailMaybe]): FailAll = current match {
case first :: rest =>
(first, validate(rest)) match {
case (Right(_), Left(errors)) => Left(errors)
case (Right(_), Left(errors)) => Left(errors)
case (Right(success), Right(successes)) => Right(success :: successes)
case (Left(error), Left(errors)) => Left(error :: errors)
case (Left(error), Right(successes)) => Left(error :: Nil)
case (Left(error), Left(errors)) => Left(error :: errors)
case (Left(error), Right(successes)) => Left(error :: Nil)
}
case Nil =>
case Nil =>
Right(Nil)
}
// HNil[FailMaybe] -> HNil[Identity]
// HCon[A, HNil[FailMaybe], FailMaybe]
def doValidate(current: List[FailMaybe]): List[Any] = {
validate(current) match {
case Left(failures) => throw autowire.Error.InvalidInput(failures.reverse: _*)
case Right(res) => res
case Right(res) => res
}
}
def read[P, T](dict: Map[String, P], default: => FailMaybe, name: String, thunk: P => T): FailMaybe = {
dict.get(name).fold[Either[autowire.Error.Param, Any]](default)( x =>
dict.get(name).fold[Either[autowire.Error.Param, Any]](default)(x =>
util.Try(thunk(x)) match {
case scala.util.Success(value) => Right(value)
case scala.util.Failure(error) => Left(autowire.Error.Param.Invalid(name, error))
Expand Down
Loading

0 comments on commit fa1c5e2

Please sign in to comment.