Skip to content
This repository has been archived by the owner on Feb 20, 2019. It is now read-only.

Commit

Permalink
Add pickler and unpickler for Nothing
Browse files Browse the repository at this point in the history
* Add picklers for Nothing, `pickle` cannot be actually called. Reuse
  `FastTypeTag[Nothing]`, which was already defined. Include the
  picklers in `Defaults`.

* Add error in case the `unpickle` method is called.

* Remove annoying println in test `NullBinaryTest`.
  • Loading branch information
jvican committed Jun 1, 2016
1 parent ff0d63b commit bae3033
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
4 changes: 4 additions & 0 deletions core/src/main/scala/scala/pickling/PicklingErrors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,8 @@ object PicklingErrors {
final case class Wrapper(e: Throwable, msg: String, delimiter: String = "\n")
extends BasePicklingException(s"$msg$delimiter${e.getMessage}", Some(e))

/** Exception thrown when one tries to unpickle on a Unpickler[Nothing]. */
case object NothingIsNotUnpicklable extends PicklingRuntimeException(
s"You called `unpickle` on `Unpickler[Nothing]`, but it cannot be unpickled")

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package pickler
/** All pickler instances, including the low priority implicits. */
trait AllPicklers extends LowPriorityPicklers
with PrimitivePicklers
with NothingPicklers
with DatePicklers
with JavaBigDecimalPicklers
with JavaBigIntegerPicklers
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/scala/scala/pickling/pickler/Nothing.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package scala.pickling.pickler

import scala.pickling._
import PicklingErrors.NothingIsNotUnpicklable

trait NothingPicklers {
implicit object NothingPicklerUnpickler extends AbstractPicklerUnpickler[Nothing] {
override def tag: FastTypeTag[Nothing] = FastTypeTag.Nothing
/** Impossible to call in Scala, no value can be typed [[Nothing]] */
override def pickle(picklee: Nothing, builder: PBuilder): Unit = ???
/** Don't call [[unpickle]], [[Nothing]] cannot be unpickled. */
override def unpickle(tag: String, reader: PReader): Any =
throw NothingIsNotUnpicklable
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class NullBinaryTest extends FunSuite {
implicit val pd = implicitly[AbstractPicklerUnpickler[D]]
implicit val pc = implicitly[AbstractPicklerUnpickler[C]]
val pickle = c.pickle
println(pickle)
assert(pickle.value.mkString("[", ",", "]") === "[0,0,0,28,115,99,97,108,97,46,112,105,99,107,108,105,110,103,46,110,117,108,108,46,98,105,110,97,114,121,46,67,-2,0,0,0,0,-2]")
assert(pickle.unpickle[C].toString === c.toString)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package scala.pickling.runtime

import org.scalatest.FunSuite

import scala.pickling.Defaults._
import scala.pickling.json._

import scala.pickling.AbstractPicklerUnpickler
import scala.pickling.PicklingErrors.NothingIsNotUnpicklable

class NothingPickleUnpickleTest extends FunSuite {

val nothingPU = implicitly[AbstractPicklerUnpickler[Nothing]]

// `pickle` cannot simply be called, only test `unpickle`
test("unpickle `Nothing` throws an exception") {
intercept[NothingIsNotUnpicklable.type] {
val reader = pickleFormat.createReader(JSONPickle("{}"))
nothingPU.unpickle("", reader)
}
}

}

0 comments on commit bae3033

Please sign in to comment.