FramedEncoder

Runtime entry point for the @FramedBy slicing-scheme encode emit. The KSP-generated @FramedBy codec calls encode, which:

  1. Allocates a GrowableWriteBuffer with a slack region at the front sized to fit the worst-case header bytes plus the worst-case framing prefix.

  2. Positions the growable past the slack region and invokes writeBody to write the body fields into the buffer.

  3. Computes bodyBytes = position - maxSlack and asks framingCodec for the actual prefix width via wireSize(bodyBytes, context).asExact.

  4. Right-flushes the prefix (and optional header) into the slack region so the wire bytes lie contiguously: [header?][prefix][body].

  5. Returns a ReadBuffer slice spanning exactly those wire bytes.

Body bytes are never moved — the slicing scheme leaves them at offset maxSlack and writes the prefix backwards into the slack instead. This is what makes the emit zero-memcpy for the body content (the design's headline property).

For a class with @FramedBy(codec, after = ""), headerWireWidth is 0 and writeHeader is null. For after = "X", headerWireWidth is the Exact wire width of X and writeHeader writes that field into the supplied buffer (the framework owns the header write so the gap between header and prefix is zero — eliminating the 1-byte memmove that an "in-variant header write" would otherwise require).

Functions

Link copied to clipboard
fun encode(factory: BufferFactory, framingCodec: BoundingLengthCodec<UInt>, context: EncodeContext, headerWireWidth: Int = 0, writeHeader: (PlatformBuffer) -> Unit? = null, initialBodyEstimate: Int = 256, writeBody: (WriteBuffer) -> Unit): ReadBuffer