title: How to ignore Codec after using it to limit size bytes
body: <p>I would create a model for the KMIP protocols that works with TTLV encoding (<a href="http://docs.oasis-open.org/kmip/spec/v1.4/os/kmip-spec-v1.4-os.html#_Toc490660911" rel="nofollow noreferrer">Tag, Type, Length, Value</a>)</p>
  
  <p>The <code>ttlv</code> function is "high level" and accepts the tag, the type and the codec of the value.</p>
  
  <pre><code> def ttlv[A&lt;:HList](tag:ByteVector, itype:ByteVector, value: Codec[A]) =
     constant(tag) :: constant(itype) :: 
     (uint32 &gt;&gt;:~ {
       case length =&gt; limitedSizeBytes(length, value)
     })
  </code></pre>
  
  <p>The result is a <code>Codec[Unit :: Unit :: Long :: A]</code>. However, I would have a <code>Codec[Unit :: Unit :: Unit :: A]</code> (or <code>Codec[A]</code>) to cast the codec to a <code>case class</code> with only the value <code>A</code>. How to ignore the <code>uint32</code> after used by <code>limitedSizeBytes</code> ? Otherwise I am interested for comments for better approaches.</p>
  
  <hr>
  
  <p>Here are <code>case class</code> examples:</p>
  
  <pre><code>case class RequestHeader(protocol:Int)
  case class RequestPayload(payload:CompromiseDate, name:CertificateName)
  case class RequestMessage(header:RequestHeader, payload: RequestPayload)
  case class CompromiseDate(value:Int)
  case class CertificateName(value:String)
  </code></pre>
  
  <p>More high level functions like <code>ttlvStructure</code></p>
  
  <pre><code>def ttlvStructure[A&lt;:HList](tag:ByteVector, struct:Codec[A]) =
    ttlv(tag, hex"01", struct)
  def ttlvTextString(tag:ByteVector) =
    ttlv(tag, hex"07", utf8.hlist)
  def ttlvInt(tag:ByteVector) =
    ttlv(tag, hex"02", int32.hlist)
  </code></pre>
  
  <p>And the final codec:</p>
  
  <pre><code>implicit val certificateNameCodec =
    ttlvTextString(hex"420020").as[CertificateName]
  
  implicit val compromiseDateCodec =
    ttlvInt(hex"420021").as[CompromiseDate]
  
  implicit val requestPayloadCodec =
    ttlvStructure(hex"420003", Codec[CompromiseDate] :: Codec[CertificateName]).as[RequestPayload]
  
  implicit val requestHeaderCodec =
    ttlvInt(hex"420002").as[RequestHeader]
  
  implicit val requestMessageCodec =
    ttlvStructure(hex"420001", Codec[RequestHeader] :: Codec[RequestPayload]).as[RequestMessage]
  </code></pre>
  
  <p>Example of object to encode: </p>
  
  <pre><code>val m = RequestMessage(
        RequestHeader(14),
        RequestPayload(
          CompromiseDate(8),
          CertificateName("certificate name")
        )
      )
  </code></pre>
  
  <hr>
  
  <p><strong>Solution</strong>:</p>
  
  <p><code>variableSizeBytesLong</code> is here to do what I want: </p>
  
  <pre><code>def ttlv[A&lt;:HList](tag:ByteVector, itype:ByteVector, value: Codec[A]): Codec[A] =
      (constant(tag) :~&gt;: constant(itype) :~&gt;: variableSizeBytesLong(uint32, value))
  </code></pre>