implicit def btfIterable[T, C[T]]()

in bigtable/src/main/scala/magnolify/bigtable/BigtableType.scala [266:316]


  implicit def btfIterable[T, C[T]](implicit
    btf: Primitive[T],
    ti: C[T] => Iterable[T],
    fc: FactoryCompat[T, C[T]]
  ): Primitive[C[T]] =
    new Primitive[C[T]] {
      override val size: Option[Int] = None

      override def fromByteString(v: ByteString): C[T] = {
        val buf = v.asReadOnlyByteBuffer()
        val n = buf.getInt
        val b = fc.newBuilder
        btf.size match {
          case Some(s) =>
            val ba = new Array[Byte](s)
            (1 to n).foreach { _ =>
              buf.get(ba)
              b += btf.fromByteString(ByteString.copyFrom(ba))
            }
          case None =>
            (1 to n).foreach { _ =>
              val s = buf.getInt
              val ba = new Array[Byte](s)
              buf.get(ba)
              b += btf.fromByteString(ByteString.copyFrom(ba))
            }
        }
        b.result()
      }

      override def toByteString(v: C[T]): ByteString = {
        val buf = btf.size match {
          case Some(s) =>
            val bb = ByteBuffer.allocate(java.lang.Integer.BYTES + v.size * s)
            bb.putInt(v.size)
            v.foreach(x => bb.put(btf.toByteString(x).asReadOnlyByteBuffer()))
            bb
          case None =>
            val vs = v.map(btf.toByteString)
            val s = java.lang.Integer.BYTES * (v.size + 1) + vs.iterator.map(_.size()).sum
            val bb = ByteBuffer.allocate(s)
            bb.putInt(v.size)
            vs.foreach { v =>
              bb.putInt(v.size())
              bb.put(v.asReadOnlyByteBuffer())
            }
            bb
        }
        ByteString.copyFrom(buf.array())
      }
    }