Skip to content

Instantly share code, notes, and snippets.

@ruslanbogun
Last active February 5, 2018 15:07
Show Gist options
  • Save ruslanbogun/49b83c36c11cc55bc9795f4226b1c40f to your computer and use it in GitHub Desktop.
Save ruslanbogun/49b83c36c11cc55bc9795f4226b1c40f to your computer and use it in GitHub Desktop.
import scodec.Codec
import scodec.codecs._
trait G
type A = G
type B = G
case class One(a: Int) extends G
implicit val codecOne: Codec[One] = int32L.as[One]
case class Two(a: Int, b: Int) extends G
implicit val codecTwo: Codec[Two] = (int16L :: int16L).as[Two]
case class Three(a: Int, b: Int, c: Int) extends G
implicit val codecThree: Codec[Three] = (int32L :: int16L :: int32L).as[Three]
case class Combs[A, B](a: A, b: B)
implicit def codecCombs[A, B](implicit ca: Codec[A], cb: Codec[B]): Codec[Combs[A, B]] =
variableSizeBytes(int32L, (implicitly[Codec[A]] :: implicitly[Codec[B]]).as[Combs[A, B]])
val a = Codec.encode(Combs(One(1), One(1))) // Successful(BitVector(96 bits, 0x080000000100000001000000))
val b = Codec.encode(Combs(One(2), Two(3, 4))) // Successful(BitVector(96 bits, 0x080000000200000003000400))
val c = Codec.encode(Combs(Two(1, 2), Three(4, 5, 6))) // Successful(BitVector(144 bits, 0x0e0000000100020004000000050006000000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment