-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EntrypointDefExt.scala
31 lines (25 loc) · 1.07 KB
/
EntrypointDefExt.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package japgolly.webapputil.boopickle
import boopickle._
import japgolly.webapputil.binary._
import japgolly.webapputil.entrypoint._
import java.nio.ByteBuffer
object EntrypointDefExt {
import EntrypointDef.Codec
trait Implicits {
@inline final implicit def EntrypointDefCodecBoopickleExt[A](self: Codec[A]): Implicits.EntrypointDefCodecBoopickleExt[A] =
new Implicits.EntrypointDefCodecBoopickleExt[A](self)
@inline final implicit def implicitEntrypointDefCodecViaBoopickle[A](implicit p: Pickler[A]): EntrypointDef.Codec[A] =
EntrypointDef.Codec.binary.pickle[A]
}
object Implicits extends Implicits {
final class EntrypointDefCodecBoopickleExt[A](private val self: Codec[A]) extends AnyVal {
type ThisIsBinary = Codec[A] =:= Codec[BinaryData]
def pickle[B](implicit pickler: Pickler[B], ev: ThisIsBinary): Codec[B] = {
val unpickle = UnpickleImpl[B]
ev(self)
.xmap[ByteBuffer](_.unsafeByteBuffer)(BinaryData.unsafeFromByteBuffer)
.xmap(unpickle.fromBytes(_))(PickleImpl.intoBytes(_))
}
}
}
}